access|上传|数据|数据库|显示//上传文件: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 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 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>
</html>
图片上传到数据库并显示(C#+Access)
80酷酷网 80kuku.com