發布信息時,图片上传并顯示在TEXTBOX(C#+Access)

图片上传到数据库并显示(C#+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 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" οnclick="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>
</htm

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值