BookBorrow.cs
资源名称:VCSDB.rar [点击查看]
上传用户:hjieqiu
上传日期:2013-05-11
资源大小:16494k
文件大小:19k
源码类别:
企业管理
开发平台:
C#
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- namespace 图书馆管理系统
- {
- /// <summary>
- /// BookBorrow 的摘要说明。
- /// </summary>
- public class BookBorrow : System.Windows.Forms.Form
- {
- private int totalCount,thisCount;//储存已借书和本次借书数据
- private DataTable tblBooks;
- private string readerID;//保存查询到的读者编号
- private System.Windows.Forms.ImageList imageList1;
- private System.Windows.Forms.ToolBar toolBar1;
- private System.Windows.Forms.ToolBarButton tBtnSubmit;
- private System.Windows.Forms.ToolBarButton tBtnCancel;
- private System.Windows.Forms.ToolBarButton tBtnQuit;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.DataGrid dataGrid1;
- private System.Windows.Forms.RadioButton rbt1;
- private System.Windows.Forms.RadioButton rbt2;
- private System.Windows.Forms.RadioButton rbt4;
- private System.Windows.Forms.RadioButton rbt3;
- private System.Windows.Forms.TextBox txt1;
- private System.Windows.Forms.TextBox txt2;
- private System.Windows.Forms.TextBox txt3;
- private System.Windows.Forms.TextBox txt4;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Label label5;
- private System.Windows.Forms.Label label6;
- private System.Windows.Forms.TextBox txt5;
- private System.Windows.Forms.Label label7;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.ComponentModel.IContainer components;
- public BookBorrow()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(BookBorrow));
- this.imageList1 = new System.Windows.Forms.ImageList(this.components);
- this.toolBar1 = new System.Windows.Forms.ToolBar();
- this.tBtnSubmit = new System.Windows.Forms.ToolBarButton();
- this.tBtnCancel = new System.Windows.Forms.ToolBarButton();
- this.tBtnQuit = new System.Windows.Forms.ToolBarButton();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.label3 = new System.Windows.Forms.Label();
- this.txt1 = new System.Windows.Forms.TextBox();
- this.rbt1 = new System.Windows.Forms.RadioButton();
- this.rbt2 = new System.Windows.Forms.RadioButton();
- this.txt2 = new System.Windows.Forms.TextBox();
- this.txt3 = new System.Windows.Forms.TextBox();
- this.txt4 = new System.Windows.Forms.TextBox();
- this.label4 = new System.Windows.Forms.Label();
- this.label5 = new System.Windows.Forms.Label();
- this.label6 = new System.Windows.Forms.Label();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.rbt4 = new System.Windows.Forms.RadioButton();
- this.rbt3 = new System.Windows.Forms.RadioButton();
- this.txt5 = new System.Windows.Forms.TextBox();
- this.label7 = new System.Windows.Forms.Label();
- this.dataGrid1 = new System.Windows.Forms.DataGrid();
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
- this.SuspendLayout();
- //
- // imageList1
- //
- this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
- this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
- this.imageList1.TransparentColor = System.Drawing.SystemColors.ControlLightLight;
- //
- // toolBar1
- //
- this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
- this.tBtnSubmit,
- this.tBtnCancel,
- this.tBtnQuit});
- this.toolBar1.DropDownArrows = true;
- this.toolBar1.ImageList = this.imageList1;
- this.toolBar1.Location = new System.Drawing.Point(0, 0);
- this.toolBar1.Name = "toolBar1";
- this.toolBar1.ShowToolTips = true;
- this.toolBar1.Size = new System.Drawing.Size(672, 41);
- this.toolBar1.TabIndex = 34;
- this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
- //
- // tBtnSubmit
- //
- this.tBtnSubmit.ImageIndex = 7;
- this.tBtnSubmit.Text = "确定借书";
- this.tBtnSubmit.ToolTipText = "确定借书";
- //
- // tBtnCancel
- //
- this.tBtnCancel.ImageIndex = 8;
- this.tBtnCancel.Text = "取消借书";
- this.tBtnCancel.ToolTipText = "取消借书";
- //
- // tBtnQuit
- //
- this.tBtnQuit.ImageIndex = 9;
- this.tBtnQuit.Text = "退出";
- this.tBtnQuit.ToolTipText = "退出";
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.label3);
- this.groupBox1.Controls.Add(this.txt1);
- this.groupBox1.Controls.Add(this.rbt1);
- this.groupBox1.Controls.Add(this.rbt2);
- this.groupBox1.Controls.Add(this.txt2);
- this.groupBox1.Controls.Add(this.txt3);
- this.groupBox1.Controls.Add(this.txt4);
- this.groupBox1.Controls.Add(this.label4);
- this.groupBox1.Controls.Add(this.label5);
- this.groupBox1.Controls.Add(this.label6);
- this.groupBox1.Location = new System.Drawing.Point(0, 40);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(672, 72);
- this.groupBox1.TabIndex = 35;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "读者信息";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(112, 24);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(100, 16);
- this.label3.TabIndex = 2;
- this.label3.Text = "输入后请回车";
- //
- // txt1
- //
- this.txt1.Location = new System.Drawing.Point(112, 40);
- this.txt1.Name = "txt1";
- this.txt1.Size = new System.Drawing.Size(136, 21);
- this.txt1.TabIndex = 1;
- this.txt1.Text = "";
- this.txt1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt1_KeyPress);
- //
- // rbt1
- //
- this.rbt1.Checked = true;
- this.rbt1.Location = new System.Drawing.Point(24, 16);
- this.rbt1.Name = "rbt1";
- this.rbt1.Size = new System.Drawing.Size(64, 24);
- this.rbt1.TabIndex = 0;
- this.rbt1.TabStop = true;
- this.rbt1.Text = "编号";
- //
- // rbt2
- //
- this.rbt2.Location = new System.Drawing.Point(24, 44);
- this.rbt2.Name = "rbt2";
- this.rbt2.Size = new System.Drawing.Size(64, 24);
- this.rbt2.TabIndex = 0;
- this.rbt2.Text = "条形码";
- //
- // txt2
- //
- this.txt2.Location = new System.Drawing.Point(280, 40);
- this.txt2.Name = "txt2";
- this.txt2.ReadOnly = true;
- this.txt2.TabIndex = 1;
- this.txt2.Text = "";
- //
- // txt3
- //
- this.txt3.Location = new System.Drawing.Point(408, 40);
- this.txt3.Name = "txt3";
- this.txt3.ReadOnly = true;
- this.txt3.TabIndex = 1;
- this.txt3.Text = "";
- //
- // txt4
- //
- this.txt4.Location = new System.Drawing.Point(536, 40);
- this.txt4.Name = "txt4";
- this.txt4.ReadOnly = true;
- this.txt4.TabIndex = 1;
- this.txt4.Text = "";
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(280, 24);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(100, 16);
- this.label4.TabIndex = 2;
- this.label4.Text = "姓名";
- //
- // label5
- //
- this.label5.Location = new System.Drawing.Point(408, 24);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(100, 16);
- this.label5.TabIndex = 2;
- this.label5.Text = "类型";
- //
- // label6
- //
- this.label6.Location = new System.Drawing.Point(536, 24);
- this.label6.Name = "label6";
- this.label6.Size = new System.Drawing.Size(100, 16);
- this.label6.TabIndex = 2;
- this.label6.Text = "可借册";
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.rbt4);
- this.groupBox2.Controls.Add(this.rbt3);
- this.groupBox2.Controls.Add(this.txt5);
- this.groupBox2.Controls.Add(this.label7);
- this.groupBox2.Enabled = false;
- this.groupBox2.Location = new System.Drawing.Point(0, 112);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(672, 72);
- this.groupBox2.TabIndex = 35;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "借阅图书信息";
- //
- // rbt4
- //
- this.rbt4.Location = new System.Drawing.Point(96, 32);
- this.rbt4.Name = "rbt4";
- this.rbt4.Size = new System.Drawing.Size(72, 24);
- this.rbt4.TabIndex = 0;
- this.rbt4.Text = "条形码";
- //
- // rbt3
- //
- this.rbt3.Checked = true;
- this.rbt3.Location = new System.Drawing.Point(24, 32);
- this.rbt3.Name = "rbt3";
- this.rbt3.Size = new System.Drawing.Size(56, 24);
- this.rbt3.TabIndex = 0;
- this.rbt3.TabStop = true;
- this.rbt3.Text = "编号";
- //
- // txt5
- //
- this.txt5.Location = new System.Drawing.Point(264, 32);
- this.txt5.Name = "txt5";
- this.txt5.Size = new System.Drawing.Size(136, 21);
- this.txt5.TabIndex = 1;
- this.txt5.Text = "";
- this.txt5.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt5_KeyPress);
- //
- // label7
- //
- this.label7.Location = new System.Drawing.Point(176, 36);
- this.label7.Name = "label7";
- this.label7.Size = new System.Drawing.Size(80, 16);
- this.label7.TabIndex = 2;
- this.label7.Text = "输入后请回车";
- //
- // dataGrid1
- //
- this.dataGrid1.CaptionVisible = false;
- this.dataGrid1.DataMember = "";
- this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
- this.dataGrid1.Location = new System.Drawing.Point(0, 184);
- this.dataGrid1.Name = "dataGrid1";
- this.dataGrid1.ReadOnly = true;
- this.dataGrid1.Size = new System.Drawing.Size(672, 296);
- this.dataGrid1.TabIndex = 36;
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(152, 488);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(100, 16);
- this.label1.TabIndex = 37;
- this.label1.Text = "已借书";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(328, 488);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(100, 16);
- this.label2.TabIndex = 37;
- this.label2.Text = "本次借书";
- //
- // BookBorrow
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(672, 509);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.dataGrid1);
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.toolBar1);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.label2);
- this.Name = "BookBorrow";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "【图书借阅】";
- this.Load += new System.EventHandler(this.BookBorrow_Load);
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- //----------创建窗体时,设置表格,并初始化变量--------------
- private void BookBorrow_Load(object sender, System.EventArgs e)
- {
- this.DataGridStateControl();
- totalCount=0;
- thisCount=0;
- }
- //----------设置明细表格各列的属性----------
- private void DataGridStateControl()
- {
- tblBooks=new DataTable();
- tblBooks.Columns.Add("状态",typeof(string));
- tblBooks.Columns.Add("图书编号",typeof(string));
- tblBooks.Columns.Add("书名",typeof(string));
- tblBooks.Columns.Add("借阅时间",typeof(string));
- tblBooks.Columns.Add("应还时间",typeof(string));
- tblBooks.Columns.Add("出版社",typeof(string));
- tblBooks.Columns.Add("价格",typeof(decimal));
- this.dataGrid1.DataSource = tblBooks;
- tblBooks.Rows.Add(tblBooks.NewRow()); //向表中添加一行
- DataGridTableStyle ts = new DataGridTableStyle();
- DataGridTextBoxColumn aColumnTextColumn;
- ts.AllowSorting = false;
- ts.AlternatingBackColor = Color.LightGray;
- ts.MappingName = tblBooks.TableName;
- int numCols = tblBooks.Columns.Count;
- for (int i = 0;i< numCols;i++)
- {
- aColumnTextColumn = new DataGridTextBoxColumn();
- if(i==2)
- {
- aColumnTextColumn.Width=140;//设置图书名称宽度
- }
- if ( i == 5 )
- {
- aColumnTextColumn.Width = 100;//设置出版社名称列宽度
- }
- aColumnTextColumn.MappingName = tblBooks.Columns[i].ColumnName;
- aColumnTextColumn.HeaderText = tblBooks.Columns[i].ColumnName;
- aColumnTextColumn.NullText = "";
- aColumnTextColumn.Format = "N"; //设置为数字格式显示
- ts.GridColumnStyles.Add(aColumnTextColumn);
- }
- dataGrid1.TableStyles.Add(ts);
- }
- //------------查询并读入读者信息以及读者已经借书的信息------------
- private void txt1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar==13&&txt1.Text.Trim()!="")
- {
- txt2.Clear();
- txt3.Clear();
- txt4.Clear();
- this.showInfo();//显示读者信息
- }
- }
- //------------显示读者信息和所借图书信息----------------
- private void showInfo()
- {
- string strConn = "workstation id=localhost;Integrated Security=SSPI;database=libbook";
- SqlConnection cn=new SqlConnection(strConn);
- cn.Open();
- SqlCommand cmd=cn.CreateCommand();
- if(rbt1.Checked)//判断根据编号查询读者信息还是根据条形码
- {
- cmd.CommandText="select a.姓名,a.类型,b.图书册书,a.编号 from 读者信息 a,读者类型 b"
- +" where (a.类型=b.类型)and(a.编号='"+txt1.Text.Trim()+"')";
- }
- else
- {
- cmd.CommandText="select a.姓名,a.类型,b.图书册书,a.编号 from 读者信息 a,读者类型 b"
- +" where (a.类型=b.类型)and(a.条形码='"+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();
- txt4.Text=dr.GetValue(2).ToString().Trim();
- readerID=dr.GetValue(3).ToString().Trim();
- }
- dr.Close();
- string strCmd="select a.状态,a.图书编号,b.书名,a.借阅时间,a.应还时间,b.出版社,b.价格"
- +" from 图书借阅 a,图书信息 b where (a.图书编号=b.编号)and(a.状态='未还')"
- +"and(读者编号='"+readerID+"')";//查询读者所借书籍信息
- SqlDataAdapter da=new SqlDataAdapter(strCmd,cn);
- tblBooks.Clear();//清空借书记录
- da.Fill(tblBooks);//读入读者借阅图书信息
- totalCount=tblBooks.Rows.Count;//显示已借书数量和本次借书数量
- thisCount=0;
- label1.Text="已借书"+totalCount.ToString()+"本";
- label2.Text="本次借书0本";
- groupBox2.Enabled=true;//允许借书
- txt5.Focus();//光标移动到输入图书编号文本框处,开始借书
- }
- //--------------按回车借书---------------
- private void txt5_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
- {
- if(e.KeyChar==13&&txt5.Text.Trim()!="")
- {
- this.borrowBook();//读者借书
- }
- }
- //------------读者借书-------------
- private void borrowBook()
- {
- if(this.totalCount>=Convert.ToInt32(txt4.Text.Trim()))//如果借书达到上限,则不允许再借
- {
- MessageBox.Show("已经达到最大借书数量,请先归还书籍后再借书","借书数量达到上限",MessageBoxButtons.OK,MessageBoxIcon.Information);
- groupBox2.Enabled=false;
- return;
- }
- string strConn = "workstation id=localhost;Integrated Security=SSPI;database=libbook";
- SqlConnection cn=new SqlConnection(strConn);
- cn.Open();
- SqlCommand cmd=cn.CreateCommand();
- DataRow aRow=tblBooks.NewRow();
- string bookType;//保存新借图书的类型
- if(rbt3.Checked)
- {
- cmd.CommandText="select 编号,书名,出版社,价格,类型 from 图书信息 where 编号='"+txt5.Text.Trim()+"'";
- }
- else
- {
- cmd.CommandText="select 编号,书名,出版社,价格,类型 from 图书信息 where 条形码='"+txt5.Text.Trim()+"'";
- }
- SqlDataReader dr=cmd.ExecuteReader();//执行查询,并读入图书数据
- dr.Read();
- if(dr.HasRows==false)//如未能查询到图书信息,则提示重新输入
- {
- MessageBox.Show("无此图书,请检查后重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- return;
- }
- else
- {
- string newbookID=dr.GetValue(0).ToString();
- foreach(DataRow newRow in tblBooks.Rows)//不允许重复借书
- {
- if(newRow["图书编号"].ToString().Trim()==newbookID.Trim())
- {
- MessageBox.Show("该读者已经借有此书,不能再借","信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
- return;
- }
- }
- aRow["图书编号"]=dr.GetValue(0).ToString();
- aRow["书名"]=dr.GetValue(1).ToString();
- aRow["出版社"]=dr.GetValue(2).ToString();
- aRow["价格"]=Convert.ToDecimal(dr.GetValue(3));
- aRow["状态"]="新借";
- aRow["借阅时间"]=System.DateTime.Now.ToString();
- bookType=dr.GetValue(4).ToString();
- }
- dr.Close();
- cmd.CommandText="select 可借天数 from 图书类型 where 类型名称='"+bookType+"'";//计算归还时间
- int days=Convert.ToInt32(cmd.ExecuteScalar());
- DateTime returnTime=System.DateTime.Now.AddDays(days);
- aRow["应还时间"]=returnTime.ToString();
- tblBooks.Rows.Add(aRow);//增加新借书记录
- totalCount++;
- thisCount++;
- label1.Text="已借书"+totalCount.ToString()+"本";
- label2.Text="本次借书"+thisCount.ToString()+"本";
- }
- //--------处理借书事务----------
- private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
- {
- if(e.Button.ToolTipText=="确定借书")
- {
- string strConn = "workstation id=localhost;Integrated Security=SSPI;database=libbook";
- SqlConnection cn=new SqlConnection(strConn);
- cn.Open();
- SqlCommand cmd=cn.CreateCommand();
- foreach(DataRow newRow in tblBooks.Rows)
- {
- if(newRow["状态"].ToString()=="新借")//插入新增的图书记录
- {
- cmd.CommandText="insert into 图书借阅([图书编号],[读者编号],[借阅时间],[应还时间],[状态],[续借次数])"
- +" values('"+newRow["图书编号"].ToString()+"','"+readerID+"','"+newRow["借阅时间"]
- +"','"+newRow["应还时间"]+"','未还','0')";
- cmd.ExecuteNonQuery();
- newRow["状态"]="未还";//将已经保存的借书状态改为未还
- }
- }
- }
- if(e.Button.ToolTipText=="取消借书")
- {
- tblBooks.Clear();
- groupBox2.Enabled=false;
- }
- if(e.Button.ToolTipText=="退出")
- {
- foreach(DataRow aRow in this.tblBooks.Rows)
- {
- if(aRow["状态"].ToString()=="新借")//检索表中是否有新借书数据,提示用户保存
- {
- DialogResult dlg=MessageBox.Show("该读者有新借图书尚未保存,退出将无法保存新借图书记录","确认退出",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
- if(dlg==DialogResult.OK)
- {
- return;
- }
- }
- }
- this.Close();
- }
- }
- }
- }