|
<% @ Page Language = "C #" enableviewstate = "true" %>
<% @ Import namespace = "system. Data. sqlclient" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<SCRIPT runat = "server">
String strcnn = "Persist Security info = false; user id = sa; Password =; initial catalog = book; server = (local );";
Protected void button#click (Object sender, eventargs E)
{
System. Io. Stream filedatastream = fileupload1.postedfile. inputstream;
If (filedatastream. Length <1)
{
MSG. Text = "select a file. ";
Return;
}
// Get the file size
Int filelength = fileupload1.postedfile. contentlength;
// Create an array
Byte [] filedata = new byte [filelength];
// Fill in the file stream to the array
Filedatastream. Read (filedata, 0, filelength );
// Obtain the file type
String filetype = fileupload1.postedfile. contenttype;
// Create database connections, SQL statements, and parameters
Sqlconnection myconnection = new sqlconnection (strcnn );
Sqlcommand command = new sqlcommand ("insert into userphoto (username, contenttype, photo)" +
"Values (@ username, @ contenttype, @ photo)", myconnection );
Command. Parameters. addwithvalue ("@ username", textbox1.text );
Command. Parameters. addwithvalue ("@ contenttype", filetype );
Command. Parameters. addwithvalue ("@ photo", filedata );
// Open the connection and execute the query
Myconnection. open ();
Command. executenonquery ();
Myconnection. Close ();
Response. Redirect (request. rawurl );
}
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
Bindgrid ();
}
}
Private void bindgrid ()
{
Sqlconnection myconnection = new sqlconnection (strcnn );
Sqlcommand mycommand = new sqlcommand ("select * From userphoto order by id desc", myconnection );
Try
{
Myconnection. open ();
Gridview1.datasource = mycommand. executereader (system. Data. commandbehavior. closeconnection );
Gridview1.databind ();
}
Catch (exception sqlexc)
{
Response. Write ("error during data extraction:" + sqlexc. tostring ());
}
}
Protected string formaturl (Object strargument)
{
Return "ReadImage. aspx? Id = "+ strargument. tostring ();
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> upload a file to the database </title>
</Head>
<Body>
<Form ID = "mengxianhui" runat = "server">
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: templatefield>
<Itemtemplate>
<% # Eval ("username") %>
</Itemtemplate>
</ASP: templatefield>
<Asp: templatefield>
<Itemtemplate>
</itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
<Br/>
<Br/>
Name: <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Br/>
Photo: <asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "btnupload" runat = "server" text = "Upload" onclick = "button#click"> </ASP: button>
<P>
<Asp: Label id = "MSG" runat = "server" forecolor = "red"> </ASP: Label> </P>
</Form>
</Body>
</Html> |