该学生考勤系统包含了登录、注册、学生端的请假功能,教师端对学生信息的增删改查和批假功能。
该类主要用于后面一些功能需要对数据库操作,即需要对数据库操作的话,直接调用该类即可。
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
// 访问数据库的类
class MySqlAccess
{
//定义mysql连接字符串
private static string constring = "data source = localhost; database = student_attendance_system; user id = root; password = 0915; pooling = true; charset = utf8; port=3306;";
//连接mysql
private static MySqlConnection msc = new MySqlConnection(constring);
// 返回用于实现mysql命令的对象
public static MySqlCommand GetMySqlCommand(string sql)
{
//打开数据库连接
if (msc.State == System.Data.ConnectionState.Closed)
msc.Open();
return new MySqlCommand(sql, msc);
}
public static MySqlDataAdapter GetMySqlDataAdapter(string sql)
{
//打开数据库连接
if (msc.State == System.Data.ConnectionState.Closed)
msc.Open();
return new MySqlDataAdapter(sql, msc);
}
}
}
该功能实现起来其实也也并不是很难,登录的话,只要读取数据库的用户表,然后和输入的用户名和密码进行匹配即可,在根据该用户表的身身份字段,进入不同的客户端。
注册的话,如果操作正确,只需对数据库的用户表增加操作就行了。
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 MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class Login_Form : Form
{
//static public Login_Form login = new Login_Form(); //静态化主窗口
public static string userName;
public static string userIdentity;
public Login_Form()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//定义sql语句
string sql = "select * from subscribers";
//调用MySqlAccess类获得实现mysql语句对象
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
//执行命令
MySqlDataReader reader = cmd.ExecuteReader();
//读取数据
bool flag1 = false, flag2 = false;
while (reader.Read())
{
if (reader.GetString("id").Equals(User_Name.Text))
{
flag1 = true;
if (reader.GetString("user_password").Equals(User_Password.Text))
{
flag2 = true;
userName = reader.GetString(0);
userIdentity = reader.GetString(2);
}
}
if (flag1 && flag2)
break;
}
reader.Close();
if (flag1 && flag2)
{
if(userIdentity == "学生")
{
Student_Form student_Form = new Student_Form();
this.Hide();
student_Form.ShowDialog();
this.Close();
}
else if(userIdentity == "管理员")
{
Admin_Form admin_Form = new Admin_Form();
this.Hide();
admin_Form.ShowDialog();
this.Close();
}
else
{
Teacher_Form teacher_Form = new Teacher_Form();
this.Hide();
teacher_Form.ShowDialog();
this.Close();
}
}
else
{
if (!flag1)
MessageBox.Show("用户名错误");
else
MessageBox.Show("密码错误");
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Register_Form register_Form = new Register_Form();
register_Form.ShowDialog();
}
}
}
注册主要用于教师、辅导员、领导、院长注册,学生有教师端添加,并且点击注册时也会弹出邀请码框,邀请码由管理员生成。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 学生考勤系统的设计
{
public partial class Register_Form : Form
{
public Register_Form()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
if(comboBox1.Text .Equals(string.Empty))
{
MessageBox.Show("请选择身份!");
return;
}
if(textBox1.Text.Equals(string.Empty))
{
MessageBox.Show("用户名不能为空");
return;
}
if (textBox2.Text.Equals(string.Empty))
{
MessageBox.Show("密码不能为空");
return;
}
if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("密码不一致!");
return;
}
Captcha_Form captcha_Form = new Captcha_Form();
DialogResult dialogResult = captcha_Form.ShowDialog();
if (dialogResult != DialogResult.OK)
{
MessageBox.Show("注册失败!");
return;
}
try {
string sql = "insert into subscribers values('{0}', '{1}', '{2}')";
sql = string.Format(sql, textBox1.Text, textBox2.Text, comboBox1.Text);
//MessageBox.Show(sql);
MySqlAccess.GetMySqlCommand(sql).ExecuteNonQuery();
MessageBox.Show("注册成功!");
}
catch(Exception ex)
{
MessageBox.Show("用户已存在!");
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class Student_Form : Form
{
public Student_Form()
{
InitializeComponent();
}
private void Student_From_Load(object sender, EventArgs e)
{
TableShow();
}
private void TableShow()
{
string userName = Login_Form.userName;
string sql = "select student_id,student_name,class,specialty,phone_number,absence_count,late_count,early_count from student_infomation where student_id = '" + userName + "'";
//MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
//MySqlDataReader reader = cmd.ExecuteReader();
//创建MySqlDataAdapter对象
MySqlDataAdapter mySqlData = MySqlAccess.GetMySqlDataAdapter(sql);
//创建DataSet对象
DataSet ds = new DataSet();
//使用MySqlDataAdapter对象mySqlData将查到的结果填充到DataSet对象中
mySqlData.Fill(ds);
//设置表格控件DataSource的属性
dataGridView1.DataSource = ds.Tables[0];
//设置列标题
dataGridView1.Columns[0].HeaderText = "学号";
dataGridView1.Columns[1].HeaderText = "姓名";
dataGridView1.Columns[2].HeaderText = "班级";
dataGridView1.Columns[3].HeaderText = "专业";
dataGridView1.Columns[4].HeaderText = "电话";
dataGridView1.Columns[5].HeaderText = "旷课次数";
dataGridView1.Columns[6].HeaderText = "迟到次数";
dataGridView1.Columns[7].HeaderText = "早退次数";
//设置数据表格为只读
dataGridView1.ReadOnly = true;
//不允许添加行
dataGridView1.AllowUserToAddRows = false;
//背景为白色
dataGridView1.BackgroundColor = Color.White;
//只允许选中单行
dataGridView1.MultiSelect = false;
//整行选中
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
private void button1_Click(object sender, EventArgs e)
{
Leave leave = new Leave();
leave.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
Login_Form lf = new Login_Form();
this.Hide();
lf.ShowDialog();
this.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class Leave : Form
{
public Leave()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
bool tut_leave = false, ter_leave = false, lea_leave = false, dean_leave = false;
string leaveTime;
string leaveReason;
//判断是否被选中
if (radioButton1.Checked)
{
leaveTime = radioButton1.Text;
ter_leave = true;
}
else if (radioButton2.Checked)
{
leaveTime = radioButton2.Text;
tut_leave = true;
}
else if (radioButton3.Checked)
{
leaveTime = radioButton3.Text; ;
tut_leave = true;
lea_leave = true;
}
else
{
leaveTime = radioButton4.Text;
tut_leave = true;
lea_leave = true;
dean_leave = true;
}
leaveReason = textBox1.Text;
try
{
//获取当前用户名
string userName = Login_Form.userName;
//定义sql语句,修改请假状态
string sql1, sql2, sql3;
// 更新请假状态表
sql2 = "insert into leave_state values({0}, {1}, {2}, {3}, {4})";
sql2 = string.Format(sql2, userName, tut_leave, ter_leave, lea_leave, dean_leave);
//MessageBox.Show(sql2);
MySqlAccess.GetMySqlCommand(sql2).ExecuteNonQuery(); // 获取并执行
// 更新学生表里的请假时长
sql1 = "update student_infomation set leave_state = '" + leaveTime + "' where student_id = '" + userName + "'";
//获取实现sql命令的对象
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql1);
//执行命令
cmd.ExecuteNonQuery();
sql3 = "update student_infomation set leave_reason = '" + leaveReason + "' where student_id = '" + userName + "'";
MySqlAccess.GetMySqlCommand(sql3).ExecuteNonQuery();
MessageBox.Show("提交成功");
this.Close();
}
catch(Exception ex)
{
MessageBox.Show("你已经提交过申请");
}
}
}
}
教师端保函对学生信息的增删改查,修改和增加学生信息时,出勤率会自动更新,增加学生时,也会给给该学生自动生成一个账号,账号为学号,密码为123456,并且也会显示未读的消息。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class Teacher_Form : Form
{
public Teacher_Form()
{
InitializeComponent();
}
private void Student_Form_Load(object sender, EventArgs e)
{
//设置数据表格为只读
dataGridView1.ReadOnly = true;
//不允许添加行
dataGridView1.AllowUserToAddRows = false;
//背景为白色
dataGridView1.BackgroundColor = Color.White;
//只允许选中单行
dataGridView1.MultiSelect = false;
//整行选中
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
ShowAllInofmation();
UnreadMessage();
}
//显示所有学生信息
private void ShowAllInofmation()
{
string sql = "select student_id,student_name,class,specialty,phone_number,absence_count,late_count,early_count,attendance from student_infomation";
MySqlDataAdapter mySqlData = MySqlAccess.GetMySqlDataAdapter(sql);
DataSet dataSet = new DataSet();
mySqlData.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
//设置列标题
dataGridView1.Columns[0].HeaderText = "学号";
dataGridView1.Columns[1].HeaderText = "姓名";
dataGridView1.Columns[2].HeaderText = "班级";
dataGridView1.Columns[3].HeaderText = "专业";
dataGridView1.Columns[4].HeaderText = "电话";
dataGridView1.Columns[5].HeaderText = "旷课次数";
dataGridView1.Columns[6].HeaderText = "迟到次数";
dataGridView1.Columns[7].HeaderText = "早退次数";
dataGridView1.Columns[8].HeaderText = "出勤率";
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals(string.Empty) && textBox2.Text.Equals(string.Empty))
MessageBox.Show("学号或姓名不能为空");
else
{
string sql = "";
if(!textBox1.Text.Equals(string.Empty))
sql = "select student_id,student_name,class,specialty,phone_number,absence_count,late_count,early_count,attendance from student_infomation where student_id = '" + textBox1.Text + "'";
else
sql = "select student_id,student_name,class,specialty,phone_number,absence_count,late_count,early_count,attendance from student_infomation where student_name = '" + textBox2.Text + "'";
try
{
MySqlDataAdapter mySqlData = MySqlAccess.GetMySqlDataAdapter(sql);
DataSet dataSet = new DataSet();
mySqlData.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show("查询失败!\n" + ex.Message);
}
}
textBox1.Clear();
textBox2.Clear();
}
private void button5_Click(object sender, EventArgs e)
{
ShowAllInofmation();
}
private void button2_Click(object sender, EventArgs e)
{
//获取DataGridView控件的值
//获取学号
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//获取姓名
string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//获取班级
string Class = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//获取专业
string specialty = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//获取电话
string phone = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
//获取缺席次数
string absenceCount = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
//获取迟到次数
string lateCount = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
//获取早退次数
string leaveCount = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
//创建Update_Form窗口,把学生信息传递给修改界面
Update_Form update_Form = new Update_Form(id, name, Class, specialty, phone, absenceCount, lateCount, leaveCount);
// 弹出修改信息修改窗口
DialogResult dialogResult = update_Form.ShowDialog();
//DialogResult:获取或设置对话框值,此值是从ShowDialog()方法返回
//判断是否点击确定按钮
if (dialogResult == DialogResult.OK)
ShowAllInofmation();
}
private void button3_Click(object sender, EventArgs e)
{
try
{
//获取DataGridView控件中选中行的学号
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string sql = "delete from student_infomation where student_id = '{0}'";
sql = string.Format(sql, id);
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功");
ShowAllInofmation();
}
catch(Exception ex)
{
MessageBox.Show("删除失败!\n" + ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
Insert_Form insert_Form = new Insert_Form();
DialogResult dialogResult = insert_Form.ShowDialog();
if (dialogResult == DialogResult.OK)
ShowAllInofmation();
}
private void button7_Click(object sender, EventArgs e)
{
Login_Form login_Form = new Login_Form();
this.Hide();
login_Form.ShowDialog();
this.Close();
}
private void button6_Click(object sender, EventArgs e)
{
ApprovalLeave_Form approvalLeave_Form = new ApprovalLeave_Form();
approvalLeave_Form.ShowDialog();
UnreadMessage();
}
//显示未读信息
private void UnreadMessage()
{
string sql;
if (Login_Form.userIdentity == "辅导员")
sql = "select count(*) from leave_state where tut_leave = true";
else if (Login_Form.userIdentity == "教师")
sql = "select count(*) from leave_state where ter_leave = true";
else if (Login_Form.userIdentity == "领导")
sql = "select count(*) from leave_state where lea_leave = true";
else
sql = "select count(*) from leave_state where dean_leave = true";
MySqlDataReader reader = MySqlAccess.GetMySqlCommand(sql).ExecuteReader();
reader.Read();
string num = reader.GetString(0);
reader.Close();
if (num == "0")
{
label3.Visible = false;
}
else
label3.Text = num;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class Insert_Form : Form
{
public Insert_Form()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals(string.Empty) || textBox2.Text.Equals(string.Empty) ||
textBox3.Text.Equals(string.Empty) || textBox4.Text.Equals(string.Empty) ||
textBox5.Text.Equals(string.Empty) || textBox6.Text.Equals(string.Empty) ||
textBox7.Text.Equals(string.Empty) || textBox8.Text.Equals(string.Empty))
{
MessageBox.Show("信息不能为空");
return ;
}
string sql = "insert into student_infomation(student_id, student_name, class, specialty, phone_number, absence_count, late_count, early_count) values('{0}', '{1}', '{2}', '{3}', '{4}', {5}, {6}, {7})";
sql = string.Format(sql, textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text);
try
{
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功");
DialogResult = DialogResult.OK;
}
catch(Exception ex)
{
MessageBox.Show("添加失败!\n");
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
//获取DataGridView控件中选中行的学号
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string sql = "delete from student_infomation where student_id = '{0}'";
sql = string.Format(sql, id);
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
cmd.ExecuteNonQuery();
MessageBox.Show("删除成功");
ShowAllInofmation();
}
catch(Exception ex)
{
MessageBox.Show("删除失败!\n" + ex.Message);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class Update_Form : Form
{
private string id;
public Update_Form(string id, string name, string Class, string specialty, string phone, string absenceCount, string lateCount, string leaveCount)
{
InitializeComponent();
textBox1.Text = name;
textBox2.Text = Class;
textBox3.Text = specialty;
textBox4.Text = phone;
textBox5.Text = absenceCount;
textBox6.Text = lateCount;
textBox7.Text = leaveCount;
this.id = id;
}
private void button2_Click(object sender, EventArgs e)
{
string sql = "update student_infomation set student_name = '{0}', class = '{1}', specialty = '{2}', phone_number = '{3}', absence_count = {4}, late_count = {5}, early_count = {6} where student_id = '{7}'";
sql = string.Format(sql, textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, id);
try
{
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功");
this.DialogResult = DialogResult.OK;
this.Close();
}
catch(Exception ex)
{
MessageBox.Show("修改失败!\n" + ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals(string.Empty) && textBox2.Text.Equals(string.Empty))
MessageBox.Show("学号或姓名不能为空");
else
{
string sql = "";
if(!textBox1.Text.Equals(string.Empty))
sql = "select student_id,student_name,class,specialty,phone_number,absence_count,late_count,early_count,attendance from student_infomation where student_id = '" + textBox1.Text + "'";
else
sql = "select student_id,student_name,class,specialty,phone_number,absence_count,late_count,early_count,attendance from student_infomation where student_name = '" + textBox2.Text + "'";
try
{
MySqlDataAdapter mySqlData = MySqlAccess.GetMySqlDataAdapter(sql);
DataSet dataSet = new DataSet();
mySqlData.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show("查询失败!\n" + ex.Message);
}
}
textBox1.Clear();
textBox2.Clear();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace 学生考勤系统的设计
{
public partial class ApprovalLeave_Form : Form
{
public ApprovalLeave_Form()
{
InitializeComponent();
}
private void ShowLeaveInfo()
{
string sql;
if (Login_Form.userIdentity == "教师")
sql = "select student_id, student_name, leave_state, leave_reason from student_infomation where student_id = any(select stu_id from leave_state where ter_leave = true)";
else if (Login_Form.userIdentity == "院长")
sql = "select student_id, student_name, leave_state, leave_reason from student_infomation where student_id = any(select stu_id from leave_state where dean_leave = true)";
else if (Login_Form.userIdentity == "领导")
sql = "select student_id, student_name, leave_state, leave_reason from student_infomation where student_id = any(select stu_id from leave_state where lea_leave = true)";
else
sql = "select student_id, student_name, leave_state, leave_reason from student_infomation where student_id = any(select stu_id from leave_state where tut_leave = true)";
try
{
MySqlDataAdapter mySqlData = MySqlAccess.GetMySqlDataAdapter(sql);
DataSet dataSet = new DataSet();
mySqlData.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show("系统错误!\n" + ex.Message);
}
dataGridView1.Columns[0].HeaderText = "学号";
dataGridView1.Columns[1].HeaderText = "姓名";
dataGridView1.Columns[2].HeaderText = "请假时长";
dataGridView1.Columns[3].HeaderText = "请假理由";
}
private void ApprovalLeave_Form_Load(object sender, EventArgs e)
{
//设置数据表格为只读
dataGridView1.ReadOnly = true;
//不允许添加行
dataGridView1.AllowUserToAddRows = false;
//背景为白色
dataGridView1.BackgroundColor = Color.White;
//只允许选中单行
dataGridView1.MultiSelect = false;
//整行选中
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
ShowLeaveInfo();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
string sql = "update leave_state set {0} = false where stu_id = '{1}'";
if (Login_Form.userIdentity == "教师")
sql = String.Format(sql, "ter_leave", id);
else if (Login_Form.userIdentity == "院长")
sql = String.Format(sql, "dean_leave", id);
else if (Login_Form.userIdentity == "领导")
sql = String.Format(sql, "lea_leave", id);
else if (Login_Form.userIdentity == "辅导员")
sql = string.Format(sql, "tut_leave", id);
MySqlCommand cmd = MySqlAccess.GetMySqlCommand(sql);
cmd.ExecuteNonQuery();
ShowLeaveInfo();
MessageBox.Show("成功!");
ShowLeaveInfo();
//判改学生是否请假成功,如果成功,删除改名学生在数据库中leave_state的数据
sql = "select * from leave_state where stu_id = '{0}'";
sql = string.Format(sql, id);
MySqlDataReader reader = MySqlAccess.GetMySqlCommand(sql).ExecuteReader();
reader.Read();
bool flag1 = reader.GetBoolean(1);
bool flag2 = reader.GetBoolean(2);
bool flag3 = reader.GetBoolean(3);
bool flag4 = reader.GetBoolean(4);
reader.Close();
//还需要在审核
if (flag1 || flag2 || flag3 || flag4)
return;
// 不需审核,删掉数据
sql = "delete from leave_state where stu_id = '{0}'";
sql = string.Format(sql, id);
MySqlAccess.GetMySqlCommand(sql).ExecuteNonQuery();
}
catch(Exception ex)
{
MessageBox.Show("失败\n" + ex.Message);
}
}
}
}
https://download.csdn.net/download/weixin_52068218/87337349
度盘链接:
https://pan.baidu.com/s/1B2mJTeSTz7T3BMcjvVKhDA?pwd=t26r 提取码: t26r 复制这段内容后打开百度网盘手机App,操作更方便哦
–来自百度网盘超级会员v3的分享