📄 default.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //引用命名空间
using System.IO; //引用命名空间
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string cmdtxt1 = "Server=(local);DataBase=mrdb;Uid=sa;Pwd=";
string cmdtxt2 = "select BookID from tb_Book";
SqlConnection mycon = new SqlConnection(cmdtxt1);
mycon.Open();
SqlCommand mycom = new SqlCommand(cmdtxt2,mycon);
SqlDataReader dr = mycom.ExecuteReader();
while (dr.Read())
{
DropDownList3.Items.Add(dr[0].ToString());
}
dr.Close();
mycon.Close();
}
if (this.RadioButtonList1.SelectedIndex == 1)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = true;
}
else
{
this.Panel2.Visible = true;
this.Panel1.Visible = false;
this.Panel3.Visible = true;
this.Panel4.Visible = false;
}
}
protected void btnBackup_Click(object sender, EventArgs e)
{
SqlConnection myconn = new SqlConnection("server=(local);Database=mrdb;Uid=sa;Pwd=");
//打开链接
myconn.Open();
string str = "select count(*) from tb_Book where BookName='" + TextBox1.Text.ToString() + "'";
//创建SqlCommand对象
SqlCommand com = new SqlCommand(str, myconn);
int intcont = Convert.ToInt32(com.ExecuteScalar());
if (intcont > 0)
{
Response.Write("<script language=javascript>alert('对不起!不允许填写相同记录!')</script>");
}
else
{
try
{
string sqlstr = "insert into tb_Book(BookName,BookIntroduce,BookPrice,BookIsNew)values(@BookName,@BookIntroduce,@BookPrice,@BookIsNew)";
SqlCommand mycom = new SqlCommand(sqlstr, myconn);
////调用存储过程
//mycom.CommandType = CommandType.StoredProcedure;
//添加参数
SqlParameter[] prams ={
new SqlParameter("@BookName", SqlDbType.VarChar, 50),
new SqlParameter("@BookIntroduce", SqlDbType.VarChar, 50),
new SqlParameter("@BookPrice", SqlDbType.Money, 8),
new SqlParameter("@BookIsNew", SqlDbType.Char, 10),
};
//给参数赋值
prams[0].Value = TextBox1.Text;
prams[1].Value = TextBox2.Text;
prams[2].Value = Convert.ToDecimal(TextBox3.Text);
prams[3].Value = DropDownList1.SelectedValue.ToString();
foreach (SqlParameter parameter in prams)
{
mycom.Parameters.Add(parameter);
}
//执行sql语句
mycom.ExecuteNonQuery();
myconn.Close();
//BindData();
Response.Write("<script language=javascript>alert('添加成功!')</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
protected void btnRestore_Click(object sender, EventArgs e)
{
SqlConnection myconn = new SqlConnection("server=(local);Database=mrdb;Uid=sa;Pwd=");
//打开链接
myconn.Open();
string sqlstr = "update tb_Book set BookName=@BookName,BookIntroduce=@BookIntroduce,BookPrice=@BookPrice,BookIsNew=@BookIsNew where BookID=@BookID";
SqlCommand mycom = new SqlCommand(sqlstr, myconn);
SqlParameter[] prams ={
new SqlParameter("@BookName", SqlDbType.VarChar, 50),
new SqlParameter("@BookIntroduce", SqlDbType.VarChar, 50),
new SqlParameter("@BookPrice", SqlDbType.Money, 8),
new SqlParameter("@BookIsNew", SqlDbType.Char, 10),
new SqlParameter("@BookID",SqlDbType.Int,4)
};
//给参数赋值
prams[0].Value = TextBox4.Text;
prams[1].Value = TextBox6.Text;
prams[2].Value = Convert.ToDecimal(TextBox7.Text);
prams[3].Value = DropDownList2.SelectedValue.ToString();
//prams[4].Value = TextBox5.Text.Trim();
prams[4].Value = Convert.ToInt32(DropDownList3.SelectedValue.ToString());
//依次把参数传入命令文本
foreach (SqlParameter parameter in prams)
{
mycom.Parameters.Add(parameter);
}
try
{
//执行更新语句
mycom.ExecuteNonQuery();
Response.Write("<script language=javascript>alert('修改成功!')</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
Response.Write("<script language=javascript>alert('修改失败!')</script>");
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
this.TextBox1.Text = "";
this.TextBox2.Text = "";
this.TextBox3.Text = "";
}
protected void Button2_Click1(object sender, EventArgs e)
{
this.TextBox4.Text = "";
//this.TextBox5.Text = "";
this.TextBox6.Text = "";
this.TextBox7.Text = "";
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList3.SelectedValue!= null)
{
string cmdtxt1 = "Server=(local);DataBase=mrdb;Uid=sa;Pwd=";
string cmdtxt2 = "select * from tb_Book where bookid='" + Convert.ToInt32(DropDownList3.SelectedValue) + "'";
SqlConnection mycon = new SqlConnection(cmdtxt1);
mycon.Open();
SqlCommand mycom = new SqlCommand(cmdtxt2, mycon);
SqlDataReader dr = mycom.ExecuteReader();
while (dr.Read())
{
TextBox4.Text=dr[1].ToString();
TextBox6.Text=dr[2].ToString();
TextBox7.Text=dr[3].ToString();
}
dr.Close();
mycon.Close();
}
}
//protected void Button3_Click(object sender, EventArgs e)
//{
// Response.Redirect("Default2.aspx");
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -