图书馆管理系统(自学记录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
建议去看别的博主的内容学习如何安装和简单的使用。
三、框架
这是我设计完后画的结构,设计图中,总会想着哪里不完善,然后再一点点添加。
四、相关功能的实现
1、登录界面
2、注册界面
3、用户主界面
3.1图书查看和借阅
3.2当前借阅的图书和归还
3.3 系统下面的用户反馈
4、管理员主界面
4.1图书管理
4.1.1 添加图书
4.1.2修改图书
4.2用户管理
4.2.1管理员查看用户归还情况
4.2.2查看用户反馈
五、代码部分
这里给出部分代码,需要代码的,可以去我的工房购买哔哩哔哩工房 (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)