⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 图书借阅.cs

📁 图书管理系统源代码,很图书管理系统源代码,很不错,有参考作用不错,有参考作用
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 图书馆管理系统
{
    public partial class 图书借阅 : Form
    {
        public 图书借阅()
        {
            InitializeComponent();
        }
        private void TextReadOnly(bool A)
        {
            textBox2.ReadOnly = A;
            textBox3.ReadOnly = A;
            textBox4.ReadOnly = A;
        }
        public string readerID;
        public DataTable tb;
        public int outnum, thisnum;
        public void DataGridinfo()
        {
             tb = new DataTable();
            tb.Columns.Add("状态", typeof(string));
            tb.Columns.Add("图书编号", typeof(string));
            tb.Columns.Add("书名", typeof(string));
            tb.Columns.Add("借阅时间", typeof(string));
            tb.Columns.Add("应还时间", typeof(string));
            tb.Columns.Add("出版社", typeof(string));
            tb.Columns.Add("价格", typeof(string));
            dataGrid1.DataSource = tb;
            //tb.Rows.Add(tb.NewRow());

            /*DataGridTableStyle ts = new DataGridTableStyle();
            DataGridTextBoxColumn aColumnTextColumn;
            ts.AllowSorting = false;
            ts.AlternatingBackColor = Color.LightCoral;
            ts.MappingName = tb.TableName;*/
        }
        private void 图书借阅_Load(object sender, EventArgs e)
        {
            DataGridinfo();
            label6.Text = "已借书0本";
            label7.Text = "本次借书0本";
            TextReadOnly(true);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==13&&textBox1.Text.Trim()!="")
            {
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                showinfo();
            }
        }
        //--------------------------------------显示---------------------------------------------
        private void showinfo()
        {
            sqlConnection1.Open();
            string str="";
            if (radioButton1.Checked == true)
                str = "select 姓名,读者信息.类型,图书册数,编号 from 读者信息 ,读者类型 where 读者信息.类型=读者类型.类型 and 读者信息.编号='" + textBox1.Text + "'";
            if (radioButton2.Checked == true)
            {
                str = "select 姓名,读者信息.类型,图书册数,编号 from 读者信息 ,读者类型 where 读者信息.类型=读者类型.类型 and 读者信息.条形码='" + textBox1.Text + "'";
            }
            
            SqlCommand mycom = new SqlCommand(str,sqlConnection1);
            SqlDataReader mydr = mycom.ExecuteReader();
                mydr.Read();
                if (mydr.HasRows == false)
                {
                    MessageBox.Show("无此读者,请检查后重新输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    textBox2.Text = mydr.GetValue(0).ToString();
                    textBox3.Text = mydr.GetValue(1).ToString();
                    textBox4.Text = mydr.GetValue(2).ToString();
                    readerID = mydr.GetValue(3).ToString();
                }
            
            mydr.Close();
            string str1 = "select 状态,图书编号,书名,借阅时间,应还时间,出版社,价格 from 图书信息 join 图书借阅 on 图书信息.编号=图书借阅.图书编号 where 状态='未还' and 读者编号="+readerID+"";
            SqlDataAdapter da = new SqlDataAdapter(str1, sqlConnection1);
            tb.Clear();
            da.Fill(tb);
            outnum = tb.Rows.Count;
            thisnum = 0;
            label6.Text = "已借书" + outnum + "本";
            label7.Text = "本次借书" + thisnum + "本";
            groupBox2.Enabled = true;
            textBox5.Focus();//----------------------------------
            sqlConnection1.Close();
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13&&textBox5.Text.Trim()!="")
            {
                borrowbook();
            }
        }
        //-----------------------------------------------------借书----------------
        private void borrowbook()
        {
            if (outnum >= Convert.ToInt32(textBox4.Text.Trim()))
            {
                MessageBox.Show("已经达到最大借书数量,请先归还书籍后再借书", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            sqlConnection1.Open();
            string str1 = "";
            if (radioButton1.Checked == true)
                str1 = "select 编号,书名,出版社,价格,类型 from 图书信息  where 编号='" + textBox5.Text + "'";
            if (radioButton2.Checked == true)
                str1 = "select 编号,书名,出版社,价格,类型 from 图书信息  where 条形码='" + textBox5.Text + "'";

            SqlCommand com = new SqlCommand(str1,sqlConnection1);
            SqlDataReader dr = com.ExecuteReader();

            DataRow arow = tb.NewRow();
            string bookType;

            dr.Read();
            if (dr.HasRows == false)
            {
                MessageBox.Show("无此图书,请检查后重新输入", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {//判断该读者是否已经借过该编号的书-----没有就在表中新增一行
                string newbookID = dr.GetValue(0).ToString();
                foreach (DataRow newRow in tb.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();
            string str2 = "select 可借天数 from 图书类型 where 类型名称='"+bookType+"'";
            SqlCommand cmd = new SqlCommand(str2, sqlConnection1);
            int day = Convert.ToInt32(cmd.ExecuteScalar());
            DateTime returntime = System.DateTime.Now.AddDays(day);
            arow["应还时间"] = returntime.ToString();
            tb.Rows.Add(arow);
            outnum++;
            thisnum++;
            label6.Text = "已借书" + outnum + "本";
            label7.Text = "本次借书" + thisnum + "本";
            sqlConnection1.Close();
        }
        //-------------------------确定借书-------
        private void button1_Click(object sender, EventArgs e)
        {
            sqlConnection1.Open();
            foreach (DataRow newRow in tb.Rows)
            {
                if (newRow["状态"].ToString() == "新借")
                {
                    SqlCommand insertcom = new SqlCommand("insert into 图书借阅(图书编号,读者编号,借阅时间,应还时间,续借次数,状态) values('" + newRow["图书编号"].ToString() + "','" + readerID + "','" + newRow["借阅时间"] + "','" + newRow["应还时间"] + "','0','未还')", sqlConnection1);
                    insertcom.ExecuteNonQuery();
                    SqlCommand s1 = new SqlCommand("update 图书信息 set 现存量 = isnull(现存量,0) - 1 from 图书信息 as a, 图书借阅 as b where a.编号=b.图书编号", sqlConnection1);
                    s1.ExecuteNonQuery();
                    newRow["状态"] = "未还";
                }
            }
            sqlConnection1.Close();
        }
        //------------------------取消借书-----------------------------------------
        private void button2_Click(object sender, EventArgs e)
        {
            tb.Clear();
            groupBox2.Enabled = false;
        }
        //----------------------退出--------------------
        private void button3_Click(object sender, EventArgs e)
        {
            foreach (DataRow aRow in tb.Rows)
            {
                if (aRow["状态"].ToString() == "新借")
                {
                    DialogResult result = MessageBox.Show("该读者新借的图书没有保存,退出将无法保存该图书新借记录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.Cancel)
                        return;
                    else
                      if(result==DialogResult.OK)
                        this.Close();
                }
            }
            this.Close();
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -