views:

41

answers:

1

I'm trying to do the same thing as in this question, but am having problems.

Can you share with me how you prepared your objects prior to inserting and updating the database?

I have a List<myObject> that I want to store as a VarBinary(max) in SQL Server 2008.

A: 

You will need to convert your List<object> to a byte[]. You don't need Linq specifically but I assume you are using Linq To Sql? Before storing your object in the property that should contain the binary you need to convert it to a byte[]. Here is some example code to do what you need (Source Link):

// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
    if(obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);
    return ms.ToArray();
}

// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
    MemoryStream memStream = new MemoryStream();
    BinaryFormatter binForm = new BinaryFormatter();
    memStream.Write(arrBytes, 0, arrBytes.Length);
    memStream.Seek(0, SeekOrigin.Begin);
    Object obj = (Object) binForm.Deserialize(memStream);
    return obj;
}
Kelsey
@ryan : this is a great answer! Consider 'accepting' this answer with the checkmark.
p.campbell