Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Image in SQL Server 7.0 using VB


Message #1 by "Sergio Davi Silva Bezerra" <davi@c...> on Tue, 4 Jul 2000 14:34:50
You need to use the AppendChunk method of an ADO field object to insert an
image into a database field. For example, this code (modified from ADO 2.1
Programmer's Reference p. 176) adds a new record to a database with an image
field and then loads the image from the field into a PictureBox on a VB form
(so you can see whether it worked):

Dim objConn As ADODB.Connection
Dim objComm As ADODB.Command
Dim objRec As ADODB.Recordset
Dim bytChunk() As Byte
Dim bytChunk2() As Byte

'Open the file with the image
Open "C:\sample.jpg" For Binary As #1

' Load the image into the array bytChunk
ReDim bytChunk(LOF(1))
Get #1, , bytChunk()
Close #1

' Open the connection
Set objConn = New ADODB.Connection
objConn.Open "Provider=SQLOLEDB;Data Source=JULIANS;Initial
Catalog=blob;User ID=sa;Password="

' Open the recordset
Set objRec = New ADODB.Recordset
objRec.Open "SELECT * FROM tbl_blob", objConn, adOpenDynamic,
adLockOptimistic

' add a new record
objRec.AddNew

' Assign the byte array to the field with the AppendChunk method
objRec("pic").AppendChunk bytChunk
objRec.Update

' Go to the record we've just added
objRec.MoveLast

' save the image from the field to a new file and load it into the picture
box
bytChunk2 = objRec("pic")
Open "c:\new_sample.jpg" For Binary As #2
Put #2, , bytChunk2()
Close #2
Picture1.Picture = LoadPicture("C:\new_sample.jpg")

' clean up
objRec.Close
objConn.Close
Set objRec = Nothing
Set objConn = Nothing
Set objComm = Nothing

Hope this helps.
Thanks,

Julian Skinner
Technical Editor, Wrox Press
http://www.wrox.com/
julians@w...


-----Original Message-----
From: Sergio Davi Silva Bezerra [mailto:davi@c...]
Sent: Tuesday, July 04, 2000 3:35 PM
To: professional vb
Subject: [pro_vb] Image in SQL Server 7.0 using VB


Please, how do i store image (Windows Bitmap) in SQL Server 7.0 using VB?

Sergio Davi Bezerra

  Return to Index