国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#保存圖片到數(shù)據(jù)庫(kù)并讀取顯示圖片的方法

2020-01-24 03:24:49
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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 = //www.survivalescaperooms.com/ianakin/archive/2012/02/02/buffByte;

this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);

this.sqlCommand1.Parameters[1].Value =//www.survivalescaperooms.com/ianakin/archive/2012/02/02/pathName.Substring(pathName.LastIndexOf("http://")+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ù)庫(kù)讀圖片到picturebox

復(fù)制代碼 代碼如下:

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;

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 上饶市| 兴海县| 连云港市| 安多县| 广南县| 乌鲁木齐县| 邯郸县| 体育| 邹城市| 天柱县| 通江县| 临猗县| 图木舒克市| 和龙市| 顺义区| 清苑县| 疏附县| 新竹市| 北票市| 息烽县| 浮梁县| 交口县| 威信县| 上杭县| 屏东县| 青浦区| 安新县| 济宁市| 措美县| 兴城市| 丰镇市| 龙州县| 攀枝花市| 图们市| 平顺县| 天津市| 若尔盖县| 蒙城县| 台南市| 忻州市| 兴化市|