项目结构:
各个界面的功能和主要代码
主界面:
主要代码
Application.Run(new login());
用户登录之后的界面
主要代码
ToolStripMenuItem_Click(object sender, EventArgs e)
{
user1 u1 = new user1();
u1.ShowDialog();
}
private void 借书和还书ToolStripMenuItem_Click(object sender, EventArgs e)
{
user3 u3 = new user3();
u3.ShowDialog();
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("您好!有什么能帮助你的?");
}
private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 联系管理员ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("您好!我是这个系统的管理人员,有什么需要帮助的吗?");
}
private void 投诉与建议ToolStripMenuItem_Click(object sender, EventArgs e)
{
useradvise ad = new useradvise();
ad.ShowDialog();
}
点击查看和借阅图书,可以查看一些书籍,并且可以查询想要的书籍
还书界面,当然现在没有借书,如果有借书,会有相应的记录
还有一些其它的功能
点击联系管理员
点击投诉与建议
当然,点击退出登录,那么就会退出当前登录的用户。
然后是管理员的代码和功能:
我们选择管理员,然后输入管理员的信息和密码登录
点击登陆
登陆的主要代码
public void Login() {
//用户
if (radioButtonU.Checked == true)
{
Dao dao = new Dao();
// string sql = "select * from t_usersg where id = '" + textBox3.Text + "'and psw = '" + textBox4.Text + "'";
//String.Format("select * from t_usersg where id='{0}'and='{1}'",textBox3.Text,textBox4.Text);
string sql1 = $"select * from t_usersg where id='{textBox3.Text}'and psw='{textBox4.Text}'";
// MessageBox.Show("1");可以用来测试连接数据库是否成功
IDataReader dc = dao.read(sql1);
if (dc.Read())
{
Data.UID = dc["id"].ToString();
Data.UName = dc["name"].ToString();
MessageBox.Show("登陆成功");
user users = new user();
this.Hide();
users.ShowDialog();
this.Show();
}
else {
MessageBox.Show("登陆失败");
}
dao.DaoClose();
}
//管理员
if (radioButtonA.Checked == true)
{
Dao dao = new Dao();
string sql1 = $"select * from t_adm where id='{textBox3.Text}'and psw='{textBox4.Text}'";
// MessageBox.Show("1");可以用来测试连接数据库是否成功
IDataReader dc = dao.read(sql1);
if (dc.Read())
{
MessageBox.Show("登陆成功");
admin a = new admin();
//a.Show();这个方法可以对登陆界面窗体进行操作
this.Hide();
a.ShowDialog();
this.Show();
}
else
{
MessageBox.Show("登陆失败");
}
dao.DaoClose();
}
}
管理员登陆之后的界面
点击系统,我们的管理员可以和普通用户一样,借书还书,退出登录
private void 图书管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("您好!有什么能帮助你的?");
}
private void 联系管理员ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("您好!我是这个图书系统的管理员");
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
//this.Close();
}
private void 借书ToolStripMenuItem_Click(object sender, EventArgs e)
{
adminbrow am = new adminbrow();
am.ShowDialog();
}
private void 还书ToolStripMenuItem_Click(object sender, EventArgs e)
{
adminreturn an = new adminreturn();
an.ShowDialog();
}
private void 系统ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void admin_Load(object sender, EventArgs e)
{
}
private void 查看学生借阅情况ToolStripMenuItem_Click(object sender, EventArgs e)
{
browdata b = new browdata();
b.ShowDialog();
}
private void 管理图书ToolStripMenuItem_Click(object sender, EventArgs e)
{
admin1 a1 = new admin1();
a1.ShowDialog();
}`
管理员管理图书的界面
点击查看学生借阅情况,就会显示借书信息
管理图书
点击添加图书,即可添加图书
还可以修改图书信息
还有其它功能也一样,就不一一演示了,直接上代码:
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void admin1_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 t_book";
IDataReader dc = dao.read(sql);//构造一个读取结果集,然后把它实例化成一个对象,通过dao类读取,返回结果
while (dc.Read())
{
dataGridView1.Rows.Add(dc[0].ToString(),dc[1].ToString(),dc[2].ToString(),dc[3].ToString(),dc[4].ToString());
}
dc.Close();
dao.DaoClose();
}
//根据书号显示数据
public void TableRE()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"select*from t_book where id='{textBox1.Text}'";
IDataReader dc = dao.read(sql);//构造一个读取结果集,然后把它实例化成一个对象,通过dao类读取,返回结果
while (dc.Read())
{
dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
}
dc.Close();
dao.DaoClose();
}
//根据书名显示数据
public void TableName()
{
dataGridView1.Rows.Clear();//清空旧数据
Dao dao = new Dao();
string sql = $"select * from t_book where name like'%{textBox2.Text}%'";
IDataReader dc = dao.read(sql);//构造一个读取结果集,然后把它实例化成一个对象,通过dao类读取,返回结果
while (dc.Read())
{
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 t_book 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)
{
label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString()+dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号和书名
}
private void button1_Click(object sender, EventArgs e)
{
admin1tj a1 = new admin1tj();
a1.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
string press = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
string number = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
admin1xg d = new admin1xg(id,name,author,press,number);//实例化
d.ShowDialog();//显示在修改界面
Table();
}
catch
{
MessageBox.Show("错误");
}
}
private void button5_Click(object sender, EventArgs e)
{
TableRE();
}
private void button6_Click(object sender, EventArgs e)
{
TableName();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
Table();
textBox1.Text = "";
textBox2.Text = "";
}
private void button7_Click(object sender, EventArgs e)
{
int n = dataGridView1.SelectedRows.Count;//获取当前选中的行数
DialogResult d = MessageBox.Show("确认删除吗?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (d == DialogResult.OK)
{
string sql = $"delete from t_book where id in(";
for (int i = 0; i < n; i++)
{
MessageBox.Show(dataGridView1.SelectedRows[i].Cells[i].Value.ToString());
sql += $"'{dataGridView1.SelectedRows[i].Cells[0].Value.ToString()}',";
}
sql = sql.Remove(sql.Length - 1);//删除最后一个字符串
sql += ")";
MessageBox.Show(sql);
Dao dao = new Dao();
if (dao.Execute(sql) > n - 1)
{
MessageBox.Show($"成功删除{n}条信息");
Table();
}
}
else {
Table();
}
}
}
最后,数据库的表,我用的SqlServer
管理员表:
用户表:
图书表:
借书表:
还书表: