1、 在数据库中建立一个test数据库,在test数据库中建立一个book_info表。
Book_name varchar(100)
Author varchar(50)
Press varchar(50)
Press_date varchar(20)
Image varchar(30)
2、 制作一个如下页面:
当单击“插入图书信息”按钮时,将用户的信息保存到book_info表中。注意:封面图片要求先上传到网站根目录下的“upload”文件夹中,再将图片在网站中的相对路径保存到数据库book_info表的Image字段中。
布局代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<%@ 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 ></ title > </ head > < body > < form id = "form1" runat = "server" > < table > < tr > < td >< asp:Label ID = "Label1" runat = "server" Text = "书名:" ></ asp:Label ></ td > < td >< asp:TextBox ID = "TextBox1" runat = "server" ></ asp:TextBox > </ td > </ tr > < tr > < td >< asp:Label ID = "Label2" runat = "server" Text = "作者:" ></ asp:Label ></ td > < td > < asp:TextBox ID = "TextBox2" runat = "server" ></ asp:TextBox ></ td > </ tr > < tr > < td > < asp:Label ID = "Label3" runat = "server" Text = "出版社:" ></ asp:Label ></ td > < td >< asp:DropDownList ID = "DropDownList1" runat = "server" AutoPostBack = "True" > < asp:ListItem >清华大学出版社</ asp:ListItem > < asp:ListItem >机械工业出版社</ asp:ListItem > < asp:ListItem >人民邮电出版社</ asp:ListItem > < asp:ListItem >电子工业出版社</ asp:ListItem > </ asp:DropDownList ></ td > </ tr > < tr > < td >< asp:Label ID = "Label4" runat = "server" Text = "出版日期:" ></ asp:Label ></ td > < td >< asp:Calendar ID = "Calendar1" runat = "server" ></ asp:Calendar ></ td > </ tr > < tr > < td > < asp:Label ID = "Label5" runat = "server" Text = "封面图片:" ></ asp:Label ></ td > < td > < asp:FileUpload ID = "FileUpload1" runat = "server" /></ td > </ tr > < tr > < td ></ td > < td > < asp:Button ID = "Button1" runat = "server" Text = "插入图片信息" onclick = "Button1_Click" /></ td > </ tr > </ table > </ form > </ body > </ html > |
cs代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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 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( "插入成功!" ); } } |
以上就是本文的全部内容,希望可以给大家一个启发,对大家的学习有所帮助。