1、以下方法将一个对象序列化为可以在数据库中存储的字节数组
private byte[] getByteData(SupplementData data)
{
BinaryFormatter serializer =new BinaryFormatter();
MemoryStream memStream =new MemoryStream();
serializer.Serialize(memStream,data);
int length = (int)memStream.Length;
byte[] buffer = new byte[length];
memStream.Position = 0;
memStream.Read(buffer,0,length);
return buffer;
}
调用方法得到字节数组后给参数赋值:
oCommand.Parameters[5].Value =getByteData(OldData);
2、以上是关于序列化后的字节数组的存储,那读取呢?
private byte[] getByteData(SupplementData data)
{
BinaryFormatter serializer =new BinaryFormatter();
MemoryStream memStream =new MemoryStream();
serializer.Serialize(memStream,data);
int length = (int)memStream.Length;
byte[] buffer = new byte[length];
memStream.Position = 0;
memStream.Read(buffer,0,length);
return buffer;
}
调用方法得到字节数组后给参数赋值:
oCommand.Parameters[5].Value =getByteData(OldData);
2、以上是关于序列化后的字节数组的存储,那读取呢?
private SupplementData GetHistoryData(string Track_UUID)
{
//根据ID得到一笔记录,GetTable是本人系统中的特定方法
DataTable table = GetTable("LIC_DATA_TRACK","TRACK_UUID",Track_UUID,out sErrorMsg);
BinaryFormatter deserializer =new BinaryFormatter();
MemoryStream stream =new MemoryStream();
//将得到的记录中的某个Bolb类型的数据读出来存储到数据流中
byte[] objBytes =(byte[])table.Rows[0]["HistoryData"];
stream.Write(objBytes,0,objBytes.Length);
stream.Position = 0;
//字节流反序列化后,返回对象SupplementData(本人定义的一个对象)
return (SupplementData)deserializer.Deserialize(stream);
}