ASP.NET書籍信息錄入實(shí)現(xiàn)代碼
1、 在數(shù)據(jù)庫(kù)中建立一個(gè)test數(shù)據(jù)庫(kù),在test數(shù)據(jù)庫(kù)中建立一個(gè)book_info表。
Book_name varchar(100)
Author varchar(50)
Press varchar(50)
Press_date varchar(20)
Image varchar(30)
2、 制作一個(gè)如下頁(yè)面:

當(dāng)單擊“插入圖書信息”按鈕時(shí),將用戶的信息保存到book_info表中。注意:封面圖片要求先上傳到網(wǎng)站根目錄下的“upload”文件夾中,再將圖片在網(wǎng)站中的相對(duì)路徑保存到數(shù)據(jù)庫(kù)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中js和jquery調(diào)用ashx的不同方法分享
asp.net中js和jquery調(diào)用ashx的不同方法分享,需要的朋友可以參考一下2013-06-06
ASP.NET中的幾種彈出框提示基本實(shí)現(xiàn)方法
NET程序的開發(fā)過程中,常常需要和用戶進(jìn)行信息交互,對(duì)話框的出現(xiàn)將解決了這些問題,下面是本人對(duì)常用對(duì)話框使用的小結(jié),希望對(duì)大家有所幫助2013-03-03
asp.net 上傳或下載當(dāng)文件名包含有特殊字符"#"的處理
在上傳或下載文件時(shí),當(dāng)文件名包含有"#"特殊字符時(shí),上傳以后的文件會(huì)被改名字,造成下載也下載不了。2010-03-03
ASP.net中保持頁(yè)面中滾動(dòng)條狀態(tài)的代碼
在ASP.Net頁(yè)面中,每次回發(fā)都會(huì)造成頁(yè)面回到頂部,那么怎樣讓它保持刷新頁(yè)面前的位置上,使得頁(yè)面提交后還在原來的位位置。2011-06-06
Entity?Framework代碼優(yōu)先Code?First入門
這篇文章介紹了Entity?Framework的代碼優(yōu)先模式Code?First,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
把a(bǔ)spx頁(yè)面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼
把a(bǔ)spx頁(yè)面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼,主要是利于搜索引擎的收錄。2011-10-10
ASP.NET Core MVC如何實(shí)現(xiàn)運(yùn)行時(shí)動(dòng)態(tài)定義Controller類型
這篇文章主要介紹了ASP.NET Core MVC如何實(shí)現(xiàn)運(yùn)行時(shí)動(dòng)態(tài)定義Controller類型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

