从SQL Server数据库提取图片并显示在DataGrid
2024-07-21 02:23:06
供稿:网友
下面的代码实现从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
}
}