自学内容网 自学内容网

C#+Winform+SQLServer+图书管理系统

 


Dao.cs

using Bookmanagementsystem;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Security.Principal;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bookmanagementsystem
{
    internal class Dao
    {
        SqlConnection sc;// 数据库连接对象
        public SqlConnection connect()// 连接数据库
        {
            // string str = @"Data Source=192.168.107.40,50134;Initial Catalog=BookMS;Persist Security Info=True;User ID=sa;Encrypt=True;Trust Server Certificate=True";// 连接串
            /// <summary>
            /// Integrated Security=True  这玩意让我想一天(•́へ•́╬)
            /// Integrated Security=True  这玩意让我想一天(•́へ•́╬)
            /// Integrated Security=True  这玩意让我想一天(•́へ•́╬)
            /// Integrated Security=True  这玩意让我想一天(•́へ•́╬)
            /// Integrated Security=True  这玩意让我想一天(•́へ•́╬)
            /// Integrated Security=True  这玩意让我想一天(•́へ•́╬)
            /// </summary>
            /// <returns></returns>
            string str = @"Data Source=192.168.107.37,54036;Initial Catalog=BookMS;Integrated Security=True";
            sc = new SqlConnection(str);
            sc.Open();
            return sc;
        }
        public SqlCommand command(string sql)// 执行一条sql语句
        {
            SqlCommand cmd = new SqlCommand(sql, connect());
            return cmd;
        }
        public int Execute(string sql)// 获取执行sql语句后,数据库表中数据条数的更新数量
        {
            return command(sql).ExecuteNonQuery();
        }
        public SqlDataReader read(string sql)// 读取数据库中的数据
        {
            return command(sql).ExecuteReader();
        }
        public void DaoClose()// 关闭数据库
        {
            sc.Close();
        }
    }
}

Form1.cs

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

