怎么把图片存放到SQL数据库中呀? C#做的WINDOW窗体程序,我想把一些图片放到SQL数据库中,怎么保存啊? 或者这样我的图片都放在D:\图片 文件夹下,想在窗体中点击 BUTTON按钮,然后自动把数据导入到数据库中,怎么办了,会的给点意见,给代码感觉不尽啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/t/20051128/10/4422308.html参考该贴 命令中设置参数,类型为byte[],然后将图片转换为byte[]并赋给参数,执行保存命令就可以了。 图片读取出来,就成了byte[],然后再写入表的时候使用SqlParameter,图片列的类型用Image,然后直接赋值执行就好了。 //插入图像数据 private void InsertImage() { try { if (null == this.picIndiPhoto.Image) //图像为空时, 不执行操作 { return; } MemoryStream MS = new MemoryStream(); Bitmap bmp = new Bitmap(this.picIndiPhoto.Image); //picIndiPhoto为Picture控件 bmp.Save(MS, ImageFormat.Bmp); Byte[] ImageData = new Byte[MS.Length]; //将相片转为 Byte 类型数据 MS.Position = 0; MS.Read(ImageData, 0, Convert.ToInt32(MS.Length)); string sql = "UPDATE t_UserInfo SET UsrPhoto=@Photo WHERE UsrID='" + this.txtUserID.Text + "'"; //SQL语句 SqlCommand dbCmd = new SqlCommand(sql); dbCmd.CommandType = CommandType.Text; SqlParameter parameter = new SqlParameter("@Photo", SqlDbType.Image, int.MaxValue); parameter.Value = ImageData; dbCmd.Parameters.Add(parameter); SQLHelper.ExecuteNonQuery(dbCmd); } catch (Exception ex) { Console.WriteLine("错误信息: " + ex.Message); } }//显示图像数据private void ShowImage(){ string strexperssion = string.Format("SELECT UsrPhoto FROM t_UserInfo WHERE UsrID='{0}'", strUserID); //SQL语句 try { byte[] data = (Byte[])SQLHelper.ExecuteScalar(strexperssion); if (!data.Length.Equals(0)) { MemoryStream MS = new MemoryStream(data); Bitmap image = new System.Drawing.Bitmap(MS); this.picIndiPhoto.Image = image; // 从数据库里读出放在picIndiPhoto里 picIndiPhoto为Picture控件 } } catch (InvalidCastException ex) //此处需处理空值异常 { Console.WriteLine(ex.Message); } catch (Exception ec) { Console.WriteLine(ec.Message); }} 片读取出来,就成了byte[],然后再写入表的时候使用SqlParameter,图片列的类型用Image,然后直接赋值执行就好了。 SQL2000存储过程问题 C#中,用VS生成的Windows应用程序,为什么在桌面上无法运行? 存储过程 如何在窗体load的时候关闭窗体? 在文本框里有刷条码刷入 FormView和GridView的显示和隐藏问题 访问记录问题 急!怎样调用C#编写的dll中的类啊? 请问在c#,如何引用dll? C#winform利用WebClient上传文件时,不报错误,但文件也上传不上去,求解决办法 没创新的人别进! c# 编程题目 你有兴趣试试么````
参考该贴
private void InsertImage()
{
try
{
if (null == this.picIndiPhoto.Image) //图像为空时, 不执行操作
{
return;
}
MemoryStream MS = new MemoryStream();
Bitmap bmp = new Bitmap(this.picIndiPhoto.Image); //picIndiPhoto为Picture控件
bmp.Save(MS, ImageFormat.Bmp);
Byte[] ImageData = new Byte[MS.Length]; //将相片转为 Byte 类型数据 MS.Position = 0;
MS.Read(ImageData, 0, Convert.ToInt32(MS.Length)); string sql = "UPDATE t_UserInfo SET UsrPhoto=@Photo WHERE UsrID='" + this.txtUserID.Text + "'"; //SQL语句
SqlCommand dbCmd = new SqlCommand(sql);
dbCmd.CommandType = CommandType.Text; SqlParameter parameter = new SqlParameter("@Photo", SqlDbType.Image, int.MaxValue);
parameter.Value = ImageData;
dbCmd.Parameters.Add(parameter); SQLHelper.ExecuteNonQuery(dbCmd);
}
catch (Exception ex)
{
Console.WriteLine("错误信息: " + ex.Message);
}
}//显示图像数据
private void ShowImage()
{
string strexperssion = string.Format("SELECT UsrPhoto FROM t_UserInfo WHERE UsrID='{0}'", strUserID); //SQL语句
try
{
byte[] data = (Byte[])SQLHelper.ExecuteScalar(strexperssion);
if (!data.Length.Equals(0))
{
MemoryStream MS = new MemoryStream(data);
Bitmap image = new System.Drawing.Bitmap(MS);
this.picIndiPhoto.Image = image; // 从数据库里读出放在picIndiPhoto里 picIndiPhoto为Picture控件
}
}
catch (InvalidCastException ex) //此处需处理空值异常
{
Console.WriteLine(ex.Message);
}
catch (Exception ec)
{
Console.WriteLine(ec.Message);
}
}