C#制作windows窗体的图书管理系统

说明:

1. 采用C#语言开发.

2. 数据库使用SQLServer 2019

3. 对数据库的操作采用ADO.NET框架

 

展示图(部分):

 

 

 

 

 

 

部分代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 图书管理系统
{
    public partial class FindBookForm : Form
    {
        public FindBookForm()
        {
            InitializeComponent();
        }
        private string _tag;
        public string Tag1
        {
            get { return _tag; }
            set { _tag = value; }
        }

        // pageIndex 标记DGV应该显示第几页
        private int pageIndex;
        // pageIndex 标记DGV的总页数
        int maxIndex;

        #region 添加书籍
        private static void INB()
        {
            新书入库Form iNB = new 新书入库Form();
            iNB.ShowDialog();
        }
        private void 新书入库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            INB();
        }
        private void 书籍入库tSBtn_Click(object sender, EventArgs e)
        {
            INB();
        }

        #endregion

        #region 注销书籍
        private void 注销书籍ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            新书入库Form iNB = new 新书入库Form();
            iNB.P = "1";
            iNB.Text = "注销书籍";
            iNB.ShowDialog();
        }
        #endregion

        #region 查看帮助
        private void 查看帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("书籍查询模块", "帮助");
        }
        private void 查看帮助tSBtn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("书籍查询模块", "帮助");
        }

        #endregion

        private void FindBookForm_Load(object sender, EventArgs e)
        {
            if (Tag1 == "user")
            {
                注销书籍ToolStripMenuItem.Enabled = false;
                新书入库ToolStripMenuItem.Enabled = false;
                书籍入库tSBtn.Enabled = false;
                dgv_list.ReadOnly = true;
            }
            pageIndex = 1;
            GetData();
        }

        private void GetData()
        {
            // 获取页大小
            int pageSize = 14;
            // 计算最多有多少页--Math.Ceiling--向上取整
            // Convert.ToInt32(SqlHelper.ExecuteScalar("select count(*) from books"))得到数据记录总数
            maxIndex = (int)Math.Ceiling((Convert.ToInt32(SqlHelper.ExecuteScalar("select count(*) from books")) * 1.0 / pageSize));
            // 获取连接字符串
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["library"].ConnectionString;
            // 得到连接
            using (SqlConnection conn = new SqlConnection(str))
            {
                // 判断是否有数据
                // Convert.ToInt32(SqlHelper.ExecuteScalar("select count(*) from books"))得到数据记录总数
                if (Convert .ToInt32 (SqlHelper.ExecuteScalar("select count(*) from books")) > 0 && pageIndex > 0)
                {
                    // 确定有数据才进行查询
                    // 创建表格,用来存储数据
                    DataTable dt = new DataTable();
                    // 构造带分页功能的 sql 语句
                    string sql = "select bNum,bName,bAuthor,bPubCom,bPubDat,ISBN,bPrice,bTag from booksWithbookindex where bookindex between @sIndex and @eIndex";
                    // 构造适配器对象
                    SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
                    // 传递参数
                    // 本页第一条数据
                    adapter.SelectCommand.Parameters.AddWithValue("@sIndex", ((pageIndex - 1) * pageSize + 1));
                    // 本页最后一条数据
                    adapter.SelectCommand.Parameters.AddWithValue("@eIndex", pageIndex * pageSize);
                    // 执行查询
                    conn.Open();
                    adapter.Fill(dt);
                    dgv_list.DataSource = dt;
                }
                else if (pageIndex <= 0)
                {
                    pageIndex = 1;
                }
                else
                {
                    MessageBox.Show("没有获取到数据","警告");
                }
            }
        }

        private void 刷新书库tSBtn_Click(object sender, EventArgs e)
        {
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            GetData();
        }
        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            GetData();
        }

        private void 跳转至首页tSBtn_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            GetData();
        }

        private void 跳转至上一页tSBtn_Click(object sender, EventArgs e)
        {
            pageIndex--;
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            GetData();
        }

        private void 跳转至下一页页tSBtn_Click(object sender, EventArgs e)
        {
            pageIndex++;
            if (pageIndex > maxIndex )
            {
                pageIndex--;
                MessageBox.Show("哎呀,到底啦 !","提示");
            }
            GetData();
        }

        private void 跳转至最后一页tSBtn_Click(object sender, EventArgs e)
        {
            pageIndex = maxIndex;
            GetData();
        }private void 按书名查找tSBtn_Click(object sender, EventArgs e)
        {
            string sql = "select bNum,bName,bAuthor,bPubCom,bPubDat,ISBN,bPrice,bTag from booksWithbookindex where bName like '%'+ @bName +'%'";
            MyDictionary dic = new MyDictionary();
            dic.Add("@bName", 书名tSTB.Text.Trim());
            DataTable dt = SqlHelper.GetList(sql, dic);
            dgv_list.DataSource = dt;
        }
    }
}

完整代码整理后放出

参考:https://blog.csdn.net/zimou5581/article/details/51646238

 

posted @ 2021-12-07 16:55  Arisf  阅读(1115)  评论(0编辑  收藏  举报