C#中图片存入SQL数据库

  //将SQL server2000中保存的图像显示在Picture中
  private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   byte[] buffByte = null;
   string comm = @"select img from table1 where id = " + this.listBox1.SelectedValue ;
   this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
   this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
   this.sqlCommand1.CommandText = comm;
   this.sqlCommand1.Connection = this.sqlConnection1 ;
   this.sqlConnection1.Open();
   System.Data.SqlClient.SqlDataReader rd = this.sqlCommand1.ExecuteReader();
   while (rd.Read())
   {
    buffByte = ((byte[])rd[0]);
   }
   rd.Close();
   this.sqlConnection1.Close();
   //将图像的字节数组放入内存流
   System.IO.MemoryStream ms = new System.IO.MemoryStream(buffByte);
   //通过流对象建立Bitmap
   System.Drawing.Bitmap bmp = new Bitmap(ms);
   this.pictureBox1.Image = bmp;
  }
  
  //将图像保存到SQL server2000的Image字段中
  private void button2_Click_1(object sender, System.EventArgs e)
  {
   string pathName;
   if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
   {
    pathName = this.openFileDialog1.FileName;
    System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);
    this.pictureBox1.Image = img;
    
    //将图像读入到字节数组
    System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
    byte[] buffByte = new byte[fs.Length];
    fs.Read(buffByte,0,(int)fs.Length);
    fs.Close();
    fs = null;
    
    //建立Command命令
    string comm = @"Insert into table1(img,name) values(@img,@name)";
    this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();
    this.sqlCommand1.CommandType = System.Data.CommandType.Text ;
    this.sqlCommand1.CommandText = comm;
    this.sqlCommand1.Connection = this.sqlConnection1 ;
    //创建Parameter
    this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);
    this.sqlCommand1.Parameters[0].Value = buffByte;
    this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
    this.sqlCommand1.Parameters[1].Value =pathName.Substring(pathName.LastIndexOf("
//")+1 );
    try
    {
     this.sqlConnection1.Open();
     this.sqlCommand1.ExecuteNonQuery();
     this.sqlConnection1.Close();
    }
    catch(System.Exception ee)
    {
     MessageBox.Show(ee.Message );
    }
    buffByte = null;

    this.FillListBox();
   }
  }

//----------------------------------------------------------------------------------------------------------

//把图片读到picturebox,再写入数据库  
   
  sqlconnection   conn=new   sqlconnection(@"data   source=chenyuming2004/vsdotnet;uid=sa;pwd=cym;database=lhf");  
  conn.open();  
  sqlcommand   cmd=new   sqlcommand("insert   into   fuser   values   (1a,1b,@i)",conn);  
  byte[]   ib=new   byte[60000];  
  filestream   fs=new   filestream(@"d:/windows   temp/temp/1.jpg",filemode.open   ,fileaccess.read   );  
  fs.read(ib,0,60000);  
  cmd.parameters.add("@i",sqldbtype.image,(int)fs.length);  
  cmd.parameters["@i"].value=ib;  
  cmd.executenonquery();  
  conn.close();  
   
  -------------------------------------------------------  
  //从数据库读图片到picturebox  
   
  sqlconnection   conn=new   sqlconnection(@"data   source=chenyuming2004/vsdotnet;uid=sa;pwd=cym;database=lhf");  
  conn.open();  
  sqlcommand   cmd=new   sqlcommand("select   照片   from   fuser   where   password=1b",conn);  
  sqldatareader   reader=cmd.executereader();  
  reader.read();  
  memorystream   buf=new   memorystream((byte[])reader[0]);  
  image   image=image.fromstream(buf,true);  
  picturebox1.image=image;


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值