Implementation Code for ASP. NET Book information entry and asp.net book information entry
1. Create a test database and 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. Create the following page:
When you click "insert book information", save your information to the book_info table. Note: The cover Image must be uploaded to the "upload" folder under the root directory of the website, and then saved to the Image field in the book_info table of the database.
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 "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title>
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) {} protected void button#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 + "','" + Calend Ar1.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 successful! ");}}
The above is all the content of this article. I hope it will inspire you and help you learn it.