SqlDataSource读取数据库的Image类型字段
虽然不大提倡,在数据库内存储图片,但有时会碰到,怎么读取呢,简单。
前台代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="getImage.aspx.cs" Inherits="getImage" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>无标题页</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12
13 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
14 ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
15 SelectCommand="SELECT ImageData FROM ImageStore where ImageID=1">
16
17 </asp:SqlDataSource>
18
19 </div>
20 </form>
21 </body>
22 </html>
23
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title>无标题页</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div>
12
13 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
14 ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
15 SelectCommand="SELECT ImageData FROM ImageStore where ImageID=1">
16
17 </asp:SqlDataSource>
18
19 </div>
20 </form>
21 </body>
22 </html>
23
注:ImageData字段的数据类型是Image
后台代码:
1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class getImage : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 DataView dv = (DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
19
20 byte[] buffer = (byte[])dv[0]["ImageData"];
21 Response.BinaryWrite(buffer);
22 Response.End();
23
24 }
25 }
26
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Linq;
6 using System.Web;
7 using System.Web.Security;
8 using System.Web.UI;
9 using System.Web.UI.HtmlControls;
10 using System.Web.UI.WebControls;
11 using System.Web.UI.WebControls.WebParts;
12 using System.Xml.Linq;
13
14 public partial class getImage : System.Web.UI.Page
15 {
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 DataView dv = (DataView)this.SqlDataSource1.Select(DataSourceSelectArguments.Empty);
19
20 byte[] buffer = (byte[])dv[0]["ImageData"];
21 Response.BinaryWrite(buffer);
22 Response.End();
23
24 }
25 }
26
就这样。
合乎自然而生生不息。。。