您的位置:首页 > 编程语言 > ASP

ASP.NET实现从数据库中读取图片的方法

2009-11-12 17:48 791 查看
在ASP.NET中,我们可以用下面的方法实现从数据库中读取图片并显示在页面上,方法如下:

SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
String sql="SELECT image FROM append where id='" + strID + "'";
SqlCommand command=new SqlCommand(sql,conn);

conn.Open();
SqlDataReader dr=command.ExecuteReader();
dr.Read();
byte[] imgdata = (byte[])dr["image"];
Response.BinaryWrite(imgdata);
dr.Close();
conn.Close();

在需要显示图片的地方,加上这样的代码,来控制图片显示的位置等信息:

<asp:Image ImageUrl="showAP.aspx?id=1" ID="imgLogo" Runat="server"></asp:Image>

这样就实现了从数据库中读取图片并显示的功能.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: