图书馆管理系统程序设计代码.docx
《图书馆管理系统程序设计代码.docx》由会员分享,可在线阅读,更多相关《图书馆管理系统程序设计代码.docx(17页珍藏版)》请在冰豆网上搜索。
图书馆管理系统程序设计代码
1.1程序设计代码
登录模块
if(username.Text.Trim()==""||password.Text.Trim()=="")
MessageBox.Show("请输入用户名和密码","提示");
else
{
if(radioManage.Checked==true)
{
stringstrcon="DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;";//连接数据库的字符串,用于指定数据库地址,名称,账号,密码,连接方式
SqlConnectionsqlCon=newSqlConnection(strcon);//实例化并定义一个数据库连接
sqlCon.Open();//打开数据库连接
stringsql="select*fromloginwhereusernum=@usernumanduserpassword=@suerpassword";//定义要查询sql语句
SqlCommandcmd=newSqlCommand(sql,sqlCon);//实例化并定义sql语句和数据库路径
cmd.Parameters.Add("@usernum",SqlDbType.NChar,20);//定义cmd查询命令的字段属性,@loginnamesqldbtypenchar(20)
cmd.Parameters.Add("@suerpassword",SqlDbType.NChar,20);//同上
cmd.Parameters["@usernum"].Value=username.Text;//将username中的text保存到变量@loginname
cmd.Parameters["@suerpassword"].Value=password.Text;//同上
SqlDataReaderdr=cmd.ExecuteReader();
if(dr.Read())
{
this.Visible=false;
Form2Formmain=newForm2();//应该是实例化一个主窗体的
this.Hide();//应该是切换到主窗口的或关闭自己的
Formmain.Show();//应该是打开一个主窗体的
dr.Close();//关闭dr的数据库连接
}
else//if(dr.Read())读取失败则执行如下代码
MessageBox.Show("密码错误,请重新输入!
");//显示提示信息
}
elseif(radioPerson.Checked==true)
{
stringstrcon="DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;";//连接数据库的字符串,用于指定数据库地址,名称,账号,密码,连接方式
SqlConnectionsqlCon=newSqlConnection(strcon);//实例化并定义一个数据库连接
sqlCon.Open();//打开数据库连接
stringsql1="select*fromreaderwhereusernum=@usernumanduserpassword=@suerpassword";//定义要查询sql语句
SqlCommandcmd1=newSqlCommand(sql1,sqlCon);//实例化并定义sql语句和数据库路径
cmd1.Parameters.Add("@usernum",SqlDbType.NChar,20);//定义cmd查询命令的字段属性,@loginnamesqldbtypenchar(20)
cmd1.Parameters.Add("@suerpassword",SqlDbType.NChar,20);//同上
cmd1.Parameters["@usernum"].Value=username.Text;//将username中的text保存到变量@loginname
cmd1.Parameters["@suerpassword"].Value=password.Text;//同上
cmd1.CommandText=sql1;
SqlDataReaderdr=cmd1.ExecuteReader();
if(dr.Read())
{
this.Visible=false;
Form9Formmain=newForm9();//应该是实例化一个主窗体的
this.Hide();//应该是切换到主窗口的或关闭自己的
dr.Close();//关闭dr的数据库连接
Formmain.Show();//应该是打开一个主窗体的
}
else
MessageBox.Show("用户名或密码错ª误","警告");
}
else
MessageBox.Show("没有选择角色","提示");
}
}
添加图书代码
SqlConnectionsqlcon=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;");
stringstr="insertintodbo.bookvalues(@bnum,@bname,@bauthor,@bpublic,@bclasses,@benshu)";
sqlcon.Open();
SqlCommandcmd=newSqlCommand(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("@bclasses",SqlDbType.NChar,20);
cmd.Parameters.Add("@benshu",SqlDbType.TinyInt);
cmd.Parameters["@bnum"].Value=bnum.Text;
cmd.Parameters["@bname"].Value=bname.Text;
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.benshu.Clear();
this.bclasses.Clear();
MessageBox.Show("添加成功!
");
删除图书代码
SqlConnectionsqlcon=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True");
sqlcon.Open();
stringstr="deletefrombookwherebnum=@bnum";
SqlCommandcmd=newSqlCommand(str,sqlcon);
cmd.Parameters.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("删除成功!
")
查询图书代码
SqlConnectioncon=newSqlConnection();//建立数据库连接
con.ConnectionString="DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;";
con.Open();//打开连接
SqlCommandcmd=newSqlCommand("select*frombookwherebname=@bname",con);
cmd.Parameters.Add("@bname",SqlDbType.NChar,20);
cmd.Parameters["@bname"].Value=bookname.Text;
SqlDataAdapterda=newSqlDataAdapter(cmd);
DataTabledt=newDataTable("图书记录表");
da.TableMappings.Add("BorrowRecord","借阅记录表");
da.TableMappings[0].ColumnMappings.Add("bnum","图书号");
da.TableMappings[0].ColumnMappings.Add("bname","图书名");
da.TableMappings[0].ColumnMappings.Add("bauthor","作者");
da.TableMappings[0].ColumnMappings.Add("bpublic","出版社");
da.TableMappings[0].ColumnMappings.Add("bclasses","类别");
da.TableMappings[0].ColumnMappings.Add("benshu","本数");
da.Fill(dt);
dataGridView1.DataSource=dt;
con.Close();
图书更新代码
SqlConnectioncon1=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;");
con1.Open();
SqlCommandcmd1=newSqlCommand("updatebooksetbname=@bname,bauthor=@bauthor,bpublic=@bpublic,bclasses=@bclasses,benshu=@benshuwherebnum=@bnum;",con1);
cmd1.Parameters.Add("@bnum",SqlDbType.NChar,20);
cmd1.Parameters.Add("@bname",SqlDbType.NChar,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.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();
this.cbenshu.Clear();
MessageBox.Show("更新成功!
");
添加用户代码
SqlConnectionsqlcon=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;");
stringstr="insertintodbo.readervalues(@usernum,@username,@userpassword,@usertype)";
sqlcon.Open();
SqlCommandcmd=newSqlCommand(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=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("用户添加成功!
"
删除用户代码
SqlConnectionsqlcon=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True");
sqlcon.Open();
stringstr="deletefromreaderwhereusernum=@usernum";
SqlCommandcmd=newSqlCommand(str,sqlcon);
cmd.Parameters.Add("@usernum",SqlDbType.NChar,20);
cmd.Parameters["@usernum"].Value=this.num.Text;
cmd.ExecuteNonQuery();
sqlcon.Close();
MessageBox.Show("删除成功!
");
修改密码代码
SqlConnectioncon1=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;");
con1.Open();
SqlCommandcmd1=newSqlCommand("updatereadersetuserpassword=@userpasswordwhereusernum=@usernum;",con1);
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();
MessageBox.Show("修改成功请从新登陆!
");
完善读者信息代码
SqlConnectionsqlcon=newSqlConnection("DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True;");
stringstr="insertintodbo.readerifovalues(@usernum,@username,@usersex,@usergrade,@telephone)";
sqlcon.Open();
SqlCommandcmd=newSqlCommand(str,sqlcon);
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=usernum.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.Clear();
this.usertype.Clear();
this.usersex.Clear();
this.usergrade.Clear();
this.telephone.Clear();
MessageBox.Show("添加信息成功!
");
检索用户信息代码
if(num.Text=="")
{
MessageBox.Show("请输入学生学号!
");
}
else
{
stringstrcon="DataSource=SIMON-VAIO;InitialCatalog=lkl2;IntegratedSecurity=True";
SqlConnectionsqlCon=newSqlConnection(strcon);
sqlCon.Open();
stringsql="select*fromreaderifowhereusernum=@usernum";
SqlCommandcmd=newSqlCommand(sql,sqlCon);
cmd.Parameters.Add("@usernum",SqlDbType.NChar,20);
cmd.Parameters["@usernum"].Value=num.Text;
//创建SqlDataReader,必须调用SqlCommand对象的ExecuteReader方法,而不要直接使用构造函数。
SqlDataReaderdr=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().Trim();