从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;
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
}
}