下面的代码实现从SQL Server数据库提取图片并显示在DataGrid的功能。
下面就是完整的代码,拷贝即可运行:
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
eMeng.Exam.DataGridShowImage
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// DataGridShowImage 的摘要说明。
/// </summary>
public class DataGridShowImage : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
protected System.Web.UI.WebControls.DataGrid DG_Persons;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void Page_Load(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
BindGrid();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
private void BindGrid()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
myConnection.Open();
DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DG_Persons.DataBind();
}
catch(SqlException SQLexc)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("提取数据时出现错误:" + SQLexc.ToString());
}
}
protected string FormatURL(object strArgument)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return "ReadImage.aspx?id=" + strArgument.ToString();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
eMeng.Exam.DataGridShowImage
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// ReadImage 的摘要说明。
/// </summary>
public class ReadImage : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
private void Page_Load(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// 在此处放置用户代码以初始化页面
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);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
myConnection.Open();
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(myDataReader.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Clear();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Response.ContentType = myDataReader["PersonImageType"].ToString();
Response.BinaryWrite((byte[])myDataReader["PersonImage"]);
}
myConnection.Close();
}
catch (SqlException SQLexc)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
Response.End();
}
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}