数据库存储图片的问题 我用的是sql 2005,现在想在数据库的图形界面做个数据库 其中有一栏是图片的,我试了几次都没成。这该怎么弄啊!!还有在编程时读取图片应该注意哪些方面! 希望有具体的步骤,特别是图片存储方面。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我常用的思路是:1、数据表中保存图片的字段类型设为Image2、把图片读到流3、从流中取出为Byte[]4、保存时把Byte[]写入到数据表的Image字段中。5、取出数据显示时,则与保存相反的顺序。 保存二进制数据到数据库using(SqlConnection conn = new SqlConnection("")){ conn.Open(); SqlCommand cmd = new SqlCommand("insert into Tb values(@img)", conn); byte[] b= new byte[60000]; FileStream fs = new FileStream("", FileMode.Open, FileAccess.Read); fs.Read(b, 0, 60000); cmd.Parameters.Add("@img", SqlDbType.Image, (int)fs.Length); cmd.Parameters["@img"].Value = b; cmd.ExecuteNonQuery(); conn.Close(); } 读取using(SqlConnection conn=new SqlConnection("")) { string strSql="select img from Tb"; SqlCommand cmd=new SqlCommand(strSql,conn); conn.Open(); SqlDataReader reader=cmd.ExecuteReader(); if(reader.Read()) { MemoryStream ms=new MemoryStream((byte[])reader["img "]); Image image=Image.FromStream(ms,true); picturebox1.Image=image; } reader.Close(); conn.Close(); } 我一般都是存储路径的,然后读取的时候用 this.images.ImageUrl = "images/" + ds.Tables[0].Rows[0]["路径"].ToString() SqlDbType.Image <=> byte[] <=> Stream Where查询语句中的日期问题 .net中在代码中如何创建带参数的存贮过程(急) C#中给treeView添加子节点? 组件“DevExpress.XtraBars.v7.2”。 如何遍历sql2000数据库,返回库中每个表的表名? C#开发Windows应用程序,在IDE设计状态下,能不能把一个数据库的表直接显示在DataGrid中?50分 简单问题!! C#发邮件正文 设计一个控件,如何在用户使用该控件编程时,单击时出现一个对话框? 终于搞完了烦琐的毕业设计!来,大家进来聊聊! winform下获取window系统登陆密码 类似QQ发送表情(C#)
1、数据表中保存图片的字段类型设为Image
2、把图片读到流
3、从流中取出为Byte[]
4、保存时把Byte[]写入到数据表的Image字段中。
5、取出数据显示时,则与保存相反的顺序。
using(SqlConnection conn = new SqlConnection(""))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into Tb values(@img)", conn);
byte[] b= new byte[60000];
FileStream fs = new FileStream("", FileMode.Open, FileAccess.Read);
fs.Read(b, 0, 60000);
cmd.Parameters.Add("@img", SqlDbType.Image, (int)fs.Length);
cmd.Parameters["@img"].Value = b;
cmd.ExecuteNonQuery();
conn.Close();
}
读取
using(SqlConnection conn=new SqlConnection(""))
{
string strSql="select img from Tb";
SqlCommand cmd=new SqlCommand(strSql,conn);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
MemoryStream ms=new MemoryStream((byte[])reader["img "]);
Image image=Image.FromStream(ms,true);
picturebox1.Image=image;
}
reader.Close();
conn.Close();
}