文章編號: 309158 - 上次校閱: 2004年7月14日 - 版次: 4.3

如何讀取及寫入 BLOB 資料使用 ADO.NET 使用 Visual C#.NET

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

GetChunkAppendChunk 方法沒有在 ADO.NET 中可用的 DataReader 的資料行、 資料集 資料行或 命令 參數。本文將告訴您,如何使用 Visual C#.NET 來讀取和寫入二進位大型物件 (BLOB) 欄位。

需求

下列清單列出建議的硬體、 軟體、 網路基礎結構及所需的 Service Pack:
  • Microsoft Windows 2000 專業版、 Windows 2000 Server Windows 2000 進階伺服器或 Windows NT 4.0 伺服器
  • Microsoft Visual Studio.NET
  • Microsoft SQL Server

建立專案

  1. 新增到您的 SQL Server 北風 資料庫命名 MyImages 表格。在您的資料表中加入下列欄位:
    • 名為 「 識別碼 」 的型別 Int 識別欄位。
    • 名為 「 說明 」 型別 VarChar 的長度為 50 的欄位。
    • 名為"ImgField"的型別 影像 的欄位。

  2. 啟動 Visual 的 Studio.NET,然後建立新的 Visual C# Windows 應用程式專案。
  3. 從工具箱拖曳兩個 按鈕 控制項加入預設表單 Form1。
  4. 在 [屬性] 視窗中 儲存至資料庫 (從檔案),將 Button1] 的 [文字] 屬性變更,然後再變更 [儲存檔案 (從資料庫) 的 [Button2] 的 [文字] 屬性。
  5. 將下列程式碼加入至程式碼] 視窗的頂端:
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    					
  6. 連按兩下 [Button1,然後將下列的程式碼加入至 Button1_Click 事件處理常式。

    附註Uid < 使用者名稱 > 必須具有權限,才能執行這些作業在資料庫上。
    {
    SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
    SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
    SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    DataSet ds = new DataSet("MyImages");
    
    da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    FileStream fs = new FileStream(@"C:\winnt\Gone Fishing.BMP", FileMode.OpenOrCreate, FileAccess.Read);
    			
    byte[] MyData= new byte[fs.Length];
    fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
    			
    fs.Close();
    			
    da.Fill(ds,"MyImages");
    				
    DataRow myRow;
    myRow=ds.Tables["MyImages"].NewRow();
    
    myRow["Description"] = "This would be description text";
    myRow["imgField"] = MyData;
    ds.Tables["MyImages"].Rows.Add(myRow);
    da.Update(ds, "MyImages");
    
    con.Close();
    		
    }
    					
  7. 連按兩下 Button2,然後將下列的程式碼加入至 Button2_Click 事件處理常式。

    附註Uid < 使用者名稱 > 必須具有權限,才能執行這些作業在資料庫上。
    {
    SqlConnection con = new SqlConnection("Server=Darkover;uid=<username>;pwd=<strong password>;database=northwind");
    SqlDataAdapter da = new SqlDataAdapter("Select * From MyImages", con);
    SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
    DataSet ds = new DataSet("MyImages");
    
    byte[] MyData= new byte[0];
    			
    da.Fill(ds, "MyImages");
    DataRow myRow;
    myRow=ds.Tables["MyImages"].Rows[0];
               
    MyData =  (byte[])myRow["imgField"];
    int ArraySize = new int();
    ArraySize = MyData.GetUpperBound(0); 
    
    FileStream fs = new FileStream(@"C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, FileAccess.Write);
    fs.Write(MyData, 0,ArraySize);
    fs.Close();
    }
    					
  8. 請按 F5 編譯並執行應用程式。
  9. 按一下 [儲存] 以從檔案]) 的資料庫 影像 C:\WinNT\Gone Fishing.bmp 載入 SQL Server 影像] 欄位。
  10. 按一下 [儲存] (從資料庫) 的檔案 儲存到檔案從 SQL Server 影像 欄位資料。

這篇文章中的資訊適用於:
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual C# .NET 2003 標準版
  • Microsoft Visual C# .NET 2002 Standard Edition
關鍵字:?
kbmt kbhowtomaster kbio kbsqlclient kbsystemdata KB309158 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:309158? (http://support.microsoft.com/kb/309158/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。