在SQL Server数据库实现图片文件的存取

-- 如果要将图片数据存入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

 

posted @ 2017-07-07 17:04  是什么让人懒惰  阅读(5286)  评论(0编辑  收藏  举报