1、 SqlConnection dataConnection;/数据库连接类 SqlCommand dataCommand;/数据库命令类 SqlDataReader dataReader;/数据库读出类 String uID;/存放用户名的String类 String uPassD;/存放密码的String类 /构造函数 public Form1() InitializeComponent(); /关闭窗口 private void button2_Click(object sender, EventArgs e) dataConnection.Close(); this.Dispose();
2、 /载入窗口时连接数据库,进展初始化 private void Form1_Load(object sender, EventArgs e) /连接数据库 try dataConnection = new SqlConnection(); dataConnection.ConnectionString = Integrated Security=true;Initial Catalog=mySQL; + Data Source=wty-PC; /dataConnection.Open(); /捕获异常 catch (SqlException ee) MessageBox.Show(ee.Mes
3、sage); /数据库类之间的互相绑定 finally dataCommand = new SqlCommand(); dataCommand.Connection = dataConnection; /进展身份验证,并登录 private void button1_Click(object sender, EventArgs e) uID =textBox1.Text; uPassD =textBox2.Text; dataConnection.Open(); if (uID = ) MessageBox.Show(请输入用户名); else if(uID!= dataCommand.Com
4、mandText = (SELECT * FROM 用户 WHERE 用户名= + uID + dataReader = dataCommand.ExecuteReader(); if (!dataReader.Read()没有此用户! textBox1.Text = textBox2.Text = null; else if (uPassD != dataReader.GetString(1)密码错误,请重新输入! textBox2.Text = else if (radioButton2.Checked) if (dataReader.GetString(2) = 读者你没有管理员权限!
5、dataReader.Close(); else if (dataReader.GetString(2) = 管理员 Form3 frm3 = new Form3(dataConnection, dataCommand, dataReader, uID); this.Hide(); frm3.ShowDialog(); else if (radioButton1.Checked) Form2 frm2 = new Form2(dataConnection, dataCommand, dataReader, uID); frm2.ShowDialog(); /按下ENTER键时,响应验证 pri
6、vate void textBox2_KeyDown(object sender, KeyEventArgs e) if (int)e.KeyCode = 13) this.button1_Click(sender, e);读者界面读者信息,显示你当前的一些信息,包括可借数量和预约数量等,并包括修改密码功能。查询,可以进展图书的查询,包括模糊查询和准确查询。通过radiobuttun来判断。模糊查询不支持ISBN的查询。选中查询结果的行头,自动填充到借阅/预约界面借阅/预约,这里的textbox都设置为readonly,主要是为了防止出现图书馆中没有的书进展借阅,这样可以节省查询的时间,并且通
7、过查询界面可以很方便的填入这两项信息。借阅和预约分别是计入相应的表内。并可以查询归还界面,同样,textbox设为readonly,通过查询借书或预约情况,选中行头来填入信息。归还或取消预约好删除相应元组。注销界面注销是返回登录界面重新登陆退出是退出程序。 public partial class Form2 : /SqlDataReader dataReader; Int32 ynum=0; Int32 lnum=0;/在馆数量 Int32 maxnum = 0; Int32 dnum = 0; Int32 ymaxnum = 0; Int32 ydnum = 0; bool bflag =
8、 true; bool yflag = false; /String bISBN;/存放ISBN编号 /String bName;/存放书名 /Int32 bNum;/图书数量 /Int32 lNum; public Form2() /传递参数的构造函数 public Form2(SqlConnection conn, SqlCommand comm, SqlDataReader read, String ID) dataConnection = new SqlConnection (); dataConnection = conn; dataCommand = comm; /dataRead
9、er = read; uID = ID; /注销,返回登录界面 Form1 frm1 = new Form1(); frm1.ShowDialog(); /退出系统 /清空所有textBox中的内容 private void button4_Click(object sender, EventArgs e) this.textBox1.Text = this.textBox2.Text = this.dataGridView1.DataSource = null; /查询图书 private void button3_Click(object sender, EventArgs e) Stri
10、ng bookname= String bookisbn= String bmark = if(textBox1.Text! bookisbn = (ISBN编号= + textBox1.Text + if (textBox2.Text != ) & (textBox1.Text!) bookname = (AND 书名= + textBox2.Text + else if(textBox2.Text !书名= bmark = bookname + bookisbn; /添加准确查询的代码 if(bmark! dataCommand.CommandText = SELECT * FROM bo
11、ok WHERE + bmark; else if(bmark=) 请输入查询数据! if (textBox1.Text !不支持ISBN的模糊查询! /添加模糊查询的代码 if (textBox2.Text !书名like % + bookname; else/查询所有图书SELECT * FROM book请选择查询方式! SqlDataReader dataReader = dataCommand.ExecuteReader(); BindingSource bs = new BindingSource(); bs.DataSource = dataReader; this.dataGr
12、idView1.DataSource = bs; if (dataReader.Read() lnum = dataReader.GetInt32(3); ynum = dataReader.GetInt32(6); /借书 private void button5_Click(object sender, EventArgs e) String bookname = String bookisbn = SELECT * FROM borrow WHERE 读者号= SqlDataReader tempReader = dataCommand.ExecuteReader(); if (text
13、Box3.Text ! (textBox4.Text ! bookisbn = textBox3.Text; bookname = textBox4.Text; bool flag = true; if (tempReader.Read() if (bookisbn = tempReader.GetString(1)你已经借了此书了! flag = false; tempReader.Close(); else if(flag =true) if (lnum=0)在馆数量为零,不可借阅 else if(maxnum=dnum)你的借书数量已满,无法再借!SELECT * FROM border
14、 WHERE 读者号= tempReader = dataCommand.ExecuteReader();你已经预约此书了,该操作将会删除你的预约记录!DELETE FROM border WHERE ISBN编号= + bookisbn + dataCommand.ExecuteNonQuery();INSERT INTO borrow(读者号,ISBN编号) VALUES (,)UPDATE book SET 在馆数量=在馆数量-1 WHERE ISBN编号=UPDATE book SET 可预约数量=可预约数量+1 WHERE ISBN编号=UPDATE reader SET 已借数量=
15、已借数量+1 WHERE 读者号=借阅成功! dnum+; textBox3.Text = textBox4.Text = /tempreader_1.Close(); SELECT * FROM BORROW WHERE 读者号=+uID+ this.dataGridView2.DataSource = bs; /将所选图书信息传递至借书模块 private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)ISBN编号.Value.ToString(); textBo
16、x4.Text = dataGridView1.Rowsdata书名 lnum = (int在馆数量.Value; ynum = (int可预约数量 /查询借书情况 private void button7_Click(object sender, EventArgs e) private void button8_Click(object sender, EventArgs e) this.dataGridView3.DataSource = bs; bflag = true; yflag = false; /传递还书信息 private void dataGridView3_RowHead
17、erMouseClick(object sender, DataGridViewCellMouseEventArgs e) if(bflag=true&yflag=false) else if(yflag=true&bflag=false) /还书 private void button9_Click(object sender, EventArgs e) String bookisbn; bookisbn = textBox5.Text; if (bookisbn !DELETE FROM borrow WHERE ISBN编号=UPDATE book SET 在馆数量=在馆数量+1 WHERE ISBN编号=UPDATE book SET 可预约数量=可预约数量-1 WHERE ISBN编号=UPDATE reader SET 已借数量=已借数量-1 WHERE 读者号= textBox5.Text = 归还成功! dnum-; /预约 private void button6_Click(object sender, EventArgs e) String
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1