📄 borrow.cs
字号:
this.groupBox1.Location = new System.Drawing.Point(8, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(616, 80);
this.groupBox1.TabIndex = 12;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "书籍借阅";
//
// da1
//
this.da1.InsertCommand = this.sqlInsertCommand1;
this.da1.SelectCommand = this.sqlSelectCommand1;
this.da1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "读者借阅", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("证号", "证号"),
new System.Data.Common.DataColumnMapping("读者姓名", "读者姓名"),
new System.Data.Common.DataColumnMapping("已借书", "已借书"),
new System.Data.Common.DataColumnMapping("书号", "书号"),
new System.Data.Common.DataColumnMapping("书名", "书名"),
new System.Data.Common.DataColumnMapping("借阅时间", "借阅时间"),
new System.Data.Common.DataColumnMapping("应还时间", "应还时间"),
new System.Data.Common.DataColumnMapping("状态", "状态")})});
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=YXK;packet size=4096;integrated security=SSPI;data source=YXK;pers" +
"ist security info=False;initial catalog=library";
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 证号, 读者姓名, 已借书, 书号, 书名, 借阅时间, 应还时间, 状态 FROM 读者借阅 WHERE (证号 LIKE @Param5) AND (书号 LIKE @Param6)";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param5", System.Data.SqlDbType.VarChar, 10, "证号"));
this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param6", System.Data.SqlDbType.VarChar, 20, "书号"));
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO 读者借阅(证号, 读者姓名, 已借书, 书号, 书名, 借阅时间, 应还时间, 状态) VALUES (@证号, @读者姓名, @已借书," +
" @书号, @书名, @借阅时间, @应还时间, @状态); SELECT 证号, 读者姓名, 已借书, 书号, 书名, 借阅时间, 应还时间, 状态 FROM" +
" 读者借阅";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@证号", System.Data.SqlDbType.VarChar, 10, "证号"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@读者姓名", System.Data.SqlDbType.VarChar, 20, "读者姓名"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@已借书", System.Data.SqlDbType.Int, 4, "已借书"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@书号", System.Data.SqlDbType.VarChar, 20, "书号"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@书名", System.Data.SqlDbType.VarChar, 100, "书名"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@借阅时间", System.Data.SqlDbType.DateTime, 4, "借阅时间"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@应还时间", System.Data.SqlDbType.DateTime, 4, "应还时间"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@状态", System.Data.SqlDbType.VarChar, 50, "状态"));
//
// Borrow
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(632, 446);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.toolBar1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Borrow";
this.Text = "图书借阅";
this.Load += new System.EventHandler(this.Borrow_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
//------------初始化窗体时,填充数据-------------
private void Borrow_Load(object sender, System.EventArgs e)
{
txt1.Focus();
txt4.Enabled=false;
tBtnSubmit.Enabled=false;
cmOrders=(CurrencyManager) BindingContext[dataSet21,"读者借阅"];
}
//------------查询并读入读者信息以及读者已经借书的信息------------
private void txt1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13&&txt1.Text.Trim()!="")
{
txt2.Clear();
txt3.Clear();
this.showInfo();//显示读者信息
txt4.Focus();//光标移动到输入图书编号文本框处,开始借书
}
}
//--------------按回车借书---------------
private void txt4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13&&txt4.Text.Trim()!="")
{
try
{
this.borrowBook();//读者借书
this.Borrowed();
}
catch
{
MessageBox.Show("查询错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
}
//--------处理借书事务----------
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button.ToolTipText=="确定借书")
{
string strConn = "workstation id=localhost;Integrated Security=SSPI;database=library";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="exec sf_图书借阅 '"+txt1.Text.Trim()+"','"+txt4.Text.Trim()+"'";
cmd.ExecuteNonQuery();
da1.SelectCommand.Parameters[0].Value="%%";
da1.SelectCommand.Parameters[1].Value="%%";
dataSet21.读者借阅.Clear();
da1.Fill(dataSet21);
this.useEnabled();
}
//---------------------------取消借书-----------------------------------
if(e.Button.ToolTipText=="取消借书")
{
this.useEnabled();
}
//------------------------退出图书借阅界面---------------------------------------
if(e.Button.ToolTipText=="退出")
{
this.Close();
}
}
//-----------------------------读者借书---------------------------------------
private void borrowBook()
{
string strConn = "workstation id=localhost;Integrated Security=SSPI;database=library";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select 书号 from 图书信息 where 书号='"+txt4.Text.Trim()+"'";
SqlDataReader dr=cmd.ExecuteReader();//执行查询,并读入图书数据
dr.Read();
if(dr.HasRows==false)//如未能查询到图书信息,则提示重新输入
{
MessageBox.Show("无此图书,请检查后重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
dr.Close();
}
//------------------------------读者是否已借阅此书-------------------------------------
private void Borrowed()
{
string strConn = "workstation id=localhost;Integrated Security=SSPI;database=library";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select 证号,书号 from 图书借阅 where (书号='"+txt4.Text.Trim()+"') and (证号='"+txt1.Text.Trim()+"') or ((证号='"+txt1.Text.Trim()+"') and (状态= '超期'))";
SqlDataReader dr=cmd.ExecuteReader();//执行查询,并读入图书数据
dr.Read();
if(dr.HasRows==true)//如查询到图书信息,则提示重新输入
{
MessageBox.Show("该读者已经借有此书或有图书超期,不能再借","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
dr.Close();
txt4.Enabled=false;
tBtnSubmit.Enabled=true;
}
//------------显示读者信息和所借图书信息----------------
private void showInfo()
{
string strConn = "workstation id=localhost;Integrated Security=SSPI;database=library";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select 读者姓名, 已借书 from 读者信息 where (证号='"+txt1.Text.Trim()+"')";
SqlDataReader dr=cmd.ExecuteReader();//执行查询,并读入读者数据
dr.Read();
if(dr.HasRows==false)//如未能查询到读者信息,则提示重新输入
{
MessageBox.Show("无此读者,请检查后重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
else
{
txt2.Text=dr.GetValue(0).ToString().Trim();//显示读者信息
txt3.Text=dr.GetValue(1).ToString().Trim();
txt1.Enabled=false;
txt4.Enabled=true;
}
dr.Close();
}
//------------防止用户错误操作的界面锁定-----------------------
private void useEnabled()
{
txt1.Clear();
txt2.Clear();
txt3.Clear();
txt4.Clear();
txt1.Enabled=true;
txt4.Enabled=false;
tBtnSubmit.Enabled=false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -