C#保存图片到数据库并读取显示图片的方法
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 = //www.jb51.net/ianakin/archive/2012/02/02/buffByte;
this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
this.sqlCommand1.Parameters[1].Value =//www.jb51.net/ianakin/archive/2012/02/02/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=chenyuming2004VSdotNET;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;
您可能感兴趣的文章:
- C#保存图片到数据库并读取显示图片的方法
- C# 图片保存到数据库和从数据库读取图片并显示
- C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”
- 图片保存到数据库和从数据库读取图片并显示(c#)
- C#保存图片到数据库,读取图片显示
- c# 图片保存到数据库和从数据库读取图片并显示
- C#保存图片到数据库,读取图片显示
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 图片保存到数据库和从数据库读取图片并显示(c#)
- 图片保存到数据库和从数据库读取图片并显示(c#)
- Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示
- 将图片保存到ORACEL数据库和读取数据显示到页面
- C#从数据库中读取二进制数据,并显示图片
- 如何读取保存在数据库中的图片显示在DataGrid中
- 如何读取保存在数据库中的图片显示在DataGrid中
- Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示
- 如何将pictureBox里的图片保存到数据库,然后从数据读取显示
- Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示
- C# 用二进制将图片保存在数据库里再读出来显示在pictureBox中