-1

I have this code, which restore message queue (serialized) from 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;
    }

But i have a problem: can't deserialize information from field "DBinaryData"; My table in DB contain:

  1. DId (integer, primary key)
  2. DTime (Text)
  3. DBinaryData (Blob) // dump of message queue as serialized object
Relrin
  • 760
  • 2
  • 10
  • 28

1 Answers1

0

Try:

reader["DBinaryData"].ToArray();

to serialize your binary data into a byte[].

i.e.

byte[] info = reader["DBinaryData"].ToArray();
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
  • I get information from reader["DBinaryData"] as Object. Object don't know something about ToArray() method. – Relrin Apr 12 '13 at 08:27
  • I think this is your problem and what you're trying to achieve: http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream – Chris Dixon Apr 12 '13 at 08:28
  • but how to use that with reader["DBinaryData"]? – Relrin Apr 12 '13 at 08:41
  • Use it in GetDeserializedMessages() - it's about the same code as the example. – Chris Dixon Apr 12 '13 at 08:43
  • im try as Queue deserializedData = GetDeserializedMessages((byte[])reader["DBinaryData"]) but get exception: end of Stream encountered before parsing was completed – Relrin Apr 12 '13 at 08:52