Access Database code
<% @ Page Language = "C #" EnableViewState = "true" %> <% @ Import Namespace = "System. Data. OleDb" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat = "server"> 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; // Construct a database connection, SQL statement, and create the string strCnn = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Server. mapPath ("Image2Access. mdb "); OleDbConnection myConnection = new OleDbConnec Tion (strCnn); OleDbCommand command = new OleDbCommand ("insert into Person (PersonName, PersonEmail, PersonSex, PersonImageType, PersonImage)" + "VALUES (@ PersonName, @ PersonEmail, @ PersonSex, @ PersonImageType, @ PersonImage) ", myConnection); command. parameters. addWithValue ("@ PersonName", TextBox1.Text); command. parameters. addWithValue ("@ PersonEmail", "mengxianhui@dotnet.aspx.cc"); command. parameters. addWithValue ("@ ParamPersonSex", "male"); command. parameters. addWithValue ("@ PersonImageType", fileType); command. parameters. addWithValue ("@ PersonImage", fileData); // open the connection and run 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 () {string strCnn = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ Server. mapPath ("Image2Access. mdb "); OleDbConnection myConnection = new OleDbConnection (strCnn); OleDbCommand myCommand = new OleDbCommand (" SELECT * FROM Person ", myConnection); try {myConnection. open (); GridView1.DataSource = myCommand. executeReader (System. data. commandBehavior. closeCon Nection); GridView1.DataBind ();} catch (OleDbException SQLexc) {Response. write ("error during data extraction:" + SQLexc. toString () ;}} protected string FormatURL (object strArgument) {return "ReadImage. aspx? Id = "+ strArgument. toString ();} </script> <ptml xmlns = "http://www.w3.org/1999/xhtml"> <pead runat = "server"> <title> upload files to the database </title> </pead> <body> <form id = "MengXianhui" runat = "server"> <asp: gridView ID = "GridView1" runat = "server" AutoGenerateColumns = "false"> <Columns> <asp: TemplateField> <ItemTemplate> <% # Eval ("PersonName ") %> </ItemTemplate> </asp: TemplateField> <ItemTemplate> <% # Eval ("PersonEmail") %> </ItemTemplate> </asp: templateField> <asp: TemplateField> <ItemTemplate> <% # Eval ("PersonSex") %> </ItemTemplate> </asp: TemplateField> <asp: templateField> <ItemTemplate> "/> </ItemTemplate> </asp: templateField> </Columns> </asp: GridView> <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> </ptml>