我有这段代码,它从SQLite恢复消息队列(序列化)
public void Restore()
{
try
{
const string databaseName = @"C:\Code\C#\WcfService\WcfService\mainDB.db3";
SQLiteConnection connection = new SQLiteConnection(string.Format("Data Source={0};", databaseName));
connection.Open();
try
{
SQLiteCommand command = new SQLiteCommand("SELECT * FROM dump ORDER BY DId DESC limit 1", connection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var info = (byte[])reader["DBinaryData"];
Queue<Message> deserializedData = GetDeserializedMessages(info);
var data = MergeQueueMessage(deserializedData);
Logger.Log(data.ToString());
}
}
finally
{
connection.Close();
}
}
catch (Exception e)
{
Logger.Log(e.Message);
}
}
public Queue<Message> GetDeserializedMessages(byte[] source)
{
Queue<Message> messages = null;
using (MemoryStream memoryStream = new MemoryStream(source))
{
BinaryFormatter formatter = new BinaryFormatter();
messages = (Queue<Message>)formatter.Deserialize(memoryStream);
}
return messages;
}
但是我遇到了一个问题:无法从字段“DBinaryData”反序列化信息; 我在DB中的表包含:
答案 0 :(得分:0)
尝试:
reader["DBinaryData"].ToArray();
将二进制数据序列化为byte []。
即
byte[] info = reader["DBinaryData"].ToArray();