Hi,
I want to retrieve a binary file from SQL Server using ADO.NET. How can I accomplish this?
Thanks in advance. :)
Printable View
Hi,
I want to retrieve a binary file from SQL Server using ADO.NET. How can I accomplish this?
Thanks in advance. :)
These are the steps I used.
1. Create a dataset control from your data tab. Put a table in it with a single image field (really a binary field).
2. Create a connection to your SQL Database with a dataadapter control.
3. Fill the dataset field with the image field from your SQL Database.
4. Use a binary writer to put the contents to a file.
My example below retrieves a JPG from my database and saves it to a file called temp.jpg. It assumes you have a dataset called dsTables with a table called Image and a dataadapter called sdaImage with a table called images and two fields, one called image and one called image_number.
Code:dsTables.Tables("Image").Clear()
Dim fsImage As New System.IO.FileStream("test.jpg", System.IO.FileMode.Create)
Dim bwImage As New System.IO.BinaryWriter(fsImage)
sdaImage.SelectCommand.CommandText = "select image from images where image_number = '12345' "
Dim nRecords as Int16 ' holds number of records returned by fill
nRecords sdaImage.Fill(dsTables, "Image") 'fill dataset with binary blob
If nRecords > 0 Then
bwImage.Write(dsTables.Tables("Image").Rows(0).Item(0)) 'retrieve binary blob from dataset
End If
bwImage.Flush() 'write binary blob to file specified above
bwImage.Close()
fsImage = Nothing
bwImage = Nothing
Hi prakash
you use stream object here
in .net we can use the stream object for binary file and alos picture object