图片上传到数据库并显示(C#+Access)
2008-05-01 02:45
295 查看
//上传文件: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">标 题:</font>
<asp:Textbox id="txtname" runat="server"></asp:Textbox>
</td>
</tr>
<tr>
<td>
<font size="2">来 源:</font>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<font size="2">作 者:</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">图 片:</font>
<input id="PersonImage" type="file" runat="server" />
</td>
</tr>
<tr>
<td>
<font size="2">内 容:</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")%> 来源:<%# 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>
</htm
<%@ 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">标 题:</font>
<asp:Textbox id="txtname" runat="server"></asp:Textbox>
</td>
</tr>
<tr>
<td>
<font size="2">来 源:</font>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<font size="2">作 者:</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">图 片:</font>
<input id="PersonImage" type="file" runat="server" />
</td>
</tr>
<tr>
<td>
<font size="2">内 容:</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")%> 来源:<%# 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>
</htm
![](http://blog.csdn.net/upimg/20061210/11AIB0Z30L544.jpg)
相关文章推荐
- VS 2010中WinForm开发(C#)—图片上传到数据库与显示(sql server 2008)
- ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现
- 图片上传到数据库与显示(转载,C#→VB)
- ASP + ACCESS 上传图片到数据库与将图片读出数据库显示之实现(详细版)
- 转:上传图片到数据库,读取数据库中图片并显示(C#)
- 上传图片到数据库,读取数据库中图片并显示(C#)
- VS 2005中winForm开发(C#)—图片上传到数据库与显示(sql server 2005)
- Winform下如何上传图片并显示出来。同时保存到数据库
- 上传图片到数据库及显示
- C#用LINQ to SQL把PictureBox中的图片存入数据库和取出数据库中的图片并显示在PictureBox中
- PHP学习技巧总结乱码显示问题,register_global为On的时候,图片上传数据库
- 郁闷了半天,原来是这个问题:关于C#中关于图片保存到数据库以及将数据库中的图片显示到PictureBox。
- 用Seam实现:图片上传 + 保存到数据库 + 从数据库读出图片并显示到页面中
- 头像图片显示---数据库中保存路径 图片上传到工作目录下
- 图片上传并保存到数据库以及显示图片
- 上传图片以二进制插入到数据库,且读取显示
- 上传图片并显示缩略图的最简单方法(c#)
- C# 2.0中的上传多个图片合并及存入数据库的示例
- C#读取数据库图片显示、缩小、更新
- Asp.net 2.0(C#)图片存储到数据库和从数据库读取显示