Hi to all,
May I ask any help for a code in VB6 in saving images in either SQL server or MS Access databases? Please give me some idea thanks a lot in advance.

Dani AI

Generated

Following 's original question and 's attached example, here are concise, practical options and extra tips (also useful for who couldn't download the attachment). The two common approaches are: store the raw image bytes in the database (blob) or store files on disk and save only paths in the DB. For small apps or when you need atomic backups, DB blobs are fine; for many/large images prefer the filesystem or a dedicated file store for performance and backup reasons.

Quick VB6 pattern (ADO + ADODB.Stream) that works for SQL Server (varbinary(max)) and similarly for Access (OLE Object or Attachment type). Make sure you have a reference to "Microsoft ActiveX Data Objects x.x Library" in Project → References. Use parameterized commands—do not embed binary into SQL text.

' Insert image file into DB (VB6)
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim stm As ADODB.Stream

Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;Data Source=SERVER;Initial Catalog=DB;Integrated Security=SSPI;"

Set stm = New ADODB.Stream
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile "C:\images\photo.jpg"

Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cn
cmd.CommandText = "INSERT INTO Images (FileName, ImageData) VALUES (?, ?)"
cmd.CommandType = adCmdText
cmd.Parameters.Append cmd.CreateParameter("pName", adVarChar, adParamInput, 255, "photo.jpg")
cmd.Parameters.Append cmd.CreateParameter("pImg", adLongVarBinary, adParamInput, , stm.Read)
cmd.Execute

stm.Close
cn.Close

To read back, SELECT the varbinary/OLE field and write it with ADODB.Stream.SaveToFile. Common troubleshooting: confirm correct provider (Jet vs ACE for Access), ensure column type supports large binary (SQL Server: varbinary(max); Access: Attachment or OLE—watch for OLE headers), use parameters to avoid type/coercion errors, and test with small images first. If you see extra OLE header bytes when using Access, consider storing raw bytes, or use Access Attachment field (2007+) or store files on disk and keep only the path in the DB for a simpler, more scalable solution.

Recommended Answers

All 4 Replies

Attached is sample code. Very important though is to remember that the field specified in sFieldName, must have a binary field type (ie. OLE Object in access) in your table....

thanks guys I owe you one. This will help me a lot

Thanks guys this will help me a lot.

i m not download ur file, plz send me vb code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.