Mssql image into MemoryStream code not working...

C

Chris Bond

Hi,

I've got the following code that works perfectly, all it does is grab a
image (binary file) from a mssql db and writes it out to the filesystem. I
then use a commercial object that reads the file in, the commercial object
also supports as LoadMessageFromStream(stream as System.IO.Stream) and i'd
like to use this instead of writing the file out to the file system.

Anychance of anybody converting the code so it produces a memory stream
output instead of a physical file on the filesystem. If theres a faster way
to do this all the better this is code that works though at the moment
below, i will change the hard coded sql statements to use a stored proc at
some stage but thats easy enough to do later. As i've tried a million
things and cant get the code to work:

Private Sub GetRawMessage(ByVal msgId As Integer)
Dim sql As String = "SELECT MsgRaw FROM tblMessages Where MsgId = "
& msgId
Dim conn As OleDbConnection = New OleDbConnection
conn.ConnectionString =
ConfigurationSettings.AppSettings("strConnOle")
If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As OleDbDataReader = _
cmd.ExecuteReader(CommandBehavior.SequentialAccess)
reader.Read()

Dim strFile As String = Server.MapPath("/mysolution/temp/") & msgId
& ".eml"
fs = New FileStream(strFile, FileMode.OpenOrCreate,
FileAccess.Write)

bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
bw.Close()
fs.Close()
reader.Close()
If conn.State = ConnectionState.Open Then
conn.Close()
conn.Dispose()
End If
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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top