如何读取保存在数据库中的图片显示在DataGrid中
2005-11-11 17:17
771 查看
在DataGrid中有一个模板列,里面放置了一个ImageButton控件,当然Image也可以。你首先必须绑定DataGrid才能在使用DataBinder.Eval(Container,"DataItem.BookGuid")等
如下:
<asp:Image id=Imagebutton1 runat="server" ImageUrl='<%# "BookCover.aspx?ImageID="+DataBinder.Eval(Container,"DataItem.BookGuid")%>'></asp:Image>
//这里是把图片的编号传到一个取图片的页面里,然后连接到图片上的,注意这里用的是ImageUrl,不是平常文本框用的text
BookCover.aspx页面
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string str=System.Configuration.ConfigurationSettings.AppSettings["cn"];//得到Webconfig里的连接字符串
SqlConnection cn=new SqlConnection(str);
SqlCommand cmd=new SqlCommand();
cmd.CommandText="select Cover from Books where BookGuid='"+this.Request["ImageID"]+"'";//取得数据库中的图片
cmd.Connection=cn;
cn.Open();
this.Response.ContentType="image/*";//设置类型
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.Response.BinaryWrite((byte[])dr["Cover"]);//读取后转换为字节数组
}
cn.Close();
}
如下:
<asp:Image id=Imagebutton1 runat="server" ImageUrl='<%# "BookCover.aspx?ImageID="+DataBinder.Eval(Container,"DataItem.BookGuid")%>'></asp:Image>
//这里是把图片的编号传到一个取图片的页面里,然后连接到图片上的,注意这里用的是ImageUrl,不是平常文本框用的text
BookCover.aspx页面
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string str=System.Configuration.ConfigurationSettings.AppSettings["cn"];//得到Webconfig里的连接字符串
SqlConnection cn=new SqlConnection(str);
SqlCommand cmd=new SqlCommand();
cmd.CommandText="select Cover from Books where BookGuid='"+this.Request["ImageID"]+"'";//取得数据库中的图片
cmd.Connection=cn;
cn.Open();
this.Response.ContentType="image/*";//设置类型
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
this.Response.BinaryWrite((byte[])dr["Cover"]);//读取后转换为字节数组
}
cn.Close();
}
相关文章推荐
- 如何读取保存在数据库中的图片显示在DataGrid中
- 如何读取保存在数据库中的图片显示在DataGrid中
- 如何读取保存在数据库中的图片显示在DataGrid中
- 如何读取保存在数据库中的图片显示在DataGrid中
- 如何将pictureBox里的图片保存到数据库,然后从数据读取显示
- web中,如何读取Word内容(包含表格,但不包含图片)并且显示在页面中或保存进数据库中
- web中,如何读取Word内容(包含表格,但不包含图片)并且显示在页面中或保存进数据库中
- 如何读取保存在数据库中的图片显示在DataGrid中
- WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- iphone Documents文件中保存的图片如何读取并显示?
- 图片保存到数据库和从数据库读取图片并显示(转)
- WPF——图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- C# 图片保存到数据库和从数据库读取图片并显示
- C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”
- 图片保存到数据库和从数据库读取图片并显示
- wpf图片的预览,以流的方式将图片保存在数据库中,再以流的方式从数据库中读取显示图片
- 图片保存到数据库和从数据库读取图片并显示(C#)
- 图片保存到数据库和从数据库读取图片并显示
- 图片保存到数据库和从数据库读取图片并显示(C#)
- VB6.0图片保存到数据库和从数据库读取图片并显示