我有这段代码,它从 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”字段的信息;我在数据库中的表包含:
- DId(整数,主键)
- DTime(文本)
- DBinaryData (Blob) // 将消息队列转储为序列化对象