Friday, August 12, 2005

vbCity/DevCity.NET Forums :: Visual Basic :: VB & Databases :: FAQ :: How to Read/Write Image files from/to Access DB using ADO:

This sample demonstrates how to use ADO object model to read/write Image files from/to an Access Database. ADO object model provides great capability to developers. Using ADO it is possible to access Data from any kind of Data source. That's why Microsoft uses the Term "Universal Data Access". Basically in this sample we will be using the 2 methods exposed by ADO Recordset object called AppendChunk and GetChuck.

AppendChunk appends data to a large text or binary data Field, or to a Parameter object and it follows the syntax

Code:
object.AppendChunk Data

Use the AppendChunk method on a Field or Parameter object to fill it with long binary or character data. You can use the AppendChunk method to manipulate long values in portions rather than in their entirety. The first AppendChunk call on a Field object writes data to the field, overwriting any existing data. Subsequent AppendChunk calls add to existing data.

Note: Only the important code sections are highlighted here. Pls refer the attched code sample for complete code listing.

In this eg. we use Common dialog control to open an Image file. It can be either bmp,co,gif, or jpg.

Code:
CommonDialog1.Filter = "(*.bmp;*.ico;*.gif;*.jpg)/*.bmp;*.ico;*.gif;*.jpg"
CommonDialog1.ShowOpen
PictBmp = CommonDialog1.FileName

then based on the file, we use the file Access method (Binary Read operation) on the PictBmp to read (converts the file
to binary data)

Code:
SourceFile = FreeFile
Open PictBmp For Binary Access Read As SourceFile

then we use the Appendchunk Method of the ADO Recordset object to append the value in the byte array to the field Pict in the EMP table

Code:
Get SourceFile, , ByteData() 'Reads data from an open disk file into ByteData()
Rs(1).AppendChunk ByteData() 'Appends the left over data first

Once all the Data is append to the database call the Update method of the ADO recordset to save the changes to the DB and finall close the file handle

Code:
Rs.Update 'Commit the new data.
Close SourceFile

Reading the Image Data from the DB

In order to read the image data, we follow the exact opposite of the write method. wht basically we do here is read the binary data from
the Database and write the binary data to a disk file using the binary write method.

Code:
DiskFile = App.Path & "\image1.bmp"

If Len(Dir$(DiskFile)) > 0 Then
Kill DiskFile
End If

DestFileNum = FreeFile
Open DiskFile For Binary As DestFileNum

But inorder to get the binary data from the DB, we use the GetChunk method of the ADO Recordset object. the binary data is then transfered to the disk file using the Put method.

Code:
ByteData() = Rs(1).GetChunk(LeftOver)
Put DestFileNum, , ByteData() 'write data from a variable to disk file

Once the data is written to the disk file which is of the type image,close the filehandle and load the image file into a picture box or image control using the LoadPicture Method.

Code:
Close DestFileNum

Image1.Visible = True
Image1.Picture = LoadPicture(App.Path & "\image1.bmp")

0 Comments:

Post a Comment

<< Home