Search code examples
c#.netsqlvarbinarymax

Convert C# Object to and from Varbinary using Linq to SQL


I want to be able to save any C# object in a single column of a SQL database table. I am not clear how to convert the object into a varbinary or get it back from a varbinary. My SystemContextObjects table has an OptionValue column that is Varbinary(max).

var dc1 = new DataContextDataContext();
var optionUpdate = dc1.SystemContextObjects.SingleOrDefault(o => o.OptionId == OptionId && o.OptionKey == OptionKey);
if (optionUpdate != null)
{
    optionUpdate.OptionValue = Value;  <===== NEED HELP HERE...
    optionUpdate.DateCreated = DateTime.Now;
    optionUpdate.PageName = PageName;
    var ChangeSet = dc1.GetChangeSet();
    if (ChangeSet.Updates.Count > 0)
    {
        dc1.SubmitChanges();
        return;
    }
}

Solution

  • I wound up using JSON to accomplish this. I serialize/deserialize the class to/from a string and store that. Works fine.