最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#操作圖片讀取和存儲SQLserver實現(xiàn)代碼

 更新時間:2013年03月07日 11:47:41   作者:  
用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫/將圖片數(shù)據(jù)從SQLserver中取出來并顯示到pictureBox控件上,接下來將為你詳細介紹下實現(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);
}

相關文章

最新評論

民和| 夏邑县| 松溪县| 丹巴县| 荥经县| 都兰县| 峨山| 疏勒县| 惠东县| 商丘市| 武义县| 新余市| 徐汇区| 维西| 资兴市| 辛集市| 宜章县| 秦安县| 长宁区| 津市市| 鞍山市| 蓬安县| 临沭县| 阜新市| 海原县| 房产| 古交市| 古浪县| 定日县| 大安市| 连云港市| 东乌珠穆沁旗| 英德市| 安新县| 安图县| 荥经县| 英山县| 九江县| 唐山市| 南京市| 永泰县|