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

首頁 > 編程 > C# > 正文

C#操作圖片讀取和存儲SQLserver實現代碼

2020-01-24 03:33:59
字體:
來源:轉載
供稿:網友
一、用C#將Image轉換成byte[]并插入數據庫:
1.1 將圖片控件的Image轉換成流:
復制代碼 代碼如下:

private byte[] PicToArray()
{
Bitmap bm = new Bitmap(picBox.Image);
MemoryStream ms = new MemoryStream();
bm.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}

復制代碼 代碼如下:

       
    //保存到數據庫
      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將圖片文件轉換成字節流并插入數據庫:
復制代碼 代碼如下:

class ImageInserter
{
public static int InsertImg(string path)
{
//----------以文件的方式讀取圖片并轉化成字節流
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();
}
}
}

二、將圖片數據從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);
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永康市| 汝南县| 汉沽区| 通化县| 屏南县| 来凤县| 南通市| 仪陇县| 宁波市| 循化| 大宁县| 徐州市| 习水县| 台东县| 深圳市| 阿巴嘎旗| 兖州市| 襄垣县| 昆明市| 岳阳县| 许昌县| 阿巴嘎旗| 巴青县| 民和| 儋州市| 阿拉善盟| 汕尾市| 黄山市| 阳新县| 德庆县| 怀来县| 梓潼县| 堆龙德庆县| 盘山县| 柳河县| 临潭县| 全南县| 西贡区| 安阳县| 九龙城区| 文成县|