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.

1
  • welcome to Stack Overflow. If one of the answers below helped answer you question you should mark it as the accepted answer. Commented Aug 19, 2010 at 4:55

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.