Asp. NET Book Information Entry implementation code _ Practical skills

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.