-- 如果要將圖片數據存入SQL Server數據庫的表中,我們必須使用SQL Server的image數據類型。
在很多時候,我們需要將圖片文件存入到SQL Server數據庫中,並且在使用的時候將數據庫中的圖片取出。本文將描述用C#語言來實現這一過程。
數據庫表結構
如果要將圖片數據存入SQL Server數據庫的表中,我們必須使用SQL Server的image數據類型,在被試驗中,我們將使用如下的語句創建數據庫表StudentInfo:
CREATE TABLE [dbo].[StudentInfo]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Age] [int] NULL,
[Sex] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[Class] [varchar](15) COLLATE Chinese_PRC_CI_AS NULL,
[Hobby] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[Picture] [image] NULL
)
其中字段Picture字段為image數據類型,用來保存學生的照片。
圖片存入數據庫
要將圖片數據存入到數據庫表的image數據類型的字段中,首先需要將圖片文件中的數據讀入到內存字節中,在將內存字節存入數據庫中,具體示例代碼如下:
private void btnUpload_Click(object sender, EventArgs e) { //上傳圖片到數據庫 OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Filter = "圖片文件(*.jpg)|*.jpg"; string filePath = ""; if (openDlg.ShowDialog() == DialogResult.OK) { filePath = openDlg.FileName; this.txtFilePath.Text = filePath; this.picShow.ImageLocation = filePath; //打開文件流,用來讀取圖片文件中的數據 FileStream stream = new FileStream(filePath,FileMode.Open,FileAccess.Read); //將文件流中的數據存入內存字節組中 byte[] buffer = new byte[stream.Length]; stream.Read(buffer,0,(int)stream.Length); stream.Close(); try { //調用存儲圖片數據的存取過程 string strName = Path.GetFileName(filePath); string connString = "Data Source=.;Initial Catalog=StuDB;Persist Security Info=True"; SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand cmd = new SqlCommand("proc_UploadPicture", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1; cmd.Parameters.Add("@Picture", SqlDbType.Image).Value = buffer; cmd.Parameters.Add("@Ext", SqlDbType.VarChar).Value = strName; cmd.ExecuteNonQuery(); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } 存儲過程proc_UploadPicture代碼如下: Create procedure [dbo].[proc_UploadPicture] @ID int, @Picture image AS update StudentInfo set Picture = @Picture where ID = @ID 從數據庫讀取圖片 要從數據庫中獲取圖片數據,並將圖片顯示在界面上,需要將數據庫中的圖片數據讀入到內存中,在將內存的數據用位圖來格式化,並將位圖顯示在界面的PictureBox控件中。具體的實例代碼如下: private void btnDownload_Click(object sender, EventArgs e) { //將數據庫中的圖片顯示出來 try { byte[] imageBytes; string connString = "Data Source=.;Initial Catalog=StuDB;Persist Security Info=True"; SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand cmd = new SqlCommand("proc_DownloadPicture", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1; SqlDataReader dataReader = cmd.ExecuteReader(); if (dataReader.Read()) { //獲取圖片數據 imageBytes = (byte[])dataReader["Picture"]; //將內存流格式化為位圖 MemoryStream stream = new MemoryStream(imageBytes); Bitmap bmap = new Bitmap(stream); stream.Close(); //將位圖顯示在界面的PictureBox控件中 this.picShow.Image = bmap; } dataReader.Close(); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
存儲過程proc_DownloadPicture代碼如下:
Create procedure [dbo].[proc_DownloadPicture]
@ID int
as
select Picture
from StudentInfo
where ID = @ID