ASP.NET書籍信息錄入實(shí)現(xiàn)代碼
1、 在數(shù)據(jù)庫中建立一個(gè)test數(shù)據(jù)庫,在test數(shù)據(jù)庫中建立一個(gè)book_info表。
Book_name varchar(100)
Author varchar(50)
Press varchar(50)
Press_date varchar(20)
Image varchar(30)
2、 制作一個(gè)如下頁面:
當(dāng)單擊“插入圖書信息”按鈕時(shí),將用戶的信息保存到book_info表中。注意:封面圖片要求先上傳到網(wǎng)站根目錄下的“upload”文件夾中,再將圖片在網(wǎng)站中的相對(duì)路徑保存到數(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>清華大學(xué)出版社</asp:ListItem> <asp:ListItem>機(jī)械工業(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("插入成功!"); } }
以上就是本文的全部?jī)?nèi)容,希望可以給大家一個(gè)啟發(fā),對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
ASP.NET環(huán)境下為網(wǎng)站增加IP過濾功能
通過深入的交流和溝通,確認(rèn)了該發(fā)電廠在企業(yè)網(wǎng)站用戶訪問控制方面的改進(jìn)要求2009-06-06.NET截取指定長(zhǎng)度漢字超出部分以"..."代替 實(shí)例分享
.NET截取指定長(zhǎng)度漢字超出部分以"..."代替 實(shí)例分享,需要的朋友可以參考一下2013-06-06.NET中 關(guān)于臟讀 不可重復(fù)讀與幻讀的代碼示例
本篇文章小編將為大家介紹,.NET中 關(guān)于臟讀 不可重復(fù)讀與幻讀的代碼示例,有需要的朋友可以參考一下2013-04-04ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子
ASP.net(c#)用類的思想實(shí)現(xiàn)插入數(shù)據(jù)到ACCESS例子...2007-07-07比較簡(jiǎn)單的將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)
史上最簡(jiǎn)單將數(shù)據(jù)信息導(dǎo)入wrod文檔方案(C# for word)2010-01-01