下面的代码实现从SQL Server数据库提取图片并显示在DataGrid的功能。

下面就是完整的代码,拷贝即可运行:

<%@ Page Language="vb" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <head> <title>用户列表</title> <script runat=server> Sub Page_Load(sender As Object, e As EventArgs) If Not Page.IsPostBack Then BindGrid() End If End Sub Private Sub BindGrid() Dim strCnn As String = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;" Dim myConnection As SqlConnection = New SqlConnection(strCnn) Dim myCommand As SqlCommand = New SqlCommand("SELECT * FROM Person", myConnection) myCommand.CommandType = CommandType.Text Try myConnection.Open() DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection) DG_Persons.DataBind() Catch SQLexc As SqlException Response.Write("Error occured while Generating Data. Error is " & SQLexc.ToString()) End Try End Sub Function FormatURL(strArgument) as String Return ("ReadImage.aspx?id=" & strArgument) End Function </script> </head> <body style="font: 9pt 宋体"> <h3 align=center>从数据库中取得照片并显示在DataGrid中</h3> <form id="Form1" method="post" runat="server"> <asp:DataGrid ID="DG_Persons" AutoGenerateColumns=False Width="99%" HeaderStyle-BackColor="#ff0000" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor=Beige BorderColor="#000000" Runat=server HeaderStyle-HorizontalAlign=Center> <Columns> <asp:TemplateColumn HeaderText="姓名"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>' ID="Label1"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="电子邮件"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>' ID="Label2"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="性别"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex") %>' ID="Label3"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="出生日期"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonDOB") %>' ID="Label4"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="照片"> <ItemTemplate> <asp:Image Runat=server ID="Image1" Width="150" Height="125" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID")) %>' /> </itemtemplate> </asp:TemplateColumn> </columns> </asp:DataGrid> </form> </body> </html>

ReadImage.aspx

<%@ Page Language="vb" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <HTML> <HEAD> <script runat=server> Public Sub Page_Load(sender As Object, e As EventArgs) Dim strImageID as String = Request.QueryString("id") Dim myConnection As New SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;") Dim myCommand As New SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" _ + strImageID, myConnection) Try myConnection.Open() Dim myDataReader as SqlDataReader myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) Do While (myDataReader.Read()) Response.ContentType = myDataReader.Item("PersonImageType") Response.BinaryWrite(myDataReader.Item("PersonImage")) Loop myConnection.Close() Catch SQLexc As SqlException End Try End Sub </script> </head> <body> <form runat="server" ID="Form1"></form> </body> </html>

C# 版本

DataGridShowImage.aspx

<%@ Page language="c#" debug="true" Codebehind="DataGridShowImage.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.DataGridShowImage.DataGridShowImage" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>从数据库中取得照片并显示在DataGrid中</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body MS_POSITIONING="GridLayout"> <form id="DataGridShowImage" method="post" runat="server"> <h3 align="center">从数据库中取得照片并显示在DataGrid中</h3> <asp:DataGrid ID="DG_Persons" AutoGenerateColumns="False" Width="99%" HeaderStyle-BackColor="#ff0000" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff" ItemStyle-BackColor="Beige" BorderColor="#000000" Runat="server" HeaderStyle-HorizontalAlign="Center"> <Columns> <asp:TemplateColumn HeaderText="姓名"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonName") %>' ID="Label1"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="电子邮件"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail") %>' ID="Label2"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="性别"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex") %>' ID="Label3"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="出生日期"> <ItemTemplate> <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonDOB") %>' ID="Label4"/> </itemtemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="照片"> <ItemTemplate> <asp:Image Runat=server ID="Image1" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID")) %>' /> </itemtemplate> </asp:TemplateColumn> </columns> </asp:DataGrid> </form> </body> </html>

DataGridShowImage.aspx.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace eMeng.Exam.DataGridShowImage { /// <summary> /// DataGridShowImage 的摘要说明。 /// </summary> public class DataGridShowImage : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid DG_Persons; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!this.IsPostBack) { BindGrid(); } } private void BindGrid() { string strCnn = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;"; SqlConnection myConnection = new SqlConnection(strCnn); SqlCommand myCommand = new SqlCommand("SELECT * FROM Person", myConnection); myCommand.CommandType = CommandType.Text; try { myConnection.Open(); DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection); DG_Persons.DataBind(); } catch(SqlException SQLexc) { Response.Write("提取数据时出现错误:" + SQLexc.ToString()); } } protected string FormatURL(object strArgument) { return "ReadImage.aspx?id=" + strArgument.ToString(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }

ReadImage.aspx

<%@ Page language="c#" Codebehind="ReadImage.aspx.cs" AutoEventWireup="false" Inherits="eMeng.Exam.DataGridShowImage.ReadImage" %>

ReadImage.aspx.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Data.SqlClient; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.DataGridShowImage { /// <summary> /// ReadImage 的摘要说明。 /// </summary> public class ReadImage : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 string strImageID = Request.QueryString["id"]; SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;"); SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" + strImageID, myConnection); try { myConnection.Open(); SqlDataReader myDataReader; myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); if(myDataReader.Read()) { Response.Clear(); Response.ContentType = myDataReader["PersonImageType"].ToString(); Response.BinaryWrite((byte[])myDataReader["PersonImage"]); } myConnection.Close(); } catch (SqlException SQLexc) { } Response.End(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
posted on 2006-01-12 18:46  xzcastle  阅读(199)  评论(0编辑  收藏  举报