无法反序列化

时间:2013-04-12 08:19:39

标签: c# types type-conversion

我有这段代码,它从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中的表包含:

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

1 个答案:

答案 0 :(得分:0)

尝试:

reader["DBinaryData"].ToArray();

将二进制数据序列化为byte []。

byte[] info = reader["DBinaryData"].ToArray();