Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deserializing an Image from SQL Server field

Status
Not open for further replies.

JerryKreischer

Programmer
Jul 31, 2003
20
US
Here's a snippet of code I use in my program which serializes a custom collection I've created (Chk) and store in a table as an image...
Code:
Dim ms As New MemoryStream()     ' Create a memory stream...

Dim bf As New BinaryFormatter()     ' and a formatter.

bf.Serialize(ms, Chk)                      ' Serialize the object into the stream.



Dim myConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

myConn.Open()

Dim myCommand As SqlCommand = New SqlCommand("spInsertOrder", myConn)

With myCommand

     .CommandType = CommandType.StoredProcedure

     .Parameters.Add("@OrderDetails", SqlDbType.Image).Value = ms.ToArray()

     .Parameters.Add("@OrderDetailsLen", SqlDbType.Int).Value = ms.Length

End With

The key for me to get this to work was the ms.ToArray()

Now....
How can I DESERIALIZE this back into my collection (Chk)???
I've seen some C# examples, but I code in VB...

Thanx all...
Jerry
 
Has anyone got a solution to this? I'm working on the same thing. I'll post back when I solve it, but if anyone knows it would be a great timesaver. Thanks!
 
'This assumes you have done the query and have a datareader with the image in it. I haven't tested this
'mDataReader is a SqlDataReader.

Dim Length As Integer = 100000
Dim img(Length) As Byte
Dim FileSize As Long
Dim FieldOffset As Long = 0
Dim BufferOffset As Integer = 0
dim indexOfField as integer = 1

mDataReader.GetBytes(indexOfField, FieldOffset, img, BufferOffset, Length)

Dim ms As System.IO.MemoryStream = New
System.IO.MemoryStream(_mg)

'Return the Bitmap
Return New Bitmap(ms)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top