I am writting an application in VB using VS2008 and SQL2005. I would like to save an array to my database and later retreive it. I understand that I need to serialize the array to do this but don't understand the process. I have looked at various resources on the Internet but seem to be missing something.

Would some one please provide me the code needed to serialize my array and then save it to my db. I am using stored procedures with paramaters to update my database.

Thanks.
Charles

Recommended Answers

All 2 Replies

>I need to serialize the array to do this but don't understand the process.

Dim no As Integer() = {11, 22, 33, 44}
        Dim MS As New System.IO.MemoryStream
        Dim bin As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        bin.Serialize(MS, no)

        Dim bytes(MS.Length - 1) As Byte
        MS.Position = 0
        MS.Read(bytes, 0, bytes.Length)

        ....
        ...
        cmd.ComandText="insert into TableName values (@data)"
        cmd.Parameters.Add("@data", SqlDbType.Image, bytes.Length).Value = bytes
       ...

This is an excellant snippet which is really helpful, thanks. My biggest issue is not having worked with serialization. I kept getting lost at the point where , it appears, you store the serialized variable (bytes in your example) in the database.

Thanks
Charles

>I need to serialize the array to do this but don't understand the process.

Dim no As Integer() = {11, 22, 33, 44}
        Dim MS As New System.IO.MemoryStream
        Dim bin As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        bin.Serialize(MS, no)

        Dim bytes(MS.Length - 1) As Byte
        MS.Position = 0
        MS.Read(bytes, 0, bytes.Length)

        ....
        ...
        cmd.ComandText="insert into TableName values (@data)"
        cmd.Parameters.Add("@data", SqlDbType.Image, bytes.Length).Value = bytes
       ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.