数据库存储图片的问题

Chen_87217 2010-12-20 08:35:48
我用的是sql 2005,现在想在数据库的图形界面做个数据库 其中有一栏是图片的,我试了几次都没成。这该怎么弄啊!!还有在编程时读取图片应该注意哪些方面! 希望有具体的步骤,特别是图片存储方面。谢谢

...全文
77 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dobdream 2010-12-20
  • 打赏
  • 举报
回复
SqlDbType.Image <=> byte[] <=> Stream
TNight 2010-12-20
  • 打赏
  • 举报
回复
我一般都是存储路径的,然后读取的时候用 this.images.ImageUrl = "images/" + ds.Tables[0].Rows[0]["路径"].ToString()
wuyq11 2010-12-20
  • 打赏
  • 举报
回复
保存二进制数据到数据库
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();
}
whb147 2010-12-20
  • 打赏
  • 举报
回复
那样数据库要多达呀
ggw128 2010-12-20
  • 打赏
  • 举报
回复
我常用的思路是:
1、数据表中保存图片的字段类型设为Image
2、把图片读到流
3、从流中取出为Byte[]
4、保存时把Byte[]写入到数据表的Image字段中。
5、取出数据显示时,则与保存相反的顺序。
int64 2010-12-20
  • 打赏
  • 举报
回复
现在想在数据库的图形界面做个数据库。。。。。啥意思

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