asp.net c#从SQL2008读取图片显示到网页
//图像数据表:tx
//字段id (nvarchar(50) ,image(image)
//tgav为图片ID,实质为上传前的主名 (省略了.jpg)
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class hander : System.Web.UI.Page { public override void ProcessRequest(HttpContext context) { string id = context.Request.QueryString["id"]; SqlConnection conn = new SqlConnection("server=MY\\SQLEXPRESS;uid=sa;pwd=*****;database=exaddddm"); SqlCommand cmd = new SqlCommand("select image from tx where id=@id", conn); cmd.Parameters.Add("@id", SqlDbType.NVarChar); cmd.Parameters["@id"].Value = id; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { context.Response.BinaryWrite((byte[])dr["image"]); } dr.Close(); } protected void Page_Load(object sender, EventArgs e) { } }
新建上面的网页保存为hander.aspx
其他页面的图片URL连接地址如下:
<td class="auto-style26" rowspan="5" style="text-align: left">
<asp:Image ID="tx1" runat="server" Height="160px" Width="400px" ImageUrl='<%# "hander.aspx?id="+Eval("tx") %>' Visible='<%# Eval("tx").ToString()!=null ?true:false %>' AlternateText='<%# " " %>' />
</td>