您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)ASP.NET中怎么錄入書籍信息,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1、 在數(shù)據(jù)庫中建立一個test數(shù)據(jù)庫,在test數(shù)據(jù)庫中建立一個book_info表。
Book_name varchar(100)
Author varchar(50)
Press varchar(50)
Press_date varchar(20)
Image varchar(30)
2、 制作一個如下頁面:
當單擊“插入圖書信息”按鈕時,將用戶的信息保存到book_info表中。注意:封面圖片要求先上傳到網(wǎng)站根目錄下的“upload”文件夾中,再將圖片在網(wǎng)站中的相對路徑保存到數(shù)據(jù)庫book_info表的Image字段中。
布局代碼:
<%@ 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>機械工業(yè)出版社</asp:ListItem> <asp:ListItem>人民郵電出版社</asp:ListItem> <asp:ListItem>電子工業(yè)出版社</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代碼
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("插入成功!"); } }
看完上述內(nèi)容,你們對ASP.NET中怎么錄入書籍信息有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。