1、1.1 程序设计代码登录模块if(username.Text.Trim()=“|password.Text.Trim()=“)MessageBox.Show(“请输入用户名和密码“,“提示“);elseif (radioManage.Checked = true)string strcon = “Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“; /连接数据库的字符串,用于指定数据库地址,名称,账号,密码,连接方式SqlConnection sqlCon = new SqlConnection(strc
2、on); /实例化并定义一个数据库连接sqlCon.Open(); /打开数据库连接string sql = “select * from login where usernum=usernum and userpassword=suerpassword“; /定义要查询sql语句SqlCommand cmd = new SqlCommand(sql, sqlCon); /实例化并定义sql语句和数据库路径cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20); /定义cmd查询命令的字段属性,loginname sqldbtype nchar(2
3、0)cmd.Parameters.Add(“suerpassword“, SqlDbType.NChar, 20); /同上cmd.Parameters“usernum“.Value = username.Text; /将username中的text保存到变量loginnamecmd.Parameters“suerpassword“.Value = password.Text; /同上SqlDataReader dr = cmd.ExecuteReader();if (dr.Read()this.Visible=false; Form2 Formmain = new Form2(); /应该是
4、实例化一个主窗体的this.Hide(); /应该是切换到主窗口的或关闭自己的Formmain.Show(); /应该是打开一个主窗体的dr.Close();/关闭dr的数据库连接else / if (dr.Read()读取失败则执行如下代码MessageBox.Show(“密码错误,请重新输入!“); /显示提示信息else if (radioPerson.Checked=true)string strcon = “Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“; /连接数据库的字符串,用于指定数据
5、库地址,名称,账号,密码,连接方式SqlConnection sqlCon = new SqlConnection(strcon); /实例化并定义一个数据库连接sqlCon.Open(); /打开数据库连接string sql1 = “select * from reader where usernum=usernum and userpassword=suerpassword“; /定义要查询sql语句SqlCommand cmd1 = new SqlCommand(sql1, sqlCon); /实例化并定义sql语句和数据库路径cmd1.Parameters.Add(“usernum“
6、, SqlDbType.NChar, 20); /定义cmd查询命令的字段属性,loginname sqldbtype nchar(20)cmd1.Parameters.Add(“suerpassword“, SqlDbType.NChar, 20); /同上cmd1.Parameters“usernum“.Value = username.Text; /将username中的text保存到变量loginnamecmd1.Parameters“suerpassword“.Value = password.Text; /同上cmd1.CommandText=sql1;SqlDataReader
7、dr = cmd1.ExecuteReader();if (dr.Read()this.Visible=false;Form9 Formmain = new Form9(); /应该是实例化一个主窗体的this.Hide(); /应该是切换到主窗口的或关闭自己的dr.Close();/关闭dr的数据库连接Formmain.Show(); /应该是打开一个主窗体的elseMessageBox.Show(“用户名或密码错误“,“警告“);elseMessageBox.Show(“没有选择角色“, “提示“);添加图书代码SqlConnection sqlcon = new SqlConnectio
8、n(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);string str = “insert into dbo.book values(bnum,bname,bauthor,bpublic,bclasses,benshu)“;sqlcon.Open();SqlCommand cmd = new SqlCommand(str, sqlcon);cmd.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bname“, Sql
9、DbType.NChar, 20);cmd.Parameters.Add(“bauthor“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bpublic“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bclasses“, SqlDbType.NChar, 20);cmd.Parameters.Add(“benshu“, SqlDbType.TinyInt);cmd.Parameters“bnum“.Value = bnum.Text;cmd.Parameters“bname“.Value = bname.Text;
10、cmd.Parameters“bauthor“.Value = bauthor.Text;cmd.Parameters“bpublic“.Value = bpublic.Text;cmd.Parameters“bclasses“.Value = bclasses.Text;cmd.Parameters“benshu“.Value = benshu.Text;cmd.ExecuteNonQuery();sqlcon.Close();this.bnum.Clear();this.bname.Clear();this.bauthor.Clear();this.bpublic.Clear();this
11、.benshu.Clear();this.bclasses.Clear();MessageBox.Show(“添加成功!“);删除图书代码SqlConnection sqlcon = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True“);sqlcon.Open();string str = “delete from book where bnum=bnum“; SqlCommand cmd = new SqlCommand(str, sqlcon);cmd.Parame
12、ters.Add(“bnum“, SqlDbType.NChar, 20);cmd.Parameters“bnum“.Value = this.tnum.Text;cmd.ExecuteNonQuery();sqlcon.Close();this.tnum.Clear();this.tno.Clear();this.tname.Clear();this.tpublisher.Clear();this.tauthor.Clear();this.tclasses.Clear();this.tbenshu.Clear();MessageBox.Show(“删除成功!“)查询图书代码SqlConnec
13、tion con = new SqlConnection();/建立数据库连接 con.ConnectionString = “Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“;con.Open();/打开连接SqlCommand cmd = new SqlCommand(“select * from book where bname=bname“, con);cmd.Parameters.Add(“bname“, SqlDbType.NChar, 20);cmd.Parameters“bname“.V
14、alue = bookname.Text;SqlDataAdapter da = new SqlDataAdapter(cmd);DataTable dt = new DataTable(“图书记录表“);da.TableMappings.Add(“BorrowRecord“, “借阅记录表“);da.TableMappings0.ColumnMappings.Add(“bnum“, “图书号“);da.TableMappings0.ColumnMappings.Add(“bname“, “图书名“);da.TableMappings0.ColumnMappings.Add(“bauthor“
15、, “作者“);da.TableMappings0.ColumnMappings.Add(“bpublic“, “出版社“);da.TableMappings0.ColumnMappings.Add(“bclasses“, “类别“);da.TableMappings0.ColumnMappings.Add(“benshu“, “本数“);da.Fill(dt);dataGridView1.DataSource = dt;con.Close();图书更新代码SqlConnection con1 = new SqlConnection(“Data Source=SIMON-VAIO;Initia
16、l Catalog=lkl2;Integrated Security=True;“);con1.Open();SqlCommand cmd1 = new SqlCommand(“update book set bname=bname,bauthor=bauthor,bpublic=bpublic,bclasses=bclasses,benshu=benshu where bnum=bnum;“, con1);cmd1.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd1.Parameters.Add(“bname“, SqlDbType.NChar,
17、 20);cmd1.Parameters.Add(“bauthor“, SqlDbType.NChar, 20);cmd1.Parameters.Add(“bpublic“, SqlDbType.NChar, 20);cmd1.Parameters.Add(“bclasses“, SqlDbType.NChar, 20);cmd1.Parameters.Add(“benshu“, SqlDbType.NChar, 20);cmd1.Parameters“bnum“.Value = cnum.Text;cmd1.Parameters“bname“.Value = cname.Text;cmd1.
18、Parameters“bauthor“.Value = cauthor.Text;cmd1.Parameters“bpublic“.Value = cpublic.Text;cmd1.Parameters“bclasses“.Value = cclasses.Text;cmd1.Parameters“benshu“.Value = cbenshu.Text;cmd1.ExecuteNonQuery();con1.Close();um.Clear();ame.Clear();this.cauthor.Clear();this.cpublic.Clear();this.cclasses.Clear
19、();this.cbenshu.Clear();MessageBox.Show(“更新成功!“);添加用户代码SqlConnection sqlcon = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);string str = “insert into dbo.reader values(usernum,username,userpassword,usertype)“;sqlcon.Open();SqlCommand cmd = new SqlCommand(
20、str, sqlcon);cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“username“, SqlDbType.NChar, 20);cmd.Parameters.Add(“userpassword“, SqlDbType.NChar, 20);cmd.Parameters.Add(“usertype“, SqlDbType.NChar, 20);cmd.Parameters“usernum“.Value =usernum.Text;cmd.Parameters“username“.Value =
21、 username.Text;cmd.Parameters“userpassword“.Value = userpassword.Text;cmd.Parameters“usertype“.Value = usertype.Text;cmd.ExecuteNonQuery();sqlcon.Close();this.usernum.Clear();this.username.Clear();this.userpassword.Clear();this.usertype.Clear();MessageBox.Show(“用户添加成功!“删除用户代码SqlConnection sqlcon = n
22、ew SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True“);sqlcon.Open();string str = “delete from reader where usernum=usernum“;SqlCommand cmd = new SqlCommand(str, sqlcon);cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd.Parameters“usernum“.Value = this.num.Text
23、;cmd.ExecuteNonQuery();sqlcon.Close();MessageBox.Show(“删除成功!“);修改密码代码SqlConnection con1 = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);con1.Open();SqlCommand cmd1 = new SqlCommand(“update reader set userpassword=userpassword where usernum=usernum;“, con1
24、);cmd1.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd1.Parameters.Add(“userpassword“, SqlDbType.NChar, 20);cmd1.Parameters“usernum“.Value = cusernum.Text;cmd1.Parameters“userpassword“.Value = cuserpassword.Text;cmd1.ExecuteNonQuery();con1.Close();this.cusernum.Clear();this.cuserpassword.Clear();
25、MessageBox.Show(“修改成功请从新登陆!“);完善读者信息代码SqlConnection sqlcon = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);string str = “insert into dbo.readerifo values(usernum,username,usersex,usergrade,telephone)“;sqlcon.Open();SqlCommand cmd = new SqlCommand(str, sql
26、con);cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“username“, SqlDbType.NChar, 20);cmd.Parameters.Add(“usersex“, SqlDbType.NChar, 20);cmd.Parameters.Add(“usergrade“, SqlDbType.NChar, 20);cmd.Parameters.Add(“telephone“, SqlDbType.NChar, 20);cmd.Parameters“usernum“.Value = use
27、rnum.Text;cmd.Parameters“username“.Value = username.Text;cmd.Parameters“usersex“.Value = usersex.Text;cmd.Parameters“usergrade“.Value = usergrade.Text;cmd.Parameters“telephone“.Value =telephone.Text;cmd.ExecuteNonQuery();sqlcon.Close();this.usernum.Clear();this.username.Clear();this.userpassword.Cle
28、ar();this.usertype.Clear();this.usersex.Clear();this.usergrade.Clear();this.telephone.Clear();MessageBox.Show(“添加信息成功!“);检索用户信息代码if (num.Text = “)MessageBox.Show(“请输入学生学号!“);elsestring strcon = “Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True“;SqlConnection sqlCon = new SqlConne
29、ction(strcon);sqlCon.Open();string sql = “select * from readerifo where usernum=usernum “;SqlCommand cmd = new SqlCommand(sql, sqlCon);cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd.Parameters“usernum“.Value = num.Text;/创建 SqlDataReader,必须调用 SqlCommand 对象的 ExecuteReader 方法,而不要直接使用构造函数。SqlDat
30、aReader dr = cmd.ExecuteReader();if (dr.Read()this.usernum.Text = dr“usernum“.ToString().Trim();this.username.Text = dr“username“.ToString().Trim();this.usersex.Text = dr“usersex“.ToString().Trim();this.usergrade.Text = dr“usergrade“.ToString().Trim();this.telephone.Text = dr“telephone“.ToString().T
31、rim();MessageBox.Show(“欢迎光临本图书馆来借书!“);sqlCon.Close();借阅图书代码SqlConnection sqlcon = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);string str = “insert into dbo.borrowifo values(bnum,bname,bauthor,bpublic,bclasses,benshu,btime,usernum,username,usersex,usergr
32、ade,telephone)“;sqlcon.Open();SqlCommand cmd = new SqlCommand(str, sqlcon);cmd.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bname“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bauthor“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bpublic“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bclass
33、es“, SqlDbType.NChar, 20);cmd.Parameters.Add(“benshu“, SqlDbType.TinyInt);cmd.Parameters.Add(“btime“, SqlDbType.Date);cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“username“, SqlDbType.NChar, 20);cmd.Parameters.Add(“usersex“, SqlDbType.NChar, 20);cmd.Parameters.Add(“usergrad
34、e“, SqlDbType.NChar, 20);cmd.Parameters.Add(“telephone“, SqlDbType.NChar, 20);cmd.Parameters“bnum“.Value = tno.Text;cmd.Parameters“bname“.Value = tname.Text;cmd.Parameters“bauthor“.Value = tauthor.Text;cmd.Parameters“bpublic“.Value = tpublisher.Text;cmd.Parameters“bclasses“.Value = tclasses.Text;cmd
35、.Parameters“benshu“.Value = “1“;cmd.Parameters“btime“.Value = System.DateTime.Now;cmd.Parameters“usernum“.Value = usernum.Text;cmd.Parameters“username“.Value = username.Text;cmd.Parameters“usersex“.Value = usersex.Text;cmd.Parameters“usergrade“.Value = usergrade.Text;cmd.Parameters.Add(new SqlParame
36、ter(“borrowtime“, DateTime.Now);cmd.Parameters“telephone“.Value = telephone.Text;cmd.ExecuteNonQuery();sqlcon.Close();SqlConnection con1 = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);con1.Open();SqlCommand cmd1 = new SqlCommand(“update book set benshu=b
37、enshu-1 where bnum=bnum“, con1);cmd1.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd1.Parameters“bnum“.Value = tnum.Text;cmd1.ExecuteNonQuery();con1.Close();this.tnum.Clear();this.tno.Clear();this.tname.Clear();this.tauthor.Clear();this.tpublisher.Clear();this.tclasses.Clear();this.num.Clear();this.
38、usernum.Clear();this.username.Clear();this.usersex.Clear();this.usergrade.Clear();this.tbenshu.Clear();this.telephone.Clear();MessageBox.Show(“借阅成功!“);归还图书代码SqlConnection sqlcon = new SqlConnection(“Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“);string str = “insert into dbo
39、.returnifo values(bnum,bname,bauthor,bpublic,bclasses,benshu,rtime,usernum,username,usersex,usergrade,telephone)“;sqlcon.Open();SqlCommand cmd = new SqlCommand(str, sqlcon);cmd.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bname“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bauthor“,
40、SqlDbType.NChar, 20);cmd.Parameters.Add(“bpublic“, SqlDbType.NChar, 20);cmd.Parameters.Add(“bclasses“, SqlDbType.NChar, 20);cmd.Parameters.Add(“benshu“, SqlDbType.TinyInt);cmd.Parameters.Add(“rtime“, SqlDbType.Date);cmd.Parameters.Add(“usernum“, SqlDbType.NChar, 20);cmd.Parameters.Add(“username“, Sq
41、lDbType.NChar, 20);cmd.Parameters.Add(“usersex“, SqlDbType.NChar, 20);cmd.Parameters.Add(“usergrade“, SqlDbType.NChar, 20);cmd.Parameters.Add(“telephone“, SqlDbType.NChar, 20);cmd.Parameters“bnum“.Value = sno.Text;cmd.Parameters“bname“.Value = sname.Text;cmd.Parameters“bauthor“.Value = sauthor.Text;
42、cmd.Parameters“bpublic“.Value = spublisher.Text;cmd.Parameters“bclasses“.Value = sclasses.Text;cmd.Parameters“benshu“.Value = sbenshu.Text;cmd.Parameters“rtime“.Value = System.DateTime.Now;cmd.Parameters“usernum“.Value =usernum.Text;cmd.Parameters“username“.Value = username.Text; cmd.Parameters“user
43、sex“.Value = usersex.Text;cmd.Parameters“usergrade“.Value = usergrade.Text;cmd.Parameters.Add(new SqlParameter(“returntime“, DateTime.Now);cmd.Parameters“telephone“.Value = telephone.Text;cmd.ExecuteNonQuery();sqlcon.Close();SqlConnection con1 = new SqlConnection(“Data Source=SIMON-VAIO;Initial Cata
44、log=lkl2;Integrated Security=True;“);con1.Open();SqlCommand cmd1 = new SqlCommand(“update book set benshu=benshu+1 where bnum=bnum“, con1);cmd1.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd1.Parameters“bnum“.Value = tnum.Text;cmd1.ExecuteNonQuery();con1.Close();this.tnum.Clear();this.sno.Clear();t
45、his.sname.Clear();this.sauthor.Clear();this.spublisher.Clear();this.sclasses.Clear();this.sbenshu.Clear();this.num.Clear();this.usernum.Clear();this.username.Clear();this.usersex.Clear();this.usergrade.Clear();this.telephone.Clear();MessageBox.Show(“还书成功!“);查询借阅归还图书信息代码if (booknum.Text = “)MessageBo
46、x.Show(“读者信息不能为空!“, “提示信息“, MessageBoxButtons.OK, MessageBoxIcon.Error);elseSqlConnection con = new SqlConnection();/建立数据库连接 con.ConnectionString = “Data Source=SIMON-VAIO;Initial Catalog=lkl2;Integrated Security=True;“;con.Open();/打开连接SqlCommand cmd = new SqlCommand(“select *,borrow as statu from d
47、bo.borrowifo where bnum=bnum union select *,return as statu from dbo.returnifo where bnum=bnum;“, con);cmd.Parameters.Add(“bnum“, SqlDbType.NChar, 20);cmd.Parameters“bnum“.Value = booknum.Text;SqlDataAdapter da = new SqlDataAdapter(cmd);DataTable dt = new DataTable(“借阅记录表“);da.TableMappings.Add(“Bor
48、rowRecord“, “借阅记录表“);da.TableMappings0.ColumnMappings.Add(“usernum“, “借阅证号“);da.TableMappings0.ColumnMappings.Add(“username“, “姓名“);da.TableMappings0.ColumnMappings.Add(“usersex“, “性别“);da.TableMappings0.ColumnMappings.Add(“usergrade“, “班级“);da.TableMappings0.ColumnMappings.Add(“bname“, “图书名“);da.TableMappings0.ColumnMappings.Add(“bauthor“, “作者“);da.TableMappings0.ColumnMappings.Add(“bnum“, “图书号“);da.TableMappings0.ColumnMappings.Add(“bpublic“, “出版社“);da.TableMappings