C#操作圖片讀取和存儲SQLserver實(shí)現(xiàn)代碼
更新時(shí)間:2013年03月07日 11:47:41 作者:
用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫/將圖片數(shù)據(jù)從SQLserver中取出來并顯示到pictureBox控件上,接下來將為你詳細(xì)介紹下實(shí)現(xiàn)步驟,感興趣的你可以參考下
一、用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫:
1.1 將圖片控件的Image轉(zhuǎn)換成流:
private byte[] PicToArray()
{
Bitmap bm = new Bitmap(picBox.Image);
MemoryStream ms = new MemoryStream();
bm.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}
//保存到數(shù)據(jù)庫
try
{
string sql = "update T_Employee set ImageLogo=@ImageLogo where EmpId=@EmpId";
SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@ImageLogo", imgSourse));
MessageBox.Show("修改已保存!");// ShowInfo(0);
}
catch (Exception ex)
{
MessageBox.Show("更新失敗!" + ex.Message);
return;
}
1.2將圖片文件轉(zhuǎn)換成字節(jié)流并插入數(shù)據(jù)庫:
class ImageInserter
{
public static int InsertImg(string path)
{
//----------以文件的方式讀取圖片并轉(zhuǎn)化成字節(jié)流
FileStream fs = new FileStream(path,FileMode.Open);
byte[] imgSourse = new byte[fs.Length];
fs.Read(imgSourse,0,imgSourse.Length);
fs.Close();
using (SqlConnection conn = new SqlConnection(SqlHelper.connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update T_Employee set ImageLogo=@ImageLogo";
// cmd.Parameters.Add("@ImageLogo", SqlDbType.Image);
cmd.Parameters.Add(new SqlParameter("@ImageLogo", imgSourse));
return cmd.ExecuteNonQuery();
}
}
}
二、將圖片數(shù)據(jù)從SQLserver中取出來并顯示到pictureBox控件上:
byte[] ImageLogoArray = row["ImageLogo"] is DBNull ? null :(byte[])(row["ImageLogo"]);
MemoryStream ms=null;
if (ImageLogoArray!=null)
{
ms = new MemoryStream(ImageLogoArray);
picBox.Image = new Bitmap(ms);
}
1.1 將圖片控件的Image轉(zhuǎn)換成流:
復(fù)制代碼 代碼如下:
private byte[] PicToArray()
{
Bitmap bm = new Bitmap(picBox.Image);
MemoryStream ms = new MemoryStream();
bm.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}
復(fù)制代碼 代碼如下:
//保存到數(shù)據(jù)庫
try
{
string sql = "update T_Employee set ImageLogo=@ImageLogo where EmpId=@EmpId";
SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@ImageLogo", imgSourse));
MessageBox.Show("修改已保存!");// ShowInfo(0);
}
catch (Exception ex)
{
MessageBox.Show("更新失敗!" + ex.Message);
return;
}
1.2將圖片文件轉(zhuǎn)換成字節(jié)流并插入數(shù)據(jù)庫:
復(fù)制代碼 代碼如下:
class ImageInserter
{
public static int InsertImg(string path)
{
//----------以文件的方式讀取圖片并轉(zhuǎn)化成字節(jié)流
FileStream fs = new FileStream(path,FileMode.Open);
byte[] imgSourse = new byte[fs.Length];
fs.Read(imgSourse,0,imgSourse.Length);
fs.Close();
using (SqlConnection conn = new SqlConnection(SqlHelper.connStr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update T_Employee set ImageLogo=@ImageLogo";
// cmd.Parameters.Add("@ImageLogo", SqlDbType.Image);
cmd.Parameters.Add(new SqlParameter("@ImageLogo", imgSourse));
return cmd.ExecuteNonQuery();
}
}
}
二、將圖片數(shù)據(jù)從SQLserver中取出來并顯示到pictureBox控件上:
復(fù)制代碼 代碼如下:
byte[] ImageLogoArray = row["ImageLogo"] is DBNull ? null :(byte[])(row["ImageLogo"]);
MemoryStream ms=null;
if (ImageLogoArray!=null)
{
ms = new MemoryStream(ImageLogoArray);
picBox.Image = new Bitmap(ms);
}
相關(guān)文章
C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果
本文主要介紹了C#調(diào)用EXE文件實(shí)現(xiàn)傳參和獲取返回結(jié)果,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01vista和win7在windows服務(wù)中交互桌面權(quán)限問題解決方法:穿透Session 0 隔離
服務(wù)(Service)對于大家來說一定不會陌生,它是Windows 操作系統(tǒng)重要的組成部分。我們可以把服務(wù)想像成一種特殊的應(yīng)用程序,它隨系統(tǒng)的“開啟~關(guān)閉”而“開始~停止”其工作內(nèi)容,在這期間無需任何用戶參與2016-04-04C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類介紹
本文主要介紹了C#中的靜態(tài)成員、靜態(tài)方法、靜態(tài)類的基礎(chǔ)的使用,并做了相關(guān)的代碼演示,供初學(xué)者參考。2016-03-03