C# 图书管理系统(MySQL)——代码(四)
目录1、通用代码介绍2、登录界面设计3、用户注册界面设计4、管理员注册界面设计5、用户查询图书界面设计6、管理员管理图书界面设计7、系统网盘地址
·
目录
1、通用代码介绍
1)C#使用MySQL
- 使用using来调用mysql连接
using MySql.Data.MySqlClient;
- 连接数据库Book
string strcon = "Database=Book;Data Source=localhost;User Id=root;Password=123456";//连接数据库的参数
MySqlConnection mysqlcon = new MySqlConnection(strcon);//连接数据库
mysqlcon.Open();
参数含义:
【Data Source】 :服务器IP地址
【Database】: 数据库名称
【User Id】 :数据库用户名
【Password】: 数据库密码
2)跳转界面
本界面跳转到Form1界面
new Form1().Show();//Form1显示
this.Hide();//本界面隐藏
3)字符串转变为数值(str转换为int)
将txtprice.Text的字符串转变为int型赋值到num
int num ;
int.TryParse(txtprice.Text, out num);
2、登录界面设计
1)界面图片 (Form1 )
2)代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MySqlConnection load()//连接数据库
{
string strcon = "Database=Book;Data Source=localhost;User Id=root;Password=123456";
MySqlConnection mysqlcon = new MySqlConnection(strcon);
mysqlcon.Open();
return mysqlcon;
}
public Boolean pan_duan(String str)
{
MySqlConnection con = load();
MySqlCommand com = new MySqlCommand(str, con);
//判断executeScalar方法返回的参数是否大于0,大于0表示查找有数据
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
return true;
}
//用户名和密码验证错误,提示错误。
else
{
MessageBox.Show("用户名或密码错误!");
return false;
}
}
bool judge = true;//设计判断布尔值
//登录按钮事件
private void btn_login_Click(object sender, EventArgs e)
{
string id = txtid.Text;
string pwd = txtpwd.Text;
// 验证用户输入是否为空
if (id.Equals("") || pwd.Equals(""))
{
MessageBox.Show("用户名或密码不能为空!");
}
// 验证用户名和密码是否与数据库匹配
else
{
foreach (Control c1 in groupBox1.Controls)// 遍历groupBox1控件(判断选择哪个按钮)
{
string str = c1.Name;//获取控件的命名
RadioButton rb = (RadioButton)c1;
if (str == "radioButton1")//用户登录
{
if (rb.Checked)//选中
{
judge = false;
string strcmd = "select count(*) from user where id="+id+" and pwd='"+pwd+"'";
if (pan_duan(strcmd))//判断用户中用户名、密码是否正确,即数据库user表中是否存在相同数据
{
Form4 f4 = new Form4();
f4.Show();
this.Hide();
}
}
}
else //管理员登录
{
if (rb.Checked)//选中
{
judge = false;
string strcmd = "select count(*) from manager where id='" + id + "' and pwd='" + pwd + "'";
if (pan_duan(strcmd))//判断管理员的用户名、密码是否正确,即数据库manager表中是否存在相同数据
{
Form5 f5 = new Form5();
f5.Show();
this.Hide();
}
}
}
}
if (judge)
{
MessageBox.Show("未选择身份,无法登录!");
}
}
}
//注册按钮事件
private void btn_register_Click(object sender, EventArgs e)
{
foreach (Control c1 in groupBox1.Controls)// 遍历groupBox1控件(判断选择哪个按钮)
{
string str = c1.Name;//获取控件的命名
RadioButton rb = (RadioButton)c1;
if (str == "radioButton1")//用户注册
{
if (rb.Checked)//选中
{
judge = false;
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
}
else //管理员注册
{
if (rb.Checked)//选中
{
judge = false;
Form3 f3 = new Form3();
f3.Show();
this.Hide();
}
}
}
if (judge)
{
MessageBox.Show("未选择身份,无法注册!");
}
}
//退出按钮事件
private void btn_exit_Click(object sender, EventArgs e)
{
this.Close();
}
}
3、用户注册界面设计
1)界面图片(Form2)
2)代码
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public MySqlConnection load()//连接数据库
{
string strcon = "Database=Book;Data Source=localhost;User Id=root;Password=123456";
MySqlConnection mysqlcon = new MySqlConnection(strcon);
mysqlcon.Open();
return mysqlcon;
}
public void execute(String str)//执行增、删、改 sql语句
{
MySqlConnection con = load();
MySqlCommand com = new MySqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
}
//注册按钮事件
private void button1_Click(object sender, EventArgs e)
{
Boolean f = true;
//循环遍历判断TextBox是否为空(用户未填信息)
foreach (Control c1 in groupBox1.Controls)// 遍历groupBox1控件
{
string s = c1.GetType().ToString();//获取控件类型
string x = "System.Windows.Forms.TextBox";
if (s == x )
{
TextBox tb = (TextBox)c1;
if (tb.Text == "")
{
MessageBox.Show("有信息未填,注册失败!");
f = false;
break;
}
}
}
//获取用户注册性别信息sex
string sex = "";
if (radioButton1.Checked)
{
sex = radioButton1.Text;
}
else if (radioButton2.Checked)
{
sex = radioButton2.Text;
}
else
{
f = false;
MessageBox.Show("未选择性别,注册失败!");
}
if (f)
{
Regex regex = new Regex(@"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");//邮箱正则表达式
MySqlConnection mysqlcon=load();//调用load函数连接数据库
//将用户名与数据库中的用户名进行比较
String sql = "select count(*) from user where id='" + txtname.Text+"'";
MySqlCommand com = new MySqlCommand(sql, mysqlcon);
if ( Convert.ToInt32(com.ExecuteScalar()) > 0)
{
MessageBox.Show("该用户名已注册,注册失败!");
}
else if (txtpwd.Text != txtpwd2.Text)
{
MessageBox.Show("两次输入密码不同,注册失败!");
}
else if (regex.IsMatch(txt_email.Text)==false)
{
MessageBox.Show("邮箱格式错误!");
}
else
{
//将注册信息放入数据库中
string addstr = "insert into user (id,pwd,sex,e_mail) values ('"+txtname.Text+"','"+txtpwd.Text+"','"+sex+"','"+txt_email.Text+"')" ;
execute(addstr);
MessageBox.Show("注册成功!");
this.Close();
Form1 f1 = new Form1();
f1.Show();
}
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)//关闭界面
{
Form1 f1 = new Form1();
f1.Show();
}
}
4、管理员注册界面设计
1)界面图片(Form3)
2)代码
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
string key = "123456";//设置验证密钥
Boolean f = true;
public MySqlConnection load()//连接数据库
{
string strcon = "Database=Book;Data Source=localhost;User Id=root;Password=123456";
MySqlConnection mysqlcon = new MySqlConnection(strcon);
mysqlcon.Open();
return mysqlcon;
}
public void execute(String str)//执行增、删、改 sql语句
{
MySqlConnection con = load();
MySqlCommand com = new MySqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
}
//注册按钮事件
private void button1_Click(object sender, EventArgs e)
{
foreach (Control c1 in groupBox1.Controls)// 遍历groupBox1控件
{
string s = c1.GetType().ToString();//获取控件类型
string x = "System.Windows.Forms.TextBox";
if (s == x)
{
TextBox tb = (TextBox)c1;
if (tb.Text == "")
{
MessageBox.Show("有信息未填,注册失败!");
f = false;
break;
}
}
}
if (f)
{
MySqlConnection mysqlcon = load();
//将用户名与数据库中的用户名进行比较
String sql = "select count(*) from manager where id='" + txtname.Text+"'";
MySqlCommand com = new MySqlCommand(sql, mysqlcon);
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
MessageBox.Show("该用户名已注册,注册失败!");
}
else if (txtpwd.Text != txtpwd2.Text)
{
MessageBox.Show("两次输入密码不同,注册失败!");
}
else if (txtmima.Text != key)
{
MessageBox.Show("验证密钥错误,注册失败!");
}
else
{
//将注册信息放入数据库中
string addstr = "insert into manager (id,pwd) values ('" + txtname.Text + "','" + txtpwd.Text + "')";
execute(addstr);
MessageBox.Show("注册成功!");
this.Close();
Form1 f1 = new Form1();
f1.Show();
}
}
}
private void Form3_FormClosed(object sender, FormClosedEventArgs e)//关闭界面
{
Form1 f1 = new Form1();
f1.Show();
}
}
5、用户查询图书界面设计
1)界面图形(Form4)
2)代码
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
public MySqlConnection load() //连接数据库
{
string strcon = "Database=Book;Data Source=localhost;User Id=root;Password=123456";
MySqlConnection mysqlcon = new MySqlConnection(strcon);
mysqlcon.Open();
return mysqlcon;
}
public void display(String strcmd)//将数据库数据显示在dataGridView1 控件中
{
MySqlConnection mysqlcon = load();
MySqlDataAdapter da = new MySqlDataAdapter(strcmd, mysqlcon);
DataSet ds = new DataSet();
da.Fill(ds,"s");
dataGridView1.DataSource = ds.Tables["s"];
}
//显示数据库Book中表book中所图书信息
private void Form4_Load(object sender, EventArgs e)
{
string str = "select book_id 书号,book_name 书名,author 作者,publisher 出版社,date 出版日期,price 定价,class 图书分类,num 库存数量 from book_information";
display(str);
}
//“显示所有图书信息”按钮事件
private void button2_Click(object sender, EventArgs e)
{
string str = "select book_id 书号,book_name 书名,author 作者,publisher 出版社,date 出版日期,price 定价,class 图书分类,num 库存数量 from book_information";
display(str);
}
//查询按钮事件
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)//按书名模糊查询
{
string str = "select * from book_information where book_name like'%" + txt.Text + "%'";
display(str);
}
else if (radioButton2.Checked)//按书号查询
{
string str = "select * from book_information where book_id='" + txt.Text + "'";
display(str);
}
else if (radioButton3.Checked)//按作者姓名查询
{
string str = "select * from book_information where author='"+txt.Text+"'";
display(str);
}
else
{
MessageBox.Show("请选择查询方式!");
}
}
//单击文本框,清空原有数据
private void txt_Click(object sender, EventArgs e)
{
txt.Text = "";
}
//返回登录首界面
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
new Form1().Show();
}
}
6、管理员管理图书界面设计
1)界面设计(Form5)
2)代码
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
//全局变量
string str = "select book_id 书号,book_name 书名,author 作者,publisher 出版社,date 出版日期,price 定价,class 图书分类,num 库存数量 from book_information";
public MySqlConnection load() //连接数据库
{
string strcon = "Database=Book;Data Source=localhost;User Id=root;Password=123456";
MySqlConnection mysqlcon = new MySqlConnection(strcon);
mysqlcon.Open();
return mysqlcon;
}
public void display(String str)//将数据库查询数据显示在dataGridView1 控件中
{
MySqlConnection mysqlcon = load();
MySqlDataAdapter da = new MySqlDataAdapter(str, mysqlcon);
DataSet ds = new DataSet();
da.Fill(ds, "s");
dataGridView1.DataSource = ds.Tables["s"];
}
public void execute(String str)//执行增、删、改 sql语句
{
MySqlConnection con = load();
MySqlCommand com = new MySqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
}
public Boolean check()//判断是否有文本信息未填
{
foreach (Control c1 in groupBox1.Controls)// 遍历groupBox1控件
{
string s = c1.GetType().ToString();//获取控件类型
string x = "System.Windows.Forms.TextBox";
if (s == x)
{
TextBox tb = (TextBox)c1;
if (tb.Text == "")
{
return false;
}
}
}
return true;
}
//清空文本数据
public void delete()
{
txtid.Text = "";
txtname.Text = "";
txtauthor.Text = "";
txtpublisher.Text = "";
txtdate.Text = "";
txtprice.Text = "";
txtclass.Text = "";
txtnum.Text = "";
txtnum2.Text = "";
}
//显示图书信息
private void Form5_Load(object sender, EventArgs e)
{
display(str);
}
//将表格(dataGridView1控件)单击选中的数据传到界面上方空白文本框
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >-1)//行索引大于等于0
{
MySqlConnection mysqlcon = load();
string str = "select * from book_information";
MySqlDataAdapter da = new MySqlDataAdapter(str, mysqlcon);
DataTable dt = new DataTable();
da.Fill(dt);
txtid.Text = dt.Rows[e.RowIndex][0].ToString();
txtname.Text = dt.Rows[e.RowIndex][1].ToString();
txtauthor.Text= dt.Rows[e.RowIndex][2].ToString();
txtpublisher.Text = dt.Rows[e.RowIndex][3].ToString();
txtdate.Text = dt.Rows[e.RowIndex][4].ToString();
txtprice.Text = dt.Rows[e.RowIndex][5].ToString();
txtclass.Text = dt.Rows[e.RowIndex][6].ToString();
txtnum.Text = dt.Rows[e.RowIndex][7].ToString();
}
}
//添加图书按钮事件
private void btn_add_Click(object sender, EventArgs e)
{
MySqlConnection mysqlcon = load();
//将用户名与数据库中的用户名进行比较
String sql = "select count(*) from book_information where book_id='" + txtid.Text + "'";
MySqlCommand com = new MySqlCommand(sql, mysqlcon);
if (!check())
{
MessageBox.Show("有信息未填!");
}
else if (Convert.ToInt32(com.ExecuteScalar()) > 0)//有该书
{
MessageBox.Show("该书已存在,图书信息添加失败!");
}
else
{
int num;
int.TryParse(txtnum.Text, out num);
int price;
int.TryParse(txtprice.Text, out price);
string addstr = "insert into book_information (book_id,book_name,author,publisher,date,price,class,num) values ('" + txtid.Text + "','" + txtname.Text + "','" + txtauthor.Text + "','" + txtpublisher.Text + "','" + txtdate.Text + "'," + price + ",'" + txtclass.Text + "'," + num + ")";
execute(addstr);
display(str);
delete();
MessageBox.Show("图书信息添加成功!");
}
}
//清空数据按钮事件
private void btn_clear_Click(object sender, EventArgs e)
{
delete();
}
//修改信息按钮事件
private void btn_update_Click(object sender, EventArgs e)
{
if (!check())
{
MessageBox.Show("有信息未填!");
}
else
{
int num;
int.TryParse(txtnum.Text, out num);
int price;
int.TryParse(txtprice.Text, out price);
string upstr = "update book_information set num=" + num + ",book_name='" + txtname.Text + "',author='" + txtauthor.Text + "',publisher='" + txtpublisher.Text + "',date='" + txtdate.Text + "',price=" + price + ",class='" + txtclass.Text + "' where book_id='" + txtid.Text + "'";
execute(upstr);
display(str);
delete();
MessageBox.Show("图书信息修改成功!");
}
}
//删除图书按钮事件
private void btn_delete_Click(object sender, EventArgs e)
{
if (!check())
{
MessageBox.Show("有信息未填!");
}
else
{
string delstr = "delete from book_information where book_id ='" + txtid.Text + "'";
execute(delstr);
display(str);
delete();
MessageBox.Show("图书信息删除成功!");
}
}
//图书数量减少按钮事件(删除数量<库存数量:库存减少;删除数量>库存数量:删除图书)
private void delete_Click(object sender, EventArgs e)
{
int num;
int.TryParse(txtnum2.Text, out num);
MySqlConnection mysqlcon = load();
String sql = "select num from book_information where book_id='" + txtid.Text + "'";
MySqlCommand com = new MySqlCommand(sql, mysqlcon);
int i = Convert.ToInt32(com.ExecuteScalar());
if (i - num> 0)//库存够
{
string upstr = "update book_information set num=num-" + num + " where book_id='" + txtid.Text + "'";
execute(upstr);
MessageBox.Show("图书库存数量减少成功!")
}
else
{
string delstr = "delete from book_information where book_id ='" + txtid.Text + "'";
execute(delstr);
MessageBox.Show("该图书库存不够,图书信息删除!");
}
display(str);
delete();
}
//图书数量增加按钮事件
private void add_Click(object sender, EventArgs e)
{
int num;
int.TryParse(txtnum2.Text, out num);
string addstr = "update book_information set num=num+" + num + " where book_id='" + txtid.Text + "'";
execute(addstr);
display(str);
delete();
MessageBox.Show("图书库存数量增加成功!");
}
//“返回全部图书信息”事件
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
display(str);
}
//“查询”事件
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (toolStripComboBox1.Text == "按书名查询")//按书名模糊查询
{
string sql = str+" where book_name like '%" + txt.Text + "%'";
display(sql);
}
else if (toolStripComboBox1.Text == "按书号查询")//按书号查询
{
string sql = str + " where book_id ='" + txt.Text + "'";
display(sql);
}
else if (toolStripComboBox1.Text == "按作者姓名查询")//按作者姓名查询
{
string sql = str + " where author ='" + txt.Text + "'";
display(sql);
}
else
{
MessageBox.Show("请选择查询方式!");
}
}
//单击文本框,清空数据
private void txt_Click(object sender, EventArgs e)
{
txt.Text = "";
}
//返回按钮——返回登录首界面
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
new Form1().Show();
}
}
7、系统网盘地址
链接:https://pan.baidu.com/s/1qYhGbl0sUY9g2Z0uXSqMlg
提取码:w901
觉得不错的小伙伴可以点赞加收藏哦。ღ( ´・ᴗ・` )
C#参考资料网站:C# WinForm界面设计教程(C# Windows窗体应用程序)
更多推荐
已为社区贡献1条内容
所有评论(0)