2013-04-12 42 views
-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」反序列化信息; 我在DB表包含:

  1. DID(整數,主鍵)
  2. DTIME(文本)
  3. DBinaryData(BLOB)//信息隊列作爲序列化對象

回答

0

菜系轉儲:

reader["DBinaryData"].ToArray(); 

將您的二進制數據序列化爲byte []。

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

我從讀取器[ 「DBinaryData」]作爲對象的信息。對象不知道ToArray()方法。 – Relrin

+0

我認爲這是你的問題,你試圖達到什麼目的:http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream –

+0

但如何與讀者一起使用[ 「DBinaryData」]? – Relrin