Dim pubsConn As New OleDbConnection("connectionstring")
Dim logoCMD As New OleDbCommand("SELECT pub_id, logo FROM pub_info", pubsConn)
Dim fs As IO.FileStream ' Writes the BLOB to a file (*.bmp).
Dim bw As IO.BinaryWriter ' Streams the binary data to the FileStream object.
Dim bufferSize As Integer = 100 ' The size of the BLOB buffer.
Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte() buffer to be filled by GetBytes.
Dim retval As Long ' The bytes returned from GetBytes.
Dim startIndex As Long = 0 ' The starting position in the BLOB output.
Dim pub_id As String = "" ' The publisher id to use in the file name.
' Open the connection and read data into the DataReader.
pubsConn.Open()
Dim myReader As OleDbDataReader = logoCMD.ExecuteReader(Data.CommandBehavior.SequentialAccess)
Do While myReader.Read()
' Get the publisher id, which must occur before getting the logo.
pub_id = myReader.GetString(0)
' Create a file to hold the output.
fs = New IO.FileStream("logo" & pub_id & ".bmp", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
bw = New IO.BinaryWriter(fs)
' Reset the starting byte for a new BLOB.
startIndex = 0
' Read bytes into outbyte() and retain the number of bytes returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
' Continue reading and writing while there are bytes beyond the size of the buffer.
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
' Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Loop
' Write the remaining buffer.
bw.Write(outbyte, 0, retval - 1)
bw.Flush()
' Close the output file.
bw.Close()
fs.Close()
Loop
' Close the reader and the connection.
myReader.Close()
pubsConn.Close()