首页 > 数据库 > Access > 正文

图片上传到数据库并显示(C#+Access)

2024-09-07 19:04:55
字体:
来源:转载
供稿:网友
//上传文件:upload.aspx
<%@ page language="c#" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<%@ import namespace="system.io" %>
<script runat="server">

public void addperson(object sender, eventargs e)
{
int64 intimagesize;
string strimagetype;
stream imagestream ;

intimagesize = personimage.postedfile.contentlength;

strimagetype = personimage.postedfile.contenttype;

imagestream = personimage.postedfile.inputstream;

byte[] imagecontent = new byte[intimagesize];

int intstatus= imagestream.read(imagecontent,0,personimage.postedfile.contentlength);

oledbconnection myconnection = new oledbconnection( "provider=microsoft.jet.oledb.4.0;data source=" + server.mappath("ps.mdb"));
oledbcommand mycommand = new oledbcommand("insert into manhua(img,biaoti,laiyuan,author,keyword,content) values(@img,@biaoti,@laiyuan,@author,@keyword,@content)", myconnection);

mycommand.parameters.add("@img",imagecontent);
mycommand.parameters.add("@biaoti",txtname.text);
mycommand.parameters.add("@laiyuan",textbox1.text);
mycommand.parameters.add("@author",textbox2.text);
mycommand.parameters.add("@keyword",textbox3.text);
mycommand.parameters.add("@content",textbox4.text);
try
{
myconnection.open();
mycommand.executenonquery();
myconnection.close();
//response.write("<font color="red">new person successfully added!</font>");
}
catch(oledbexception ex)
{
response.write("insert failed.error details are:" + ex.tostring());
}

txtname.text="";
}

</script>
<html>
<head>
<title>upload image to db!</title>
</head>
<body style="font: 10pt verdana">
<form enctype="multipart/form-data" runat="server">
<table align="center">
<tbody>
<tr>
<td>
<font size="2">标&nbsp; 题:</font>
<asp:textbox id="txtname" runat="server"></asp:textbox>
</td>
</tr>
<tr>
<td>
<font size="2">来&nbsp; 源:</font>
<asp:textbox id="textbox1" runat="server"></asp:textbox>
</td>
</tr>
<tr>
<td>
<font size="2">作&nbsp;&nbsp;者:</font>
<asp:textbox id="textbox2" runat="server"></asp:textbox>
</td>
</tr>
<tr>
<td>
<font size="2">关键字:</font>
<asp:textbox id="textbox3" runat="server"></asp:textbox>
</td>
</tr>
<tr>
<td>
<font size="2">图&nbsp; 片:</font>
<input id="personimage" type="file" runat="server" />
</td>
</tr>
<tr>
<td>
<font size="2">内&nbsp; 容:</font>
<asp:textbox id="textbox4" runat="server" height="76px" width="383px" textmode="multiline"></asp:textbox>
</td>
</tr>
<tr>
<td>
<asp:button id="button1" onclick="addperson" text="添加" runat="server" width="60"></asp:button>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

//读出图片文件
<%@ page language="c#" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<script runat="server">

public void page_load(object sender, eventargs e)
{
if(!ispostback)
{
bindgrid();
}
}

private void bindgrid()
{
oledbconnection mycn = new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=" + server.mappath("ps.mdb"));
oledbcommand mycmd =new oledbcommand("select * from manhua",mycn);
mycmd.commandtype = commandtype.text;
try
{
mycn.open();
datalist1.datasource = mycmd.executereader(commandbehavior.closeconnection);
datalist1.databind();
}
catch(oledbexception ex)
{
response.write("error occured while generating data.error is " + ex.tostring());
}
}


public string formaturl(string strargument)
{
return("showimg.aspx?id=" + strargument);
}

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<table style="width: 792px; height: 531px" width="792">
<tbody>
<tr>
<td align="middle">
<asp:datalist id="datalist1" runat="server">
<itemtemplate>
<table>
<tr>
<td align="center">
<font size="3" color="red"><%# databinder.eval(container.dataitem,"biaoti")%></font></td>
</tr>
<tr>
<td align="center">
<font size="2">作者:<%# databinder.eval(container.dataitem,"author")%>&nbsp;&nbsp;来源:<%# databinder.eval(container.dataitem,"laiyuan")%></font></td>
</tr>
<tr>
<td align="center">
<itemtemplate>
<asp:image runat="server" id="image1" imageurl='<%# formaturl(databinder.eval(container.dataitem, "id").tostring()) %>' />
</itemtemplate>
</td>
</tr>
<tr>
<td align="center">
<font size="2"><%# databinder.eval(container.dataitem,"content")%></font>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist>
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

//显示图片
<%@ page language="c#" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<%@ import namespace="system.io" %>
<script runat="server">

public void page_load(object sender, eventargs e)
{
try
{
string strimageid=request.querystring["id"];
oledbconnection mycn = new oledbconnection("provider=microsoft.jet.oledb.4.0; data source = " + server.mappath("ps.mdb"));
oledbcommand mycmd =new oledbcommand("select * from manhua where id=" + strimageid,mycn);

mycn.open();
oledbdatareader dr;

dr = mycmd.executereader(commandbehavior.closeconnection);
if(dr.read())
{
response.contenttype = dr["biaoti"].tostring();
response.binarywrite((byte[])dr["img"]);
}
mycn.close();
}
catch(oledbexception ex)
{

}
}

</script>
<html>
<head>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表