-1

我有这段代码,它从 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”字段的信息;我在数据库中的表包含:

  1. DId(整数,主键)
  2. DTime(文本)
  3. DBinaryData (Blob) // 将消息队列转储为序列化对象
4

1 回答 1

0

尝试:

reader["DBinaryData"].ToArray();

将您的二进制数据序列化为字节[]。

IE

byte[] info = reader["DBinaryData"].ToArray();
于 2013-04-12T08:23:21.220 回答