2013-03-30 57 views
1

我只需要做反向轉換爲this question曝光。如何將內存流轉換爲System.Data.Linq.Binary?

我有一個MemoryStream並希望將它保存在我的SQL CE數據庫的System.Data.Linq程序二進制領域(僅供參考我使用EF代碼優先)。

將MemoryStream實際上是一個XML,這比一個字符串字段的最大尺寸,所以我發現沒有其他的方法比在二進制存儲它(關於這個問題的建議真正理解)。

我的代碼(適於)

private Stream _userLayout; 
    _userLayout = new MemoryStream(); 
    DXGridControl_Table.SaveLayoutToStream(_userLayout); 
    MyDatabse.SomeTable.SomeBinaryField = _userLayout.???? 

回答

3

首先是

 byte[] buffer = new byte[LENGTH]; 
     MemoryStream memoryStream = new MemoryStream(buffer); 

在您的例子,你可以使用

 DXGridControl_Table.SaveLayoutToStream(_userLayout); 
     byte[] doSomethingwithyourData = _userLayout.GetBuffer(); 
     var length = _userLayout.Length; 

有了這些信息,你可以寫二進制數據什麼的。

注意,緩衝區包含分配的字節,這可能是未使用的。 例如,如果將字符串「test」寫入到MemoryStream 對象中,則從GetBuffer返回的緩衝區的長度爲256,而不是 4,未使用252個字節。要僅獲取緩衝區中的數據,請使用ToArray方法 ;但是,ToArray會在內存中創建數據的副本。

或者

Binary binary = new Binary(_userLayout.ToArray()); 

像對方回答說,有二進制的隱式轉換:

public static implicit operator Binary(byte[] value) { 
    return new Binary(value); 
} 

您請求的例子。 ,使用方法的一個小例子:

namespace Stackoverflow.Hannish.SaveLayout 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.IO; 
    using System.Text; 
    using System.Windows.Forms; 

    public partial class Form1 : Form 
    { 
     /// <summary> 
     /// Here we store the layout data as a string. This is the data, that 
     /// gets saved to disk/database/etc. 
     /// </summary> 
     private string layoutdata = string.Empty; 

     public Form1() 
     { 
      this.InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      // Just some FooBar data. 
      var data = new List<DataValue> 
          { 
           new DataValue { Id = 1, Name = "Xyz", IsCool = true }, 
           new DataValue { Id = 2, Name = "Abc", IsCool = false } 
          }; 

      this.gridControl1.DataSource = data; 
     } 

     private void bnLoadLayout_Click(object sender, EventArgs e) 
     { 
      using (var stream = new MemoryStream()) 
      { 
       var strdata = Encoding.Default.GetBytes(this.layoutdata); 
       stream.Write(strdata, 0, strdata.Length); 
       stream.Seek(0, SeekOrigin.Begin); 
       this.gridView1.RestoreLayoutFromStream(stream); 
      } 
     } 

     private void bnSaveLayout_Click(object sender, EventArgs e) 
     { 
      using (var stream = new MemoryStream()) 
      { 
       this.gridView1.SaveLayoutToStream(stream); 
       this.layoutdata = Encoding.Default.GetString(stream.ToArray()); 
      } 
     } 
    } 
} 

而一些字節到文件魔術:

private void bnLoadBinLayout_Click(object sender, EventArgs e) 
    { 
     using (FileStream fstream = File.Open("Layoutdata.bin", FileMode.Open)) 
     { 
      int length = (int)fstream.Length; 
      byte[] buffer = new byte[length]; 
      fstream.Read(buffer, 0, length); 

      var memstream = new MemoryStream(buffer); 
      this.gridView1.RestoreLayoutFromStream(memstream); 
     } 
    } 

    private void bnSaveBinLayout_Click(object sender, EventArgs e) 
    { 
     using (FileStream fstream = File.Create("Layoutdata.bin")) 
     { 
      var memstream = new MemoryStream(); 
      this.gridView1.SaveLayoutToStream(memstream); 
      fstream.Write(memstream.GetBuffer(), 0, (int)memstream.Length); 
     } 
    } 

...只是作爲例子。 DevExpress GridView可以使用SaveLayoutToXml()保存佈局本身;

+0

Jedzia,關愛給您的評論的一個例子」有了這些信息,你可以寫二進制數據。什麼?」。適用於這種情況下你可以看到,我不太習慣用流和二進制類型的工作,所以請容許我 – Hannish

+2

我補充說:‘關於使用一個小例子:’在上面的回答,真希望幫助:) – Jedzia

+0

鑑於偉大而徹底的例子,我認爲這是最好的答案,非常感謝你! – Hannish

4

MemorySteam類有ToArray()方法:

MemoryStream.ToArray Method

寫入流內容到一個字節數組,而不管位置屬性的。

此方法返回的MemoryStream的內容的副本作爲字節數組。如果當前實例是在提供的字節數組上構造的,則返回此實例有權訪問的數組部分的副本。有關詳細信息,請參閱MemoryStream構造函數。

這種方法時,MemoryStream的關閉工作。

隨着byte[]就可以輕鬆搞定Binary實例,因爲Byte[] to Binary implicit conversion可用:

MyDatabse.SomeTable.SomeBinaryField = (Binary) _userLayout.ToArray(); 
+0

謝謝馬爾欽抽出時間來回答。我必須在這裏丟失一些非常簡單的東西,但是當我鍵入「(Binary)_userLayout.ToArray();」或者像這樣投射((Binary)_userLayout).ToArray();我得到一個「無法鍵入System.IO.MemoryStream轉換爲System.Data.Linq.Binary。任何想法,爲什麼發生這種情況? – Hannish

1

給你的第二個問題和你的(可能的)來回轉換數據的概念錯誤。

將那個有趣的DevExpress XML保存在ntext數據列中。

NTEXT:與(2^30-2)/ 2(536870911)字符的最大長度可變長度Unicode數據。存儲大小(以字節爲單位)是輸入字符數的兩倍。

SQL Server Compact 4.0 Data Types

+0

謝謝Jedzia。你的回答提出了兩個問題...如何我是否定義了一個ntext字段w? ith代碼優先方法(字符串屬性被轉換爲數據庫中的nchar字段),鏈接中的註釋是什麼意思? 「字符串函數不再支持ntext」。謝謝 – Hannish

+0

事實上,這是對我來說似乎是最好的選擇,因爲我可以完全避免轉換到/從二進制轉換,因爲有趣的DevExpress組件也有一個「SaveLayoutToXML」方法,我假設我可以直接存儲在這樣的一個數據庫字段。 – Hannish

+0

請看看http://stackoverflow.com/questions/11365186/ntext-with-more-than-4000-characters-in-sql-server-ce-in-windows-phone?answertab=oldest#tab-頂部關於定義ntext字段(閱讀並檢查微軟KB的東西)。 – Jedzia

相關問題