C# 图书管理系统

本文描述了一个图书管理系统的需求分析,包括管理员对图书信息、借阅信息和用户信息的管理,以及用户自助借阅和信息查询的功能。涉及到数据库操作、SQL查询和图表数据展示,展示了关键代码片段以实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求分析:

3.2.1系统管理员

整个系统的管理人员,负责书籍的管理,添加,修改校正,以方便用户的取阅;同时负责用户的管理、删除、查看;了解借阅情况等。

3.2.2用户

使用该系统借阅书籍的学生,不需要人工服务即可完成借阅归还书籍。

3.2.3为系统管理员提供的服务

·图书信息管理:图书添加、图书修改、图书相关信息

·图书借阅管理:图书借阅信息查询

·读者信息管理:读者相关信息、读者删除

3.2.4为用户提供的服务

·信息管理:用户注册、用户登录、用户信息查看、用户信息修改

·图书管理:图书快速查询、图书借阅、图书归还、借阅图书查询

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Windows.Forms.DataVisualization.Charting;

namespace 图书管理
{
    //管理员的操作界面
    public partial class Form3 : Form
    {
        private const string sqluser = "sa";
        private const string sqlpsd = "zyw123456";
        //保存图表横坐标
        List<int> x = new List<int>(12);
        public Form3()
        {
            //初始化各个组件
            InitializeComponent();
            //将窗口放置在屏幕中央
            this.StartPosition = FormStartPosition.CenterScreen;
            //设置各个文本框的文本
            this.textBox1.Text = "用户须知:\r\n该页面提供了图书的快速查找——用户可以根据不同的属性进行对应的查找\r\n" +
                "同时提供了对图书的修改,灰色列属性是不可修改对象,其他属性均可以修改——只需点击对应的单元并提交最后一列的修改按钮即可\r\n" +
                "下方提供了新增图书的功能——用户只需填写相关属性的信息,并点击添加按钮即可";
            this.textBox11.Text = "用户须知:\r\n" +
                "该页面仅提供借书条目的快速查询\r\n" +
                "具体操作:根据下滑框选择对应属性,在下方文本框内输入对应的信息,点击搜索即可\r\n" +
                "例如:选择bbid,输入1,搜索,将展示出所有bbid包含1的条目";
            this.textBox14.Text = "用户须知:\r\n" +
                "1. 通过下滑框快速查找用户\r\n" +
                "2. 可以注销已经归还所有图书的用户";
            //初始化图表横坐标
            for(int i = 1; i <= 12; i++)
            {
                x.Add(i);
            }
            List<int> y = new List<int>();
            //调用addy函数读取数据库中每一月份借书数据
            addy(y);
            //设置柱状图横纵坐标
            this.chart1.ChartAreas[0].AxisX.Title = "月份";
            this.chart1.ChartAreas[0].AxisY.Title = "借书数量";
            //绑定数据
            this.chart1.Series[0].Points.DataBindXY(x,y);
            //将排行榜信息刷新
            button14_Click(null, null);
        }



        private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“testDataSet1.Student”中。您可以根据需要移动或移除它。
            this.studentTableAdapter.Fill(this.testDataSet1.Student);
            // TODO: 这行代码将数据加载到表“testDataSet.BorBook”中。您可以根据需要移动或移除它。
            this.borBookTableAdapter.Fill(this.testDataSet.BorBook);
            // TODO: 这行代码将数据加载到表“book._Book”中。您可以根据需要移动或移除它。
            this.bookTableAdapter.Fill(this.book._Book);

        }
        //新增图书按钮触发事件
        private void button1_Click(object sender, EventArgs e)
        {
            //定义数据库连接字符串
            string conStr = "server=.;database=test;uid={0};password={1}";
            conStr = String.Format(conStr, sqluser, sqlpsd);
            //表示数据库连接
            SqlConnection conn = new SqlConnection(conStr);
            //打开数据库连接
            conn.Open();
            try
            {
                //定义SQL命令
                string sql = "insert into Book values('{0}','{1}','{2}','{3}','{4}',{5},{6},{7})";
                //格式化SQL命令
                sql = String.Format(sql,this.textBox3.Text, this.textBox4.Text, this.textBox5.Text, this.textBox6.Text, this.textBox7.Text, this.textBox8.Text, this.textBox9.Text,this.textBox8.Text);
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    //MessageBox.Show(sql);
                    //执行SQL命令
                    int res = cmd.ExecuteNonQuery();
                    if (res == 1)
                    {
                        //提示框提示
                        MessageBox.Show("添加成功!");
                    }
                }
            }
            //处理异常
            catch (SqlException e1)
            {
                MessageBox.Show(e1.Message.ToString());
            }
            finally
            {
                //实时刷新dataGridView的数据
                fresh(dataGridView1,"select * from Book");
                //关闭连接
                conn.Close();
            }
        }
        //Book的搜索按钮触发事件
        private void button2_Click(object sender, EventArgs e)
        {
            //通过comboBox和textBox获取属性列和属性值
            //根据SQL语句快速select数据
            fresh(dataGridView1, String.Format("select * from Book where {0} LIKE '%{1}%'", this.comboBox1.Text.ToString(), this.textBox2.Text));
        }
        //dataGridView1的单元格点击触发事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取当前选中行的索引
            int Index = this.dataGridView1.CurrentRow.Index;
            //判断是否点击"修改"列的按钮
            if (Index<this.dataGridView1.Rows.Count-1 && this.dataGridView1.CurrentCell.Value.ToString()== "修改")
            {
                //数据库连接字符串
                string Constr = "server=.;database=test;uid={0};password={1}";
                Constr = string.Format(Constr, sqluser, sqlpsd);
                //定义SQL Server连接对象
                SqlConnection conn = new SqlConnection(Constr);
                //打开数据库连接
                conn.Open();
                //定义SQL命令
                string sql = "update Book set bname = '{0}',btype = '{1}',bauthor = '{2}',inamount = '{3}',allamount = '{4}' where bid = '{5}'";
                //格式化SQL命令
                sql = String.Format(sql, dataGridView1[1, Index].Value.ToString(),
                    dataGridView1[2, Index].Value.ToString(),
                    dataGridView1[3, Index].Value.ToString(),
                    Convert.ToInt32(dataGridView1[7, Index].Value)-Convert.ToInt32(dataGridView1[6, Index].Value),
                    dataGridView1[7, Index].Value.ToString(),
                    dataGridView1[0,Index].Value.ToString());
                try
                {
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        //执行SQL命令
                        cmd.ExecuteNonQuery();
                    }
                }
                //捕获异常
                catch (SqlException se)
                {
                    MessageBox.Show(se.ToString());
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                    //实时刷新update Book额的数据
                    fresh(this.dataGridView1, "select * from Book");
                }
            }
        }
        /*
         * 用于实时刷新dataGridView,根据SQL语句实时更新数据库的内容并显示在dataGridView
         * param1:想要刷新的dataGridView
         * param2:执行的SQL语句
         */
        private void fresh(DataGridView dataGridView1, string sql)
        {
            string consqlserver = "Data Source=.;Initial Catalog=test;Integrated Security=True;";
            DataSet ds = new DataSet();                   //定义数据内存中缓存
            SqlConnection con;                            //定义SQL Server连接对象
            SqlDataAdapter da;                            //数据库命令和数据库连接
            con = new SqlConnection(consqlserver);        //定义SQL Server连接对象
            da = new SqlDataAdapter(sql, con);            //数据库命令和数据库连接
            con.Open();
            try
            {
                da.Fill(ds);                                    //填充数据
                dataGridView1.DataSource = ds.Tables[0];        //获取数据源赋值数据库控件
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg.Message);                   //异常处理
            }
            finally
            {
                con.Close();                    //关闭连接
                con.Dispose();                  //释放连接
                da.Dispose();                   //释放资源
            }
        }
        //清除Book的搜索按钮触发事件
        private void button5_Click(object sender, EventArgs e)
        {
            //将textBox2文本清空,并返回select的所有数据
            this.textBox2.Text = "";
            fresh(this.dataGridView1, "select * from Book");
        }
        //Book搜索按钮触发事件
        private void button6_Click(object sender, EventArgs e)
        {
            //通过comboBox和textBox获取属性列和属性值
            //根据SQL语句快速select数据
            fresh(dataGridView2, String.Format("select * from BorBook where {0} LIKE '%{1}%'", this.comboBox2.Text.ToString(), this.textBox10.Text));
        }
        //清除BorBook的搜索按钮触发事件
        private void button7_Click(object sender, EventArgs e)
        {
            //将textBox10文本清空,并返回select的所有数据
            this.textBox10.Text = "";
            fresh(this.dataGridView2, "select * from BorBook");
        }
        //BorBook搜索按钮触发事件
        private void button10_Click(object sender, EventArgs e)
        {
            //通过comboBox和textBox获取属性列和属性值
            //根据SQL语句快速select数据
            fresh(dataGridView3, String.Format("select sno,sname,ssex,sage from Student where {0} LIKE '%{1}%';", this.comboBox3.Text.ToString(), this.textBox12.Text));
        }
        //清除Student的搜索按钮触发事件
        private void button11_Click(object sender, EventArgs e)
        {
            //将textBox12文本清空,并返回select的所有数据
            fresh(this.dataGridView3, "select sno,sname,ssex,sage from Student;");
            this.textBox12.Text = "";
        }

        //datGridView3的单元格点击触发事件
        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取点击的行号
            int Index = this.dataGridView3.CurrentRow.Index;
            //判断是否点击"注销"列的按钮
            if (Index<this.dataGridView3.Rows.Count-1 && this.dataGridView3.CurrentCell.Value.ToString()== "注销")
            {
                //数据库连接字符串
                string Constr = "server=.;database=test;uid={0};password={1}";
                Constr = String.Format(Constr, sqluser, sqlpsd);
                //定义SQL Server连接对象
                SqlConnection conn = new SqlConnection(Constr);
                //打开数据库连接
                conn.Open();
                //定义SQL命令
                string sql = "select count(*) from BorBook where sno = '{0}' and retdate is null";
                //格式化SQL命令
                sql = String.Format(sql, dataGridView3[0, Index].Value.ToString());
                try
                {
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        //执行SQL命令并返回第一个结果即count(*)
                        int res = Convert.ToInt32(cmd.ExecuteScalar());
                        //若res不为0,即还有图书未归还则不能注销
                        if(res != 0)
                        {
                            MessageBox.Show("该用户还有书籍未归还,不能注销!");
                            return;
                        }
                        else
                        {
                            //修改cmd的CommandText文本
                            cmd.CommandText = String.Format("delete from Student where sno = '{0}'", dataGridView3[0, Index].Value.ToString());
                            //执行SQL的删除语句
                            res = cmd.ExecuteNonQuery();
                            if(res!= 0)
                            {
                                //提示删除成功
                                MessageBox.Show("注销成功!");
                            }
                            else
                            {
                                MessageBox.Show("注销失败!");
                            }
                        }
                    }
                }
                //捕获异常
                catch (SqlException se)
                {
                    MessageBox.Show(se.ToString());
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                    //更新Student和BorBook数据
                    fresh(this.dataGridView3, "select * from Student");
                    fresh(this.dataGridView2, "select * from BorBook");
                }
            }
        }

      

效果展示:

源码:

百度链接 

https://pan.baidu.com/s/1ZqQ0XxOQ_mFaXWempYpB3w?pwd=rwfl 
提取码:rwfl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值