如何把圖片保存到SQLServer數(shù)據(jù)庫?
this.pictureBox1.Image=Image.FromStream(this.openFileDialog1.OpenFile());
//獲取當(dāng)前圖片的路徑
stringpath=openFileDialog1.FileName.ToString();
//將制定路徑的圖片添加到FileStream類中
FileStreamfs=newFileStream(path,FileMode.Open,FileAccess.Read);
//通過FileStream對(duì)象實(shí)例化BinaryReader對(duì)象
BinaryReaderbr=newBinaryReader(fs);
//通過BinaryReader類對(duì)象的Readbytes()方法將FileStream類對(duì)象轉(zhuǎn)化為二進(jìn)制數(shù)組
byte[]imgBytesIn=br.ReadBytes(Convert.ToInt32(fs.Length));第二步:
//將圖片添加到數(shù)據(jù)庫中
stringsql="insertintopicvalues(@pic)";
sqlParameter[]param=newSqlParameter[]{newSqlParameter("@pic",imgBytesIn)};
DBHelper.GetExecuteQuery(sql,param);第三步:
//將圖片從數(shù)據(jù)庫中取出
stringsql="select*frompicwhereid=0";
SqlDataReaderreader=DBHelper.GetExecuteReader(sql,null);
MemoryStreammss=null;