2017-06-14 4 views
0

シングルC#のバイトにSqlCommandオブジェクトの列(複数行)を読む[]配列

ID Content  ByteLength Sequence 
1 Part1... 945669  1 
1 Part2... 945669  2 
1 Part3... 945669  3 

... 

2 Part1... 45234  1 
2 Part2... 45234  2 

:私はDocument.Contentのすべてを読むことができますどのように

Document.Content = Up to 32KB of data 
Document.ByteLength = Total data in bytes for the Document ID 
Document.Sequence = Order of content in the Document ID 

1バイトの配列にbyte[] Content

using (var command = new SqlCommand("SELECT Content FROM Document WHERE ID=1 ORDER BY Sequence", connection)) 
    using (var reader = command.ExecuteReader()) 
    { 
     while(reader.Read()) 
     { 
      // What goes here? 

      // if we just want one row: 
      //var fileBytes = (byte[])reader.GetValue(0); // read the file data from the selected row (first column in above query) 
     } 
    } 
+0

バイナリファイルに追加モードで書き込むことができます。 [コンテンツ]フィールドのデータ型は? – Steve

+0

最初の3つのシーケンスは1,2,3でない場合はすべて1です。 –

+0

@Steve現在varchar(最大)。これは主にRTFとtxtの内容を保持します。 – Adam

答えて

2

このコンテンツフィールドにテキストデータが含まれているという事実を考えると、あなたは単純にすべての、あなたは今すぐ

using (var command = new SqlCommand("SELECT Content FROM Document WHERE ID=1 ORDER BY Sequence", connection)) 
using (var reader = command.ExecuteReader()) 
{ 
    // Set a large enough initial capacity 
    StringBuilder sb = new StringBuilder(32767); 
    while(reader.Read()) 
    { 
     sb.Append(reader.GetString(0)); 
    } 
} 

コンテンツフィールドを読みながらループ出口で、データを追加するためのStringBuilderを使用することができます内容はStringBuilderバッファにあり、バイト配列に戻すことができます。

byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString()); 
+0

現在のコードにはリーダーからの(byte [])キャストがあるので、それはvar/binaryでなければならないと思います。 –

+0

上記のコメントに記載されているOPはContentがvarchar(max)フィールドです – Steve

関連する問題