图书馆管理系统(自学记录C#)


目录

前言

一、数据库的选择

二、数据库的安装

1.安装sql server  链接在右边   

2.安装SQL Server Management Studio

三、框架

四、相关功能的实现

 1、登录界面                  

 2、注册界面

 3、用户主界面

 4、管理员主界面

 4.1图书管理

 4.2用户管理

 五、代码部分

1、数据库操作的代码

2、登录界面的代码

3、注册界面的代码

总结

前言

主要是实训需要用C#做一个东西,然后选择了数据库,选题选了图书馆管理系统,之后再b站学习了一下想把这阶段做的东西来一个汇总。


一、数据库的选择

Access的话驱动和我电脑有点冲突,所以我用的是sql server

二、数据库的安装

1.安装sql server   

2.安装SQL Server Management Studio

可以去csdn里搜索安装,我这里给出一个安装链接 SQL Server 下载 | Microsoft

建议去看别的博主的内容学习如何安装和简单的使用。

三、框架

        694ab13056ac44f79dd7099e0402ba02.png


这是我设计完后画的结构,设计图中,总会想着哪里不完善,然后再一点点添加。

四、相关功能的实现

  1、登录界面

                37fb68ed65f241efbbb72a35281583ee.png

                  

2、注册界面

33699388a7a24285951c33547456bc1b.png

3、用户主界面

                        15b08710730c455bb6b2e259dcb26aab.png

 3.1图书查看和借阅

        60001739151f4313a4ebf590c196ada3.png

  3.2当前借阅的图书和归还

5ae7b181edf146359c051a04b5d1679d.png

  3.3 系统下面的用户反馈

ada2ed3e96e34d12bec8c2f30f6a4868.png

29dcaa995c794187b494714db784b055.png

   4、管理员主界面

c4f16c9f94c94afe8517e915b4ecb38f.png

 4.1图书管理

a54d84d0df8440f3b4c7ad9cdec0f858.png

  4.1.1 添加图书

          41608952aafb4c909310ba56a7c6eda4.png

  4.1.2修改图书

e10e0764ccd64535854602ba714afa21.png

 4.2用户管理

50b0e987498b4fe2be40a62b899aa279.png

 4.2.1管理员查看用户归还情况

65bda6e8eb704ce398a1f0f5101412d2.png

 4.2.2查看用户反馈

3830ab5b7e274a84865ee83ff17997bf.png

 五、代码部分

这里给出部分代码,需要代码的,可以去我的工房购买哔哩哔哩工房 (bilibili.com)

1、数据库操作的代码

using System.Data.SqlClient;
using System.Drawing;

namespace WindowsFormsApp1
{
    internal class Dao
    {
        SqlConnection sc;
        public SqlConnection connect()
        {
            string str = @"Data Source=画船听雨眠;Initial Catalog=BookDB;Integrated Security=True";
            sc = new SqlConnection(str);
            sc.Open();
            return sc;
        }
        public SqlCommand command(string sql) {
            SqlCommand cmd = new SqlCommand(sql, connect());
            return cmd;

        }
        public int Execute(string sql)//返回数据库里受影响的行数
        {
            return command(sql).ExecuteNonQuery();

        }
        public SqlDataReader read(string sql)//读取
        {
            return command(sql).ExecuteReader();

        }
        public void DaoClose()
        { 
            sc.Close();//关闭数据库链接
        }
    }
}

2、登录界面的代码

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 WindowsFormsApp1
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//登录按钮
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                login();
            }
            else
            {
                MessageBox.Show("输入有空项,请重新输入!");
            }
        }
        //登录判断
        public void login()
        {
            if (radioButtonuser.Checked == true)
            {
                Dao dao = new Dao();
                // string sql = "select*from t_user where id='" + textBox1.Text + "'and psw='" + textBox2.Text + "'";
                // string sql1=string.Format("select*from t_user where id='{0}'and psw='{1}'",textBox1.Text,textBox2.Text);
                string sql = $"select*from t_user where id='{textBox1.Text}'and psw='{textBox2.Text}'";
                IDataReader dc = dao.read(sql);//执行sql语句
                if (dc.Read())//判断sql语句是否执行成功
                {
                   Data.UID = dc["id"].ToString();//将读取到的id值赋给Data.UID
                    Data.UName = dc["name"].ToString();//将读到的name值赋给Data.UName

                    MessageBox.Show("登录成功!");//对话框显示登录成功
                    user1 user = new user1();//打开用户窗体主界面
                    this.Hide();//隐藏登录界面
                    user.ShowDialog();//对登录窗体无法操作,只能先关掉对话框
                    this.Show();//显示登录界面
                }
                else
                {
                    MessageBox.Show("登录失败!");//sql执行失败就对话框显示登录失败

                }
                dao.DaoClose();//关闭数据库连接
                //MessageBox.Show(dc[0].ToString()+ dc[1].ToString());
            }
            if (radioButtonadmin.Checked == true)
            {
                Dao dao = new Dao();
                // string sql = "select*from t_user where id='" + textBox1.Text + "'and psw='" + textBox2.Text + "'";
                // string sql1=string.Format("select*from t_user where id='{0}'and psw='{1}'",textBox1.Text,textBox2.Text);
                string sql = $"select*from t_admin where id='{textBox1.Text}'and psw='{textBox2.Text}'";
                IDataReader dc = dao.read(sql);
                if (dc.Read())//从第一行开始读
                {
                    MessageBox.Show("登录成功!");
                    admin1 admin = new admin1();
                    this.Hide();
                    admin.ShowDialog();//对登录窗体无法操作,只能先关掉对话框
                    this.Show();
                }
                else
                {
                    MessageBox.Show("登录失败!");
                }
                dao.DaoClose();
            }


        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)//注册按钮
        {
            register a= new register();//注册窗体创建一个a
            a.ShowDialog();//打开窗体,以对话框的模式显示a的内容
        }

        private void button2_Click(object sender, EventArgs e)//取消按钮
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
    }
}

3、注册界面的代码

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 WindowsFormsApp1
{
    public partial class register : Form
    {
        public register()
        {
            InitializeComponent();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)//确认按钮
        {
            Dao dao = new Dao();//用Dao 类定义了一个dao的对象
            string sql = $"insert into t_user values('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}')";//往表里对面的列插入对应的值
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" )//判断文本框是否为空
            {
                MessageBox.Show("输入信息不完整,请重新输入!");
            }
         
            if (dao.Execute(sql) > 0)//判断sql语句是否成功,如果成功那么受影响的行数为1
            {
                MessageBox.Show("注册成功!");
                TextClear();//清空文本
            }
            
        }
        public void TextClear()//写的一个清空文本框控件的函数,一个个清太麻烦了
        {
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {
                    TextBox textBox = (TextBox)c;
                    textBox.Text = string.Empty;
                }

            }
        }
        private void register_Load(object sender, EventArgs e)
        {

        }
    }
}

图书馆管理系统演示

总结

完成这种数据库的项目最主要是熟悉数据库的操作函数,学会在SQL Server Management Studio里设计表、编辑表,然后先在SQL Server Management Studio里用sql语句增删改查成功后,再把语句复制到C++里,一般报错和出问题都是因为sql语句错误。

资源链接:哔哩哔哩工房 (bilibili.com)

  • 6
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值