Upload the image to the database and display (c#+access)
Last Update:2017-02-28
Source: Internet
Author: User
access| Upload | data | database | Show//upload file: 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>
<title>upload image to Db!</title>
<body style= "font:10pt Verdana" >
<form enctype= "Multipart/form-data" runat= "Server" >
<table align= "center" >
<tbody>
<tr>
<td>
<font size= "2" > title:</font>
<asp:textbox id= "txtname" runat= "Server" ></asp:Textbox>
</td>
</tr>
<tr>
<td>
<font size= "2" > Source:</font>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<font size= "2" > Author:</font>
<asp:textbox id= "TextBox2" runat= "Server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<font size= "2" > Keyword:</font>
<asp:textbox id= "TextBox3" runat= "Server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<font size= "2" > Photo:</font>
<input id= "personimage" type= "file" runat= "Server"/>
</td>
</tr>
<tr>
<td>
<font size= "2" > Content:</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= "Add" runat= "Server" width= "></asp:Button>"
</td>
</tr>
</tbody>
</table>
</form>
</body>
Read the picture file
<%@ 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>
<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" > Author: <%# databinder.eval (Container.DataItem, "author")%> Source: <%# DataBinder.Eval ( Container.DataItem, "Laiyuan")%></font></td>
</tr>
<tr>
<TD align= "center" >
<ItemTemplate>
<asp:image runat= "Server" id= "Image1" Imageurl= ' <%# formaturl (DataBinder.Eval, "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>
Show pictures
<%@ 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>
<body>
<form id= "Form1" runat= "Server" >
</form>
</body>