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

来源:互联网 发布:农村婆婆知乎 编辑:程序博客网 时间:2024/05/06 16:53

在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>

这样就实现了从数据库中读取图片并显示的功能.

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wx4421/archive/2006/10/12/1331288.aspx