📄 bookmanage.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class bookmanage : System.Web.UI.Page
{
private static int MyBookID = 0;
protected void Page_Load(object sender, EventArgs e)
{
insert.Enabled = false;
update.Enabled = false;
delete.Enabled = false;
}
//录入书籍信息
protected void insert_Click1(object sender, EventArgs e)
{
if (booknametxt.Text == "")
{
lblmessage.Text = "必须填写书名!";
}
if (booknumtxt.Text == "")
{
lblmessage.Text = "必须填写图书编号!";
}
else
{
SqlConnection con = new SqlConnection("Server=.;database=Mylibrary;uid=sa;pwd=123");
//con.Open();
string comstring = "insert into bookmessage (书名,图书编号,定价,类别,作者,出版社,出版时间,存放位置) values ('" + booknametxt.Text + "','" + booknumtxt.Text + "'," + pricetxt.Text + ",'" + typetxt.Text + "','" + writertxt.Text + "','" + presstxt.Text + "','" + presstimetxt.Text + "','" + placetxt.Text + "')";
SqlCommand com = new SqlCommand(comstring, con);
try
{
con.Open();
int r = com.ExecuteNonQuery();
if (r == 1)
{
Response.Write("<Script Language=JavaScript> alert(\"录入成功!\")</Script>");
}
}
catch (Exception ex)
{
Response.Write("出现错误,原因是:" + ex.Message);
}
finally
{
con.Close();
}
booknametxt.Text = "";
booknumtxt.Text = "";
pricetxt.Text = "";
typetxt.Text = "";
writertxt.Text = "";
presstxt.Text = "";
presstimetxt.Text = "";
placetxt.Text = "";
}
}
//修改操作
protected void update_Click(object sender, EventArgs e)
{
string sql = "UPDATE bookmessage SET 图书编号='" + booknumtxt.Text + "',定价=" + pricetxt.Text + ",类别='" + typetxt.Text + "',作者='" + writertxt.Text + "',出版社='" + presstxt.Text + "',出版时间='" + presstimetxt.Text + "',存放位置='" + placetxt.Text + "'where 书名='" + booknametxt.Text + "'";
SqlConnection conn = new SqlConnection("Server=.;database=Mylibrary;uid=sa;pwd=123");
SqlCommand com = new SqlCommand(sql, conn);
try
{
conn.Open();
int r = com.ExecuteNonQuery();
if (r == 1)
{
Response.Write("<Script Language=JavaScript> alert(\"修改成功!\")</Script>");
}
}
catch(Exception ex)
{
Response.Write("出现错误,原因是:" + ex.Message );
}
finally
{
conn.Close();
}
booknametxt.Text = "";
booknumtxt.Text = "";
pricetxt.Text = "";
typetxt.Text = "";
writertxt.Text = "";
presstxt.Text = "";
presstimetxt.Text = "";
placetxt.Text = "";
}
protected void delete_Click(object sender, EventArgs e)
{
string sql = "delete from bookmessage where 书名='" + booknametxt.Text + "'";
SqlConnection conn = new SqlConnection("Server=.;database=Mylibrary;uid=sa;pwd=123");
SqlCommand com = new SqlCommand(sql, conn);
try
{
conn.Open();
int r = com.ExecuteNonQuery();
if (r == 1)
{
Response.Write("<Script Language=JavaScript> alert(\"删除成功!\")</Script>");
}
}
catch (Exception ex)
{
Response.Write("出现错误,原因是:" + ex.Message);
}
finally
{
conn.Close();
}
booknumtxt.Text = "";
booknametxt.Text = "";
pricetxt.Text = "";
typetxt.Text = "";
writertxt.Text = "";
presstxt.Text = "";
presstimetxt.Text = "";
placetxt.Text = "";
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("Server=.;database=Mylibrary;uid=sa;pwd=123");
cn.Open();
string MySQL = "Select COUNT(图书编号) From bookmessage WHERE 图书编号= '" + booknumtxt.Text + "'";
SqlCommand MyCommand = new SqlCommand(MySQL, cn);
string MyCount = MyCommand.ExecuteScalar().ToString();
MyBookID = Convert.ToInt16(MyCount);
if (MyBookID > 0)
{
Response.Write("<Script Language=JavaScript> alert(\"已有该编号,不能再录入!\")</Script>");
insert.Enabled = false;
string MySQL1 = "Select * From bookmessage WHERE 图书编号= '" + booknumtxt.Text + "'";
SqlCommand MyCommand1 = new SqlCommand(MySQL1,cn);
SqlDataReader sdr = MyCommand1.ExecuteReader();
sdr.Read();
booknametxt.Text = sdr[0].ToString();
booknumtxt.Text = sdr[1].ToString();
writertxt.Text=sdr[4].ToString();
pricetxt.Text=sdr[3].ToString();
typetxt.Text=sdr[2].ToString();
presstxt.Text=sdr[5].ToString();
presstimetxt.Text=sdr[6].ToString();
placetxt.Text=sdr[7].ToString();
sdr.Close();
update.Enabled = true;
delete.Enabled = true ;
}
else
{
Response.Write("<Script Language=JavaScript> alert(\"不存在该编号,可以录入!\")</Script>");
insert.Enabled = true;
booknametxt.Text = "";
pricetxt.Text = "";
typetxt.Text = "";
writertxt.Text = "";
presstxt.Text = "";
presstimetxt.Text = "";
placetxt.Text = "";
}
cn.Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -