Reading BLOB

R

Raymond Lewallen

Here is my example using SqlClient... shouldn't be too different from what
you need. This example write the blob out to a file.

Public Sub Load()
Dim dr As Data.SqlClient.SqlDataReader
Dim ParamList(0) As SqlParameter
Dim index As Int32

Dim fs As FileStream
Dim bw As BinaryWriter

Dim intBufferSize As Integer = 100
Dim aryBLOB(intBufferSize - 1) As Byte
Dim lngBLOB As Long
Dim lngStartIndex As Long = 0
Dim strBinaryFile As String

If Right(m_strDocumentLocation, 1) = "\" Then
strBinaryFile = m_strDocumentLocation & m_strDocumentName
Else
strBinaryFile = m_strDocumentLocation & "\" & m_strDocumentName
End If

Try
ParamList(0) = SQLHelper.MakeInParam("@DocumentTypeID",
SqlDbType.Int, 4, 434)

dr = SQLHelper.GetDataReader(CommandType.StoredProcedure,
"DocumentLoad", ParamList)

If dr.Read Then
fs = New FileStream(strBinaryFile, FileMode.OpenOrCreate,
FileAccess.Write)
bw = New BinaryWriter(fs)
lngStartIndex = 0
lngBLOB = dr.GetBytes(dr.GetOrdinal("document"),
lngStartIndex, aryBLOB, 0, intBufferSize)

Do While lngBLOB = intBufferSize
bw.Write(aryBLOB)
bw.Flush()
lngStartIndex += intBufferSize
lngBLOB = dr.GetBytes(dr.GetOrdinal("document"),
lngStartIndex, aryBLOB, 0, intBufferSize)
Loop

bw.Write(aryBLOB)
bw.Flush()
bw.Close()
fs.Close()
bw = Nothing
fs = Nothing
End If
Finally
If Not dr Is Nothing Then
dr.Close()
dr = Nothing
End If
SQLHelper.Dispose()
End Try
End Sub
 

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