Friday, July 22, 2005

How to serialize an object to a datacolumn in a dataset

The image column is represented as byte[] in a dataset, so all you have to do is, assign and read the value of the column.

I looked for an example around, but could not find exactly same thing.

So I had to write it my own.

If you have a similar challenge, here is the code to "leverage":

public static byte[] SerializeObject(object obj)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter f = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream() )
{
f.Serialize(ms, obj);
return ms.GetBuffer();
}
}
public static object DeSerializeObject(byte[] buffer)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter f = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer) )
{
return f.Deserialize(ms);
}
}

0 Comments:

Post a Comment

<< Home