📄 bookadd.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration ;
using System.Data .SqlClient ;
namespace BMS
{
/// <summary>
/// BookAdd 的摘要说明。
/// </summary>
public class BookAdd : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbx_bname;
protected System.Web.UI.WebControls.TextBox tbx_bisbn;
protected System.Web.UI.WebControls.TextBox tbx_bauthor;
protected System.Web.UI.WebControls.TextBox tbx_bpress;
protected System.Web.UI.WebControls.TextBox tbx_bprice;
protected System.Web.UI.WebControls.TextBox tbx_bdescribe;
protected System.Web.UI.WebControls.DropDownList ddl_bgroup;
protected System.Web.UI.WebControls.Button btn_submit;
protected System.Web.UI.WebControls.HyperLink hlk_bookmanage;
protected System.Web.UI.WebControls.RegularExpressionValidator rev_bprice;
protected System.Web.UI.WebControls.RequiredFieldValidator rfv_bname;
protected System.Web.UI.WebControls.RequiredFieldValidator rfv_bisbn;
protected System.Web.UI.WebControls.RequiredFieldValidator rfv_banthor;
protected System.Web.UI.WebControls.RequiredFieldValidator rfv_bpress;
protected System.Web.UI.WebControls.RequiredFieldValidator rfv_bprice;
protected System.Web.UI.WebControls.RegularExpressionValidator rev_bisbn;
protected System.Web.UI.WebControls.DropDownList ddl_bdiscount;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btn_submit.Click += new System.EventHandler(this.btn_submit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btn_submit_Click(object sender, System.EventArgs e)
{
if(Page.IsValid )
{//从文件Web.config中读取连接字符串
string strconn= ConfigurationSettings.AppSettings["dsn"];
//连接本地计算机的BMS数据库,创建三个,分别供以下三个连接
SqlConnection cn= new SqlConnection (strconn);
cn.Open ();
SqlConnection cn1= new SqlConnection (strconn);
cn1.Open ();
SqlConnection cn2= new SqlConnection (strconn);
cn2.Open ();
//利用Command对象调用存储过程,创建添加book表命令类型
SqlCommand cmbookadd=new SqlCommand ("bookadd",cn);
//将命令类型转为存储类型
cmbookadd.CommandType =CommandType.StoredProcedure ;
//添加并给参数付值
cmbookadd.Parameters .Add ("@BID",SqlDbType.Int);
cmbookadd.Parameters .Add ("@BISBN",SqlDbType.VarChar);
cmbookadd.Parameters .Add ("@BName",SqlDbType.VarChar);
cmbookadd.Parameters .Add ("@BAuthor",SqlDbType.VarChar);
cmbookadd.Parameters .Add ("@BPress",SqlDbType.VarChar );
cmbookadd.Parameters .Add ("@BGroup",SqlDbType.Int );
cmbookadd.Parameters .Add ("@BDescribe",SqlDbType.VarChar);
//利用Command对象调用存储过程,创建添加bookprice表命令类型
SqlCommand cmbookpriceadd=new SqlCommand ("bookpriceadd",cn1);
//将命令类型转为存储类型
cmbookpriceadd.CommandType =CommandType.StoredProcedure ;
//添加并给参数付值
cmbookpriceadd.Parameters .Add ("@PPID",SqlDbType.Int);
cmbookpriceadd.Parameters .Add ("@PPrice",SqlDbType.Money );
cmbookpriceadd.Parameters .Add ("@PDiscount",SqlDbType.Float );
//利用Command对象调用存储过程,创建添加BookMaxBIDGet表命令类型
//该类型返回我们所要往book,bookprice表中添加的BID值
SqlCommand cmbookmaxbidget=new SqlCommand ("BookMaxBIDGet ",cn2);
//将命令类型转为存储类型
cmbookmaxbidget.CommandType =CommandType.StoredProcedure ;
//添加并给参数付值
cmbookmaxbidget.Parameters .Add ("@maxbid",SqlDbType.Int);
//将BookMaxBIDGet过程参数输出
cmbookmaxbidget.Parameters ["@maxbid"].Direction =ParameterDirection.Output ;
cmbookmaxbidget.ExecuteReader ();
string maxbidstring=cmbookmaxbidget.Parameters ["@maxbid"].Value.ToString();
//考虑数据库为空的情况,maxbidstring将为空
int maxbid ;
if(maxbidstring=="")//如果数据库为空
{maxbid=1 ;
}
else//如果数据库有数据
{maxbid=Convert.ToInt32(maxbidstring)+1 ; //取出表中BID的最大值作为下一纪录的BID
}
//往book表中参数付值
cmbookadd.Parameters ["@BID"].Value =maxbid;
cmbookadd.Parameters ["@BName"].Value =tbx_bname.Text .ToString ();
cmbookadd.Parameters ["@BISBN"].Value =tbx_bisbn.Text .ToString ();
cmbookadd.Parameters ["@BAuthor"].Value =tbx_bauthor.Text .ToString ();
cmbookadd.Parameters ["@BPress"].Value =tbx_bpress.Text .ToString ();
cmbookadd.Parameters ["@BDescribe"].Value =tbx_bdescribe.Text .ToString ();
cmbookadd.Parameters ["@BGroup"].Value =ddl_bgroup.SelectedItem .Value.ToString () ;
//往bookprice表中参数付值
cmbookpriceadd.Parameters ["@PPID"].Value =maxbid;
cmbookpriceadd.Parameters ["@PPrice"].Value =Convert.ToDouble (tbx_bprice.Text .ToString ());
cmbookpriceadd.Parameters ["@PDiscount"].Value =Convert.ToInt16 (ddl_bdiscount.SelectedItem .Value .ToString());
cmbookadd.ExecuteNonQuery ();
cmbookpriceadd.ExecuteNonQuery ();
//关闭连接
cn.Close();
cn1.Close ();
cn2.Close ();
Response.Redirect("bookmanage.aspx");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -