C#连接数据库实现开发图书管理系统操作代码
//客户端登录界面(Form1.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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Data.SqlClient;
namespace 图书管理系统1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(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 dbo.用户表 where 用户ID='{textBox1.Text}'and 用户密码='{textBox2.Text}'";
IDataReader dc = dao.read(sql);
if(dc.Read())
{
Data.UID = dc["用户ID"].ToString();
Data.UName = dc["用户姓名"].ToString();
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 管理员表 where 管理员ID='{textBox1.Text}'and 密码='{textBox2.Text}'";
IDataReader dc = dao.read(sql);
if (dc.Read())
{
MessageBox.Show("登陆成功!");
admin1 a = new admin1();
this.Hide();
a.ShowDialog();//用户窗体进行实例化
this.Show(); //显示旧窗体
}
else
{
MessageBox.Show("登陆失败!");
}
}
MessageBox.Show("单选框请先选中!");
}
}
}
//program.cs(C#页面)
using System;
using System.Windows.Forms;
namespace 图书管理系统1
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
// Application.Run(new admin2());
}
}
}
//Data.cs(C#页面)
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 图书管理系统1
{
internal class Data//存储登陆的一些数据
{
public static string UID = "", UName = "";//用户ID和姓名默认为空
}
}
//Dao.cs(C#页面)数据库连接代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Security.Policy;
using System.Runtime.Remoting.Messaging;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Data;
namespace 图书管理系统1
{
class Dao//数据库连接
{
SqlConnection sc;
public SqlConnection connect()
{
string str = @"Data Source=WIN-GIHP114UG9H;Initial Catalog=图书管理;Integrated Security=SSPI";//数据库连接字符串windons登录
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();
}
}
}
//登录管理员的主页面(admin1.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 图书管理系统1
{
public partial class admin1 : Form
{
public admin1()
{
InitializeComponent();
}
private void 图书管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
admin2 admin = new admin2();
admin.ShowDialog();//实现跳转
}
}
}
//管理员图书管理页面(admin2.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 图书管理系统1
{
public partial class admin2 : Form
{
public admin2()
{
InitializeComponent();
}
private void admin2_Load(object sender, EventArgs e)//页面
{
Table();
label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString()+ dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号和书名
}
//从数据库读取数据显示在表格控件
public void Table()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = "SELECT * FROM dbo.图书表";
IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据
while(dc.Read())//读完返回一个false跳出循环
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.DaoClose();
}
public void Table图书ID()//根据书号进行查询
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"SELECT * FROM dbo.图书表 WHERE 图书ID='{textBox1.Text}'";
IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据
while (dc.Read())//读完返回一个false跳出循环
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.DaoClose();
}
public void Table图书名Name()//根据书名进行查询,模糊语句
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"SELECT * FROM dbo.图书表 WHERE 图书名 like'%{textBox2.Text}%'";
IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据
while (dc.Read())//读完返回一个false跳出循环
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.DaoClose();
}
private void button3_Click(object sender, EventArgs e)//删除图书
{
try
{
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号
label2.Text = id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
DialogResult dr = MessageBox.Show("确认删除吗?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
string sql = $"DELETE FROM dbo.图书表 WHERE 图书ID='{id}'";
Dao dao = new Dao();
if (dao.Execute(sql) > 0)
{
MessageBox.Show("删除成功!");
Table();
}
else
{
MessageBox.Show("删除失败!"+sql);
}
dao.DaoClose();
}
}
catch
{
MessageBox.Show("请先在表格中选中要删除的图数记录!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void dataGridView1_Click(object sender, EventArgs e)//点击click选定要删除的图书
{
label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号和书名
}
private void button1_Click(object sender, EventArgs e)//添加图书
{
admin21 a = new admin21();
a.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string 图书ID=dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string 图书名 = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
string 作者 = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
string 出版社 = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
string 库存 = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
admin22 admin = new admin22(图书ID, 图书名, 作者, 出版社, 库存);
admin.ShowDialog();
Table();//刷新数据
}
catch
{
MessageBox.Show("ERROR!");
}
}
private void button5_Click(object sender, EventArgs e)
{
Table图书ID();
}
private void button6_Click(object sender, EventArgs e)
{
Table图书名Name();
}
private void button4_Click(object sender, EventArgs e)
{
Table();
textBox1.Text = "";//文本框清空
textBox1.Text = "";
}
}
}
//管理员图书管理——添加图书(admin21.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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace 图书管理系统1
{
public partial class admin21 : Form
{
public admin21()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")
{
Dao dao = new Dao();
string sql = $"INSERT INTO dbo.图书表 VALUES('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}','{textBox5.Text}')";
int n = dao.Execute(sql);
if (n > 0)
{
MessageBox.Show("添加成功!");
}
else
{
MessageBox.Show("添加失败!");
}
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
else
{
MessageBox.Show("输入不允许有空项!");
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
}
}
}
//管理员图书管理——修改图书(admin22.cs窗口体系)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 图书管理系统1
{
public partial class admin22 : Form
{
string ID = "";//公用变量
public admin22()
{
InitializeComponent();
}
public admin22(string 图书ID, string 图书名, string 作者, string 出版社, string 库存)
{
InitializeComponent();
ID=textBox1.Text = 图书ID;
textBox2.Text = 图书名;
textBox3.Text = 作者;
textBox4.Text = 出版社;
textBox5.Text = 库存;
}
private void button1_Click(object sender, EventArgs e)//修改按钮的代码
{
string sql = $"UPDATE dbo.图书表 SET 图书ID='{textBox1.Text}',图书名='{textBox2.Text}',作者='{textBox3.Text}',出版社='{textBox4.Text}',库存='{textBox5.Text}' WHERE 图书ID='{ID}'";
Dao dao = new Dao();
if(dao.Execute(sql)>0)
{
MessageBox.Show("修改成功!");
this.Close();
}
}
}
}
//用户登录主页面(user1.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 图书管理系统1
{
public partial class user1 : Form
{
public user1()
{
InitializeComponent();
label2.Text = $"欢迎{Data.UName}登录";
}
private void 图书查看和借阅ToolStripMenuItem_Click(object sender, EventArgs e)
{
user2 a = new user2();//跳转页面
a.ShowDialog();
}
private void 当前图书的借出和归还ToolStripMenuItem_Click(object sender, EventArgs e)
{
user3 us = new user3();
us.ShowDialog();
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("HELP!");
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
//用户图书管理——图书的查看和借阅(user2.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 图书管理系统1
{
public partial class user2 : Form
{
public user2()
{
InitializeComponent();
Table();
}
private void user2_Load(object sender, EventArgs e)
{
}
public void Table()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = "SELECT * FROM dbo.图书表";
IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据
while (dc.Read())//读完返回一个false跳出循环
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.DaoClose();
}
private void button1_Click(object sender, EventArgs e)//借出图书
{
string 图书ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//书号
int 库存 = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());//库存
if(库存<1)
{
MessageBox.Show("库存不够,请联系管理员购入!");
}
else
{
string sql = $"INSERT INTO dbo.借书表([用户ID],[图书ID] ,[截止日期] )VALUES ('{Data.UID}','{图书ID}',GETDATE());UPDATE dbo.图书表 SET 库存=库存-1 WHERE 图书ID='{图书ID}'";
Dao dao = new Dao();
if(dao.Execute(sql)>1)//执行两条sql语句大于一才是都执行成功
{
MessageBox.Show($"用户:{Data.UName}借出了图书{图书ID}!");
Table();
}
else
{
MessageBox.Show("借书失败!");
}
}
}
}
}
//用户图书管理——我借到和归还的图书(user3.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.Web;
using System.Windows.Forms;
namespace 图书管理系统1
{
public partial class user3 : Form
{
public user3()
{
InitializeComponent();
Table();
}
public void Table()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"SELECT [no],[图书ID],[截止日期] FROM dbo.借书表 WHERE 用户ID='{Data.UID}'";
IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据
while (dc.Read())//读完返回一个false跳出循环
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString());//三个参数
}
dc.Close();
dao.DaoClose();
}
private void button1_Click(object sender, EventArgs e)
{
string 图书ID = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号
string no= dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取编号
string sql = $"DELETE FROM dbo.借书表 WHERE [no]={no};UPDATE dbo.图书表 SET 库存=库存+1 WHERE 图书ID='{图书ID}'";
Dao dao = new Dao();
if(dao.Execute(sql)>1)
{
MessageBox.Show("归还成功!");
Table();
}
}
}
}