asp.net c#從SQL2008讀取圖片顯示到網頁
阿新 • • 發佈:2020-12-13
//影象資料表: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>