C#保存圖片到數(shù)據(jù)庫并讀取顯示圖片的方法
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;
//將圖像讀入到字節(jié)數(shù)組
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 ;
//創(chuàng)建Parameter
this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);
this.sqlCommand1.Parameters[0].Value = http://www.dhdzp.com/ianakin/archive/2012/02/02/buffByte;
this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);
this.sqlCommand1.Parameters[1].Value =http://www.dhdzp.com/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();
}
讀取:
從數(shù)據(jù)庫讀圖片到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;
相關(guān)文章
C# 獲取打印機(jī)當(dāng)前狀態(tài)的方法
C# 獲取打印機(jī)當(dāng)前狀態(tài)的方法,需要的朋友可以參考一下2013-04-04
c#求范圍內(nèi)素?cái)?shù)的示例分享(c#求素?cái)?shù))
問題是判斷101-200之間有多少個(gè)素?cái)?shù),并輸出所有素?cái)?shù)。下面是使用C#解決這個(gè)問題的方法 ,需要的朋友可以參考下2014-03-03
C#winform中數(shù)據(jù)庫綁定DataGrid的實(shí)現(xiàn)
本文主要介紹了C#winform中數(shù)據(jù)庫綁定DataGrid的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

