示例
- 使用以下結建構立一個 SQL Server 或 Access 表:
CREATE TABLE BLOBTest ( BLOBID INT IDENTITY NOT NULL, BLOBData IMAGE NOT NULL )
- 打開 Visual Studio .NET,然後建立一個 Visual C# Windows 應用程式項目。
- 從工具箱向預設的 Form1 添加一個 PictureBox 和兩個 Button 控件。将 Button1 的 Text 屬性設定為 File to Database,并将 Button2 的 Text 屬性設定為 Database to PictureBox。
- 在窗體的代碼子產品頂部插入 using 語句:
using System.Data.SqlClient; using System.IO; using System.Drawing.Imaging;
- 将以下資料庫連接配接字元串的聲明添加到 public class Form1 :System.Windows.Forms.Form 類聲明中,并根據需要調整連接配接字元串:
String strCn = "Data Source=localhost;integrated security=sspi;initial catalog=mydata";
- 将下面的代碼插入 Button1 (File to Database) 的 Click 事件過程中。根據需要調整到一個可用示例圖像檔案的可用路徑。此代碼可将圖像檔案從磁盤(使用一個 FileStream 對象)讀入 Byte 數組,然後使用一個參數化的 Command 對象将資料插入資料庫。
try { SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn); String strBLOBFilePath = @"C:\blue hills.jpg";//Modify this path as needed. //Read jpg into file stream, and from there into Byte array. FileStream fsBLOBFile = new FileStream(strBLOBFilePath,FileMode.Open, FileAccess.Read); Byte[] bytBLOBData = new Byte[fsBLOBFile.Length]; fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length); fsBLOBFile.Close(); //Create parameter for insert command and add to SqlCommand object. SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); //Open connection, execute query, and close connection. cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }catch(Exception ex) {MessageBox.Show(ex.Message);}
- 将下面的代碼插入 Button2 (Database to PictureBox) 的 Click 事件過程。此代碼将行從資料庫中的 BLOBTest 表檢索到一個資料集,複制最新添加的圖像到 Byte 數組,然後到 MemoryStream 對象,接着将 MemoryStream 加載到PictureBox 控件的 Image 屬性。
try { SqlConnection cn = new SqlConnection(strCn); cn.Open(); //Retrieve BLOB from database into DataSet. SqlCommand cmd = new SqlCommand("SELECT BLOBID, BLOBData FROM BLOBTest ORDER BY BLOBID", cn); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "BLOBTest"); int c = ds.Tables["BLOBTest"].Rows.Count; if(c>0) { //BLOB is read into Byte array, then used to construct MemoryStream, //then passed to PictureBox. Byte[] byteBLOBData = new Byte[0]; byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]); MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); pictureBox1.Image= Image.FromStream(stmBLOBData); } cn.Close(); } catch(Exception ex) {MessageBox.Show(ex.Message);}
- 按 F5 鍵編譯并運作該項目。
- 單擊 File to Database 按鈕将至少一個示例圖像加載到資料庫。
- 單擊 Database to PictureBox 按鈕将儲存的圖像顯示在 PictureBox 控件中。
- 如果想能夠直接将圖像從 PictureBox 控件插入資料庫,則請添加第三個 Button 控件,并将下面的代碼插入其 Click 事件過程。此代碼将圖像資料從 PictureBox 控件檢索到 MemoryStream 對象,将 MemoryStream 複制到一個 Byte 數組,然後使用一個參數化的 Command 對象将 Byte 數組儲存到資料庫。
try { SqlConnection cn = new SqlConnection(strCn); SqlCommand cmd = new SqlCommand("INSERT INTO BLOBTest (BLOBData) VALUES (@BLOBData)", cn); //Save image from PictureBox into MemoryStream object. MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); //Read from MemoryStream into Byte array. Byte[] bytBLOBData = new Byte[ms.Length]; ms.Position = 0; ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length)); //Create parameter for insert statement that contains image. SqlParameter prm = new SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false, 0, 0,null, DataRowVersion.Current, bytBLOBData); cmd.Parameters.Add(prm); cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); }catch(Exception ex) {MessageBox.Show(ex.Message);}
- 運作該項目。單擊 Database to PictureBox 按鈕以顯示剛才在 PictureBox 控件中儲存過的圖像。單擊新添加的按鈕将此圖像從 PictureBox 儲存到資料庫。然後再次單擊 Database to PictureBox 按鈕以确認圖像已正确儲存。
http://support.microsoft.com/kb/317701/zh-cn
文中講得十分的詳細。