1. Create a test database in the database and create a book_info table in the test database.
book_name varchar (100)
Author varchar (50)
Press varchar (50)
Press_date varchar (20)
Image varchar (30)
2, make a following page:
When you click the Insert Book information button, the user's information is saved to the Book_info table. Note: The cover picture requirements are uploaded to the "Upload" folder in the root directory of the Web site, and then the relative paths in the Web site are saved to the Image field in the Database Book_info table.
Layout code:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
CS Code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.SqlClient; Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) {} p
rotected void Button1_Click (object sender, EventArgs e) {string savepath = Server.MapPath ("~/images/");
if (fileupload1.hasfile) {String fileName = fileupload1.filename;
Savepath + = FileName;
Fileupload1.saveas (Savepath);
String sql = "Data source=a25;initial catalog=test;integrated security=true"; String sqlstr = @ "Insert into Book_info (book_name,author,press,press_date,image) VALUES (' + TextBox1.Text +" ', ' "+ TextBox2.Text + "', '" + DropDownList1.SelectedItem.Text + "', '" + Calendar1.SelectedDate.ToShortDateString () + "', '
"+ Fileupload1.filename +" "; using (SqlConnection conn = new SqlConnection (SQL)) {conn.
Open (); Using(SqlCommand cmd = conn.)
CreateCommand ()) {cmd.commandtext = Sqlstr; Cmd.
ExecuteNonQuery (); } Response.Write ("Insert succeeded!")
"); }
}
Above is the whole content of this article, hope can give everybody an inspiration, to everybody's study help.