namespace Bookmanagementsystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // 用户的账号和姓名
        public static int id;
        public static string name;

        // 管理员登录的方法
        private void AdminLogin()
        {
            // 获取账号和密码  ->  对比
            // 用户输入的
            int id = int.Parse(txtId.Text);
            string pwd = txtPassword.Text;

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_Admin where AdminID = '{id}' and Pwd = '{pwd}'";
            SqlDataReader reader = dao.read(sql);
            if (reader.Read()==true)
            {
                // 表里面有该用户的消息
                Form1.id = id;
                sql = $"select [Name] from T_Admin where AdminID = {id}";
                reader = dao.read(sql);
                reader.Read();
                Form1.name = reader[0].ToString();

                txtId.Text = "";
                txtPassword.Text = "";

                reader.Close();
                dao.DaoClose();
                // 登录成功
                FormAdmin form = new FormAdmin();
                form.ShowDialog();
            }
            else
            {
                // 登录失败
                MessageBox.Show("账号或密码错误!", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }
        }
        private void UserLogin()
        {
            int id = int.Parse(txtId.Text);
            string pwd = txtPassword.Text;

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_User where Uid = '{id}' and Pwd = '{pwd}'";
            SqlDataReader reader = dao.read(sql);
            if (reader.Read() == true)
            {
                // 做一个判断 看看他是否有登录权限
                string sqlStatus = $"select Used from T_User where [Uid] = {int.Parse(txtId.Text)}";
                reader = dao.read(sqlStatus);
                reader.Read();
                if ((bool)reader[0]!=true)
                {
                    // 没有登录权限
                    MessageBox.Show("您的账号没有登录权限!\n\r请联系管理员qq:3587767616", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    return;
                }

                Form1.id = id;
                sql = $"select Uname from T_User where [Uid] = {id}";
                reader = dao.read(sql);
                reader.Read();
                Form1.name = reader[0].ToString();

                txtId.Text = "";
                txtPassword.Text = "";

                reader.Close();
                dao.DaoClose();
                // 登录成功
                FormUser form = new FormUser();
                form.ShowDialog();
            }
            else
            {
                // 登录失败
                MessageBox.Show("账号或密码错误!", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            }
        }

        private void btnLogon_Click(object sender, EventArgs e)
        {
            FormLogon form = new FormLogon();
            form.ShowDialog();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // 判断文本框是否有内容
            if (txtId.Text==""||txtPassword.Text=="")
            {
                MessageBox.Show("有空项!", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                return;
            }

            // 判断登录的对象
            if (rbtnAdmin.Checked==true)
            {
                // 管理员在登录
                AdminLogin();
            }
            if (rbtnUser.Checked==true)
            {
                // 用户在登录
                UserLogin();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes==MessageBox.Show("确认退出吗?","消息",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
            {
                // 退出
                this.Close();
            }
        }
    }
}

FormAddBook.cs

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;

namespace Bookmanagementsystem
{
    public partial class FormAddBook : Form
    {
        public FormAddBook()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // 如果文本框内容为空 返回
            if (txtAuthor.Text.Trim() == "" || txtID.Text.Trim() == "" || txtIntorduce.Text.Trim() == "" || txtName.Text.Trim() == "" || txtNum.Text.Trim() == "" || txtPrice.Text.Trim() == "" || txtType.Text == "" || Publisher.Text.Trim() == "" || dtpPBDate.Text == "")
            {
                MessageBox.Show("有空项", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 添加图书
            Dao dao = new Dao();
            dao.connect();
            string sql = $"insert into T_Book values('{int.Parse(txtID.Text)}','{txtName.Text}','{txtAuthor.Text}','{Publisher.Text}','{dtpPBDate.Value}','{txtType.Text}','{float.Parse(txtPrice.Text)}','{int.Parse(txtNum.Text)}','{txtIntorduce.Text}','0')";
            try
            {
                if (dao.Execute(sql) > 0)
                {
                    // 添加成功
                    dao.DaoClose();
                    MessageBox.Show("添加成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    dao.DaoClose();
                    MessageBox.Show("添加失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch 
            {
                dao.DaoClose();
                MessageBox.Show("ERROR", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

FormAdmin.cs

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;

namespace Bookmanagementsystem
{
    public partial class FormAdmin : Form
    {
        public FormAdmin()
        {
            InitializeComponent();
        }

        private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("确认退出吗?", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                // 退出
                this.Close();
            }
        }

        private void FormAdmin_Load(object sender, EventArgs e)// 窗体加载
        {
            this.label1.Text = $"管理员:{Form1.name} {Form1.id}";
        }

        private void 注销账号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("确定注销当前账号吗?", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                int id = Form1.id;
                Dao dao = new Dao();
                dao.connect();
                string sql = $"delete T_Admin where AdminID = '{id}'";
                if (dao.Execute(sql) > 0)
                {
                    // 注销账号成功
                    MessageBox.Show("注销账号成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dao.DaoClose();

                    // 返回到登录窗口
                    this.Close();
                }
                else
                {
                    // 注销失败
                    MessageBox.Show("注销账号失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dao.DaoClose();
                }
            }
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormUpdatePwd_Admin form = new FormUpdatePwd_Admin();
            form.ShowDialog();
        }

        private void 添加图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddBook form = new FormAddBook();
            form.ShowDialog();
        }

        private void 修改图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManage form = new FormManage();
            form.ShowDialog();
        }

        private void 搜索图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManage form = new FormManage();
            form.ShowDialog();
        }

        private void 下架图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormManage form = new FormManage();
            form.ShowDialog();
        }

        private void 查看用户租借情况ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormLookBorrow form = new FormLookBorrow();
            form.ShowDialog();
        }

        private void 用户信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormLookUser form = new FormLookUser();
            form.ShowDialog();
        }

        private void 图书评价管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDiscussion form = new FormDiscussion();
            form.ShowDialog();
        }

        private void 消息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormLookFeedBack form = new FormLookFeedBack();
            form.ShowDialog();
        }
    }
}

FormBorrowBook.cs

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

namespace Bookmanagementsystem
{
    public partial class FormBorrowBook : Form
    {
        public FormBorrowBook()
        {
            InitializeComponent();
        }

        private void LoadBook()
        {
            Dao dao = new Dao();
            dao.connect();
            string sql = "select * from T_Book";
            SqlDataReader reader = dao.read(sql);

            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), reader[8].ToString(), reader[9].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void FormBorrowBook_Load(object sender, EventArgs e)
        {
            // 把书籍数据显示到网格当中
            LoadBook();

            cobNum.Text = "1";
            if (dgv.Rows.Count==1)
            {

            }
            else
            {
                IbiName.Text = dgv.CurrentRow.Cells[1].Value.ToString();
            }
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv.CurrentRow == null || dgv.CurrentRow.Cells[0].Value==null)
            {
                MessageBox.Show("选中无效数据", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            IbiName.Text = dgv.CurrentRow.Cells[1].Value.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = dgv.CurrentRow.Cells[1].Value.ToString();
            int id = int.Parse(dgv.CurrentRow.Cells[0].Value.ToString());
            int num = int.Parse(cobNum.Text);
            DateTime date = DateTime.Now;
            int key = 1;

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select [Key] from T_Borrow where [Key] = '{key}'";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            // 循环读取租借表中是否有为key的数据
            while (true)
            {
                key++;
                sql = $"select [Key] from T_Borrow where [Key] = '{key}'";
                reader = dao.read(sql);
                reader.Read();

                if (!reader.HasRows)
                {
                    break;
                }
            }
            reader.Close();

            // 判断库存是否充足  库存不足  return
            string sqlFlag = $"select Bid from T_Book where 0<=Num-'{num}' and Bid = '{id}'";
            SqlDataReader reader1 = dao.read(sqlFlag);
            if (!reader1.Read())
            {
                MessageBox.Show("库存不足!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reader1.Close();
                dao.DaoClose();
                return;
            }

            string sqlInsert = $"insert into T_Borrow values('{key}','{Form1.id}','{Form1.name}','{id}','{name}','{date}','{num}')";
            string sqlUpdate = $"update T_Book set Num=Num-'{num}',BorrowCount=BorrowCount+'{num}'where Bid='{id}'";
            try
            {
                if (dao.Execute(sqlInsert) + dao.Execute(sqlUpdate) >= 2)
                {
                    // 租借成功
                    dao.DaoClose();
                    MessageBox.Show("租借成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dgv.Rows.Clear();
                    LoadBook(); // 更新表格
                }
                else
                {
                    dao.DaoClose();
                    MessageBox.Show("租借失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch 
            {
                MessageBox.Show("ERROR", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

FormDiscussion.cs

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

namespace Bookmanagementsystem
{
    public partial class FormDiscussion : Form
    {
        public FormDiscussion()
        {
            InitializeComponent();
        }
        private void LoadDiscussion()
        {
            dgv.Rows.Clear();

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_Discussion";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }
        private void LoadMyBook()
        {
            Dao dao = new Dao();
            dao.connect();
            string sql = $"select Bid from T_Borrow where [Uid] = {Form1.id}";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                cobId.Items.Add(reader[0].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void FormDiscussion_Load(object sender, EventArgs e)
        {
            if (Form1.id.ToString().Length==8)
            {
                // 用户在登录
                btnDelete.Visible = false;
            }
            else
            {
                // 管理员在登录
                btnSend.Visible = false;
            }
            LoadDiscussion();
            LoadMyBook();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            // 检查控件内容
            if (IbiName.Text=="NULL"||txtWords.Text==""||cobId.Text==""||cobScore.Text=="")
            {
                // 有空项
                MessageBox.Show("有空项!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 添加评论
            int Bid = int.Parse(cobId.Text);
            string Bname = IbiName.Text;
            int id = Form1.id;
            string name = Form1.name;
            string words = txtWords.Text;
            DateTime date = DateTime.Now;
            string score = cobScore.Text;
            int key = 0;

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select [Key] from T_Discussion where [Key] = {key}";

            SqlDataReader reader = dao.read(sql);

            while (true)
            {
                key++;
                sql = $"select [Key] from T_Discussion where [Key] = {key}";
                reader = dao.read(sql);
                reader.Read();

                if (!reader.HasRows)
                {
                    break;
                }
            }

            // 添加评论
            string sqlInsert = $"insert into T_Discussion values('{key}','{Bid}','{Bname}','{id}','{name}','{words}','{date}','{score}')";
            if (dao.Execute(sqlInsert)>0)
            {
                // 发表成功
                MessageBox.Show("发表成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                // 发表失败
                MessageBox.Show("发表失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            LoadDiscussion();// 更新评论
            reader.Close();
            dao.DaoClose();
        }

        private void cobId_TextChanged(object sender, EventArgs e)
        {
            // 获取当前选中的图书编号
            int id = int.Parse(cobId.Text);
            // 通过编号到T_Book中获取书名
            Dao dao = new Dao();
            dao.connect();
            string sql = $"select Bname from T_Book where Bid={id}";

            SqlDataReader reader = dao.read(sql);
            reader.Read();
            string name = reader[0].ToString();

            IbiName.Text = name;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // 判断选中的内容是否有效
            if (dgv.CurrentRow.Cells[0].Value==null)
            {
                MessageBox.Show("选中无效数据!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 删除操作
            Dao dao = new Dao();
            dao.connect();

            string sql = $"delete T_Discussion where [Key] = {int.Parse(dgv.CurrentRow.Cells[0].Value.ToString())}";
            if (dao.Execute(sql)>0)
            {
                // 删除成功
                MessageBox.Show("删除成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                // 删除失败
                MessageBox.Show("删除失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            LoadDiscussion();
            dao.DaoClose();
        }
    }
}

FormFeedBook.cs 

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;

namespace Bookmanagementsystem
{
    public partial class FormFeedBack : Form
    {
        public FormFeedBack()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 添加
            Dao dao = new Dao();
            dao.connect();
            string sql = $"insert into T_FeedBack values('{Form1.id}','{txtFeedBack.Text}','{DateTime.Now}')";
            if (dao.Execute(sql)>0)
            {
                // 提交反馈成功
                MessageBox.Show("提交成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                // 操作失败
                MessageBox.Show("操作失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            dao.DaoClose();
        }
    }
}


FormLogon.cs

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

namespace Bookmanagementsystem
{
    public partial class FormLogon : Form
    {
        public FormLogon()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogon_Click(object sender, EventArgs e)
        {
            // 先判断文本框是否为空
            if (txtAgainPwd.Text == "" || txtIDCard.Text == "" || txtName.Text == "" || txtTel.Text == "" || txtPwd.Text == "" || cobSex.Text == "")
            {
                // 提示有空项
                MessageBox.Show("有空项", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txtPwd.Text.Trim()!=txtAgainPwd.Text.Trim())
            {
                // 密码不匹配
                MessageBox.Show("密码不匹配", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 注册 获取数据库中账号的最大值 然后加1
            Dao dao = new Dao();
            dao.connect();// 链接并打开数据库
            string sql = "select MAX(Uid) from T_User";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            int id = int.Parse(reader[0].ToString());// 注册的账号
            string name = txtName.Text;
            string idCard = txtIDCard.Text;
            string tel = txtTel.Text;
            string sex = cobSex.Text;
            string pwd = txtPwd.Text;

            // 进行添加
            sql = $"insert into T_User values('{++id}','{name}','{pwd}','{sex}','{idCard}','{tel}','1')";
            if (dao.Execute(sql)>0)
            {
                // 注册成功
                MessageBox.Show($"注册成功,您的账号是{id}", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                // 注册失败
                MessageBox.Show("注册失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            reader.Close();
            dao.DaoClose();
        }
    }
}

FormLookBorrow.cs

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

namespace Bookmanagementsystem
{
    public partial class FormLookBorrow : Form
    {
        public FormLookBorrow()
        {
            InitializeComponent();
        }

        private void LoadBorrowInfo()
        {
            dgv.Rows.Clear();

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_Borrow";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void FormLookBorrow_Load(object sender, EventArgs e)
        {
            // 加载租借表中所有的信息到网格控件中
            LoadBorrowInfo();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string key = txtKey.Text;
            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_Borrow where Bname like '%{key}%' or Uname like '%{key}%'";

            dgv.Rows.Clear();

            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }
    }
}


FormLookFeedBack.cs

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

namespace Bookmanagementsystem
{
    public partial class FormLookFeedBack : Form
    {
        public FormLookFeedBack()
        {
            InitializeComponent();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormLookFeedBack_Load(object sender, EventArgs e)
        {
            // 把反馈表中的内容显示到网格控件中
            Dao dao = new Dao();
            dao.connect();

            string sql = $"select * from T_FeedBack";
            SqlDataReader reader = dao.read(sql);

            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }
    }
}

FormLookUser.cs 

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

namespace Bookmanagementsystem
{
    public partial class FormLookUser : Form
    {
        public FormLookUser()
        {
            InitializeComponent();
        }

        private void LoadUserInfo()
        {
            dgv.Rows.Clear();

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select Uid,Uname,Sex,IDCard,Tel,Used from T_User";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void FormLookUser_Load(object sender, EventArgs e)
        {
            // 显示所有的用户信息  密码除外
            LoadUserInfo();
            if (dgv.Rows.Count>1)
            {
                IbiID.Text = dgv.CurrentRow.Cells[0].Value.ToString();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LoadUserInfo();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (IbiID.Text=="NULL")
            {
                MessageBox.Show("未选中用户!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Dao dao = new Dao();
            dao.connect();
            string sql = $"update T_User set Used = 0 where [Uid] = {int.Parse(IbiID.Text)}";
            if (dao.Execute(sql)>0)
            {
                // 用户被停止登录系统
                dao.DaoClose();
                MessageBox.Show("用户被停止登录系统!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LoadUserInfo();
            }
            else
            {
                dao.DaoClose();
                MessageBox.Show("操作失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (IbiID.Text == "NULL")
            {
                MessageBox.Show("未选中用户!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Dao dao = new Dao();
            dao.connect();
            string sql = $"update T_User set Used = 1 where [Uid] = {int.Parse(IbiID.Text)}";
            if (dao.Execute(sql) > 0)
            {
                // 用户被停止登录系统
                dao.DaoClose();
                MessageBox.Show("用户被停止登录系统!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                LoadUserInfo();
            }
            else
            {
                dao.DaoClose();
                MessageBox.Show("操作失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            IbiID.Text = dgv.CurrentRow.Cells[0].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 获取关键字  进行模糊查询  用户表中的字段
            string key = txtKey.Text.Trim();
            // 清空网格数据
            dgv.Rows.Clear();
            // 显示到网格控件中
            Dao dao = new Dao();
            dao.connect();
            string sql = $"select Uid,Uname,Sex,IDCard,Tel,Used from T_User where Uname like '%{key}%' or Sex like '%{key}%' or Used like '%{key}%'";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString());
            }
            // 读取器、数据库链接关闭
            reader.Close();
            dao.DaoClose();
        }
    }
}

FormManage.cs 

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

namespace Bookmanagementsystem
{
    public partial class FormManage : Form
    {
        public FormManage()
        {
            InitializeComponent();
        }
        // 图书信息  修改图书功能
        public static int Bid;
        public static string Bname;
        public static string Author;
        public static string Publisher;
        public static string PubDate;
        public static string Type;
        public static int Num;
        public static float Price;
        public static string Introduce;

        private void LoadBooks()
        {
            dgv.Rows.Clear();

            Dao dao = new Dao();
            dao.connect();
            string sql = "select * from T_Book";
            SqlDataReader reader = dao.read(sql);

            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), reader[8].ToString(), reader[9].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void FormManage_Load(object sender, EventArgs e)
        {
            // 在窗体加载的同时  将数据库中的图书信息显示到网格控件当中
            LoadBooks();
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv.CurrentRow == null || dgv.CurrentRow.Cells[0].Value==null)
            {
                MessageBox.Show("选中无效数据!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string id = dgv.CurrentRow.Cells[0].Value.ToString();// 当前选中的图书编号
            string name = dgv.CurrentRow.Cells[1].Value.ToString();// 当前选中的图书名

            IbIID.Text = id;
            IbIName.Text = name;

            Bid = int.Parse(dgv.CurrentRow.Cells[0].Value.ToString());
            Bname = dgv.CurrentRow.Cells[1].Value.ToString();
            Author = dgv.CurrentRow.Cells[2].Value.ToString();
            Publisher = dgv.CurrentRow.Cells[3].Value.ToString();
            PubDate = dgv.CurrentRow.Cells[4].Value.ToString();
            Type = dgv.CurrentRow.Cells[5].Value.ToString();
            Num = int.Parse(dgv.CurrentRow.Cells[7].Value.ToString());
            Price = float.Parse(dgv.CurrentRow.Cells[6].Value.ToString());
            Introduce = dgv.CurrentRow.Cells[8].Value.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LoadBooks();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (IbIName.Text=="NULL")
            {
                MessageBox.Show("未选中图书!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 获取到图书的编号
            Dao dao = new Dao();
            dao.connect();
            string sql = $"select Intorduce from T_Book where Bid = '{IbIID.Text}'";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            string introduce = reader[0].ToString();

            // 通过对话框显示出 图书的简介
            MessageBox.Show(introduce, $"{IbIName.Text}的简介", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 拿到关键字
            string key = txtKey.Text.Trim();
            // 以关键字 对图书消息进行一个模糊查询
            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_Book where Bname like '%{key}%' or Author like '%{key}%' or Publisher like '%{key}%'";

            SqlDataReader reader = dao.read(sql);
            // 清空表格
            dgv.Rows.Clear();
            // 结果显示到表格控件中
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString(), reader[7].ToString(), reader[8].ToString(), reader[9].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 获取到当前选中的图书编号
            if (IbIID.Text=="NULL")
            {
                MessageBox.Show("未选中图书", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 删除对应数据
            Dao dao = new Dao();
            dao.connect();
            string sql = $"delete T_Book where Bid = '{int.Parse(IbIID.Text)}'";
            if (dao.Execute(sql)>0)
            {
                // 删除成功
                // 两个对应的Ibi数据的修改
                IbIID.Text = "NULL";
                IbIName.Text = "NULL";
                // 更新表格数据
                LoadBooks();
                dao.DaoClose();
                MessageBox.Show("下架成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dao.DaoClose();
                MessageBox.Show("删除失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FormUpdateBook form = new FormUpdateBook();
            form.ShowDialog();
        }
    }
}

FormReturnBook.cs

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

namespace Bookmanagementsystem
{
    public partial class FormReturnBook : Form
    {
        public FormReturnBook()
        {
            InitializeComponent();
        }

        private void LoadBorrowInfo()
        {
            dgv.Rows.Clear();

            Dao dao = new Dao();
            dao.connect();
            string sql = $"select * from T_Borrow where [Uid] = '{Form1.id}'";
            SqlDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                dgv.Rows.Add(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString(), reader[5].ToString(), reader[6].ToString());
            }

            reader.Close();
            dao.DaoClose();
        }

        private void FormReturnBook_Load(object sender, EventArgs e)
        {
            // 把当前账号租借的图书显示到表格控件中
            LoadBorrowInfo();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 检查当前选中的数据  是否为空
            if (dgv.CurrentRow.Cells[0].Value==null)
            {
                MessageBox.Show("未选中数据", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 1.图书表中对应库存增加     2.租借表 删除该条租借信息
            int key = int.Parse(dgv.CurrentRow.Cells[0].Value.ToString());
            Dao dao = new Dao();
            dao.connect();
            string sqlDelete = $"delete T_Borrow where [Key] = '{key}'";
            string sqlUpdate = $"update T_Book set Num = Num+{int.Parse(dgv.CurrentRow.Cells[6].Value.ToString())} where Bid = {int.Parse(dgv.CurrentRow.Cells[3].Value.ToString())}";
            if (dao.Execute(sqlUpdate)+dao.Execute(sqlDelete)>=2)
            {
                // 归还成功
                dao.DaoClose();
                MessageBox.Show("归还成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadBorrowInfo();
            }
            else
            {
                // 归还失败
                dao.DaoClose();
                MessageBox.Show("归还失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}

FormUpdateBook.cs 

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;

namespace Bookmanagementsystem
{
    public partial class FormUpdateBook : Form
    {
        public FormUpdateBook()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormUpdateBook_Load(object sender, EventArgs e)
        {
            txtAuthor.Text = FormManage.Author;
            txtID.Text = FormManage.Bid.ToString();
            txtintroduce.Text = FormManage.Introduce;
            txtName.Text = FormManage.Bname;
            txtNum.Text = FormManage.Num.ToString();
            txtPrice.Text = FormManage.Price.ToString();
            txtPublisher.Text = FormManage.Publisher;
            txtType.Text = FormManage.Type;
            dtpDate.Value = DateTime.Parse(FormManage.PubDate);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 判断文本框中内容
            if (txtType.Text == "" || txtPublisher.Text == "" || txtPrice.Text == "" || txtNum.Text == "" || txtName.Text == "" || txtintroduce.Text == "" || txtID.Text == "" || txtAuthor.Text == "" || dtpDate.Text == "")
            {
                MessageBox.Show("有空项", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 修改图书信息
            Dao dao = new Dao();
            dao.connect();
            string sql = $"update T_Book set Bid = '{txtID.Text}',Bname = '{txtName.Text}',Type = '{txtType.Text}',Num = '{int.Parse(txtNum.Text)}',Price = '{float.Parse(txtPrice.Text)}',Intorduce = '{txtintroduce.Text}',Author = '{txtAuthor.Text}',Publisher = '{txtPublisher.Text}',PBDate = '{dtpDate.Value}' where Bid = '{FormManage.Bid}'";
            if (dao.Execute(sql)>0)
            {
                // 修改成功
                dao.DaoClose();
                MessageBox.Show("修改成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                // 修改失败
                dao.DaoClose();
                MessageBox.Show("修改失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}

 FormUpdatePwd_Admin.cs

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

namespace Bookmanagementsystem
{
    public partial class FormUpdatePwd_Admin : Form
    {
        public FormUpdatePwd_Admin()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogon_Click(object sender, EventArgs e)
        {
            // 需要返回的可能
            if (txtNewPwd.Text==""||txtOKPwd.Text==""||txtOIdPwd.Text=="")
            {
                // 有空项的返回
                MessageBox.Show("有空项!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txtNewPwd.Text!=txtOKPwd.Text)
            {
                // 密码不匹配
                MessageBox.Show("密码不匹配!","消息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            // 原密码与数据库中的作比较
            Dao dao = new Dao();
            dao.connect();
            // 读取数据库中的原密码
            string sql = $"select Pwd from T_Admin where AdminID = '{Form1.id}'";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            if (reader[0].ToString()!=txtOIdPwd.Text)
            {
                // 密码输入不正确
                MessageBox.Show("密码不正确!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                // 修改
                sql = $"update T_Admin set Pwd = '{txtOKPwd.Text}' where AdminID = '{Form1.id}'";
                if (dao.Execute(sql)>0)
                {
                    // 修改成
                    MessageBox.Show("修改成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    reader.Close();
                    dao.DaoClose();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch 
            {
                MessageBox.Show("ERROR!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

 FormUpdatePwd_User.cs

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

namespace Bookmanagementsystem
{
    public partial class FormUpdatePwd_User : Form
    {
        public FormUpdatePwd_User()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogon_Click(object sender, EventArgs e)
        {
            // 需要返回的可能
            if (txtNewPwd.Text == "" || txtOKPwd.Text == "" || txtOIdPwd.Text == "")
            {
                // 有空项的返回
                MessageBox.Show("有空项!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (txtNewPwd.Text != txtOKPwd.Text)
            {
                // 密码不匹配
                MessageBox.Show("密码不匹配!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 原密码与数据库中的作比较
            Dao dao = new Dao();
            dao.connect();
            // 读取数据库中的原密码
            string sql = $"select Pwd from T_User where [Uid] = '{Form1.id}'";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            if (reader[0].ToString() != txtOIdPwd.Text)
            {
                // 密码输入不正确
                MessageBox.Show("密码不正确!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                // 修改
                sql = $"update T_User set Pwd = '{txtOKPwd.Text}' where [Uid] = '{Form1.id}'";
                if (dao.Execute(sql) > 0)
                {
                    // 修改成
                    MessageBox.Show("修改成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    reader.Close();
                    dao.DaoClose();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch
            {
                MessageBox.Show("ERROR!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}


FormUser.cs 

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;

namespace Bookmanagementsystem
{
    public partial class FormUser : Form
    {
        public FormUser()
        {
            InitializeComponent();
        }

        private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("确认退出吗?", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                // 退出
                this.Close();
            }
        }

        private void FormUser_Load(object sender, EventArgs e)
        {
            this.label1.Text = $"用户:{Form1.name} {Form1.id}";
        }

        private void 注销账号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 删除数据库中 当前账号信息
            if (DialogResult.Yes==MessageBox.Show("确定注销当前账号吗?","消息",MessageBoxButtons.YesNo,MessageBoxIcon.Question))
            {
                // 删除操作
                // 获取到要注销的账号
                int id = Form1.id;
                Dao dao = new Dao();
                dao.connect();
                string sql = $"delete T_User where [Uid] = '{id}'";
                if (dao.Execute(sql)>0)
                {
                    // 注销账号成功
                    MessageBox.Show("注销账号成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dao.DaoClose();

                    // 返回到登录窗口
                    this.Close();
                }
                else
                {
                    // 注销失败
                    MessageBox.Show("注销账号失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dao.DaoClose();
                }
            }
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormUpdatePwd_User form = new FormUpdatePwd_User();
            form.ShowDialog();
        }

        private void 租借图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormBorrowBook form = new FormBorrowBook();
            form.ShowDialog();
        }

        private void 归还图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormReturnBook form = new FormReturnBook();
            form.ShowDialog();
        }

        private void 查看评价ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDiscussion form = new FormDiscussion();
            form.ShowDialog();
        }

        private void 反馈到管理员ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormFeedBack form = new FormFeedBack();
            form.ShowDialog();
        }
    }
}

SQLServer\BookMS


T_Admin


T_Book


T_Borrow


T_Discussion

 


T_FeedBack 


T_User


原文地址:https://blog.csdn.net/zoushier/article/details/145110890

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!