Reading BLOB data from SQL using a datareader

J

Jason James

Hi all,

I have some working code that reads BLOBs from an
SQL database, but it reads them into a dataset. This
is fine for small BLOBs, but if they are large I want to
be able to retreive them only when needed. Therefore
I should like to use a datareader. Can this be done?
Does anyone have any sample code or useful
resources that could help me with this task.

Many thanks,

Jason.
 
J

Jason James

Hi guys,

the BLOB data is an image and I would also like to
know how to then insert the image into an imagebox.

Regards,

Jason.
 
V

Val Mazur \(MVP\)

Jason,

Here is what I used some time ago

Dim loConnection As SqlClient.SqlConnection
Dim loCommand As SqlClient.SqlCommand
Dim loDataReader As SqlClient.SqlDataReader
Dim loBuffer() As Byte
Dim lnRealLength As Long
Dim loStream As System.IO.MemoryStream

'Instantiate new connection object
loConnection = New SqlClient.SqlConnection()

'Prepare connection string and open connection to database
With loConnection
.ConnectionString = "Data Source='ExampleServer';" & _
"Initial Catalog=Examples;Integrated Security=SSPI"

.Open()

'instantiate new command object which
'we will use to read image from table
loCommand = New SqlClient.SqlCommand()
loCommand.CommandText = "SELECT Product_Image " & _
"FROM tblProductImages WHERE Product_ID=1"
loCommand.CommandType = CommandType.Text
loCommand.Connection = loConnection

'Execute prepared command to provide actual
'reading of previously saved binary data
loDataReader = _
loCommand.ExecuteReader( _
CommandBehavior.SequentialAccess)

'instantiate new memory stream to load
'data from binary field
loStream = New System.IO.MemoryStream()

'read actual value from field
Do While loDataReader.Read()
'Get actual length of data in bytes
lnRealLength = loDataReader.GetBytes( _
0, 0, Nothing, 0, Integer.MaxValue)

'Reallocate storage space for an array variable to the
'size of data
ReDim loBuffer(lnRealLength)

'Load data from field into array
lnRealLength = loDataReader.GetBytes( _
0, 0, loBuffer, 0, lnRealLength)

'Load array into prepared stream to allow
'to show it in PictureBox control
loStream.Write(loBuffer, 0, lnRealLength)

'Load image into PictureBox control
PictureBox1.Image = Image.FromStream(loStream)
Loop

'Close all opened resources and objects
loStream.Close()
loStream = Nothing
loDataReader.Close()
.Close()
loCommand.Dispose()
loCommand = Nothing
loConnection.Dispose()
loConnection = Nothing
End With
 
J

Jason James

Val,

I've spent some time working on the solution and things are
working great now. Thanks for the help.

Jason
 
C

Cor Ligthert [MVP]

Jason,

You make me curious, you asked for a solution for an imagebox control, which
is a webcontrol. In my opinon is Val giving you a solution for a picturebox
control what is a windowsform control.

What did you ask?

Cor
 
J

Jason James

Cor,

my appologies. It was a picture box control, not an image control.

Your information helped to point me in the right direction for some
general information that was useful in solving the problem. I was
just struggling with the memory stream and getbytes.

Many thanks for your assistance.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top