图书管理系统,这一类系统通过C# Winform窗体实现,对于初学者来说对着代码写都出现很多问题,今终于敲出来了,对于这样的系统有了初步的认识,后续还会学着对系统功能增加写一写。
https://download.csdn.net/download/dongdongxu520/88828199
PS:最多的问题字符没有敲对,数据库连接失败了,对于初学可以一起学习讨论,一起进步。
图书管理系统功能实现:
a.图书管理系统实现窗体用户和管理员登录,
b.用户查阅和归还图书,管理员查阅图书,增加图书,修改图书,删除图书,借出,归还
1.登录界面
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 BookMS
{
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}'";
IDataReader dc = dao.read(sql);
// dc.Read();
// MessageBox.Show(dc[0].ToString()+dc["name"].ToString());
if (dc.Read())
{
MessageBox.Show("登陆成功");
user1 user = new user1();
this.Hide();
user.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
dao.DaoClose();
}
//管理员
if (radioButtonAdmin.Checked == true)
{
Dao dao = new Dao();
//三种弹出对话框方法
//string sql = "select * from t_user where id='' "+textBox1.Text+" and psw=''"+textBox2.Text+"";
//string sql2 = 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);
//dc.Read();
// MessageBox.Show(dc[0].ToString()+dc["name"].ToString());
if (dc.Read())
{
MessageBox.Show("登陆成功");
admin1 a = new admin1();
this.Hide();
a.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
}
MessageBox.Show("单选框请先选中");
}
private void login_Load(object sender, EventArgs e)
{
this.BackgroundImage = Image.FromFile("D:\\VS\\MyProject\\2024\\202402项目\\20240209\\BookMS\\BookMS\\image\\2.jpg"); // 设置背景图为2.jpg
this.BackgroundImageLayout = ImageLayout.Stretch; // 设置背景图自适应
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
2.数据库连接Dao类和Data类
Dao类
using System.Data.SqlClient;
namespace BookMS
{
class Dao
{
SqlConnection sc;
public SqlConnection connect()
{
string str = @"Data Source=Win10-2024LTZKD\MSSQLSERVER01;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();//关闭数据库连接
}
}
}
Data类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookMS
{
class Data
{
public static string UID = "", UName = "";//登录用户名的ID和姓名
}
}
3.用户和管理员窗体界面