欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法

 更新時(shí)間:2015年09月21日 18:04:46   作者:我心依舊  
這篇文章主要介紹了C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法,以實(shí)例形式較為詳細(xì)的分析了C#保存圖片到SQL Server數(shù)據(jù)庫(kù)的具體步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法。分享給大家供大家參考。具體如下:

第一步: 

//獲取當(dāng)前選擇的圖片
this.pictureBox1.Image = Image.FromStream(this.openFileDialog1.OpenFile());
//獲取當(dāng)前圖片的路徑
string path = openFileDialog1.FileName.ToString();
//將制定路徑的圖片添加到FileStream類(lèi)中    
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
//通過(guò)FileStream對(duì)象實(shí)例化BinaryReader對(duì)象
BinaryReader br = new BinaryReader(fs);
//通過(guò)BinaryReader類(lèi)對(duì)象的ReadBytes()方法將FileStream類(lèi)對(duì)象轉(zhuǎn)化為二進(jìn)制數(shù)組
byte[] imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));

第二步:

//將圖片添加到數(shù)據(jù)庫(kù)中
string sql="insert into pic values(@pic)"; 
SqlParameter[] param = new SqlParameter[] { new SqlParameter("@pic", imgBytesIn) };
DBHelper.GetExecuteQuery(sql, param);

第三步:

//將圖片從數(shù)據(jù)庫(kù)中取出
string sql="select * from pic where id=0";
SqlDataReader reader = DBHelper.GetExecuteReader(sql, null);
MemoryStream mss = null;
if (reader.Read())
{
  byte[] bytes = (byte[])reader["pic"];
  mss = new MemoryStream(bytes);
}
this.pictureBox2.Image = Image.FromStream(mss);

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論