1.识别实体
实体:职员
属性:编号、姓名、性别、部门、权限
实体:打卡机
属性:编号、位置
实体关系:打卡
属性:流水号、打卡日期、打卡时间、职员编号、打卡机编号
2.E-R图
3.数据表图
4.数据库图
职员表
打卡机表
数据库表
5登录注册界面
点击“登录”按钮则登录系统
private void bt_Login_Click(object sender, EventArgs e)
{
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令发送给数据库
String sqlStr = "select * from EMPLOYEE where ID=@id and PASSWORD=@pwd";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// 注意是用用户ID登录,而不是用户名,用户名可能会重复
cmd.Parameters.Add(new SqlParameter("@id", this.tb_User.Text.Trim()));
cmd.Parameters.Add(new SqlParameter("@pwd", this.tb_Password.Text.Trim()));
SqlDataReader dr = cmd.ExecuteReader();
// 如果从数据库中查询到记录,则表示可以登录
if (dr.HasRows)
{
dr.Read();
UserInfo.userId = int.Parse(dr["ID"].ToString());
UserInfo.userName = dr["NAME"].ToString();
UserInfo.userPwd = dr["PASSWORD"].ToString();
UserInfo.userRole = dr["ROLE"].ToString();
UserInfo.userDepartment = dr["DEPARTMENT"].ToString();
UserInfo.userGender = dr["GENDER"].ToString();
MessageBox.Show(UserInfo.userRole + "登录成功");
if (UserInfo.userRole == "职员")
{
Form3 Form = new Form3();
Form.Show();
//// 显示收银员主界面
//MainFormUser formUser = new MainFormUser();
//formUser.Show();
//// 隐藏登录界面
this.Hide();
}
if (UserInfo.userRole == "管理员")
{
Form4 Form = new Form4();
Form.Show();
//// 显示库管员主界面
//MainFormAdmin formAdmin = new MainFormAdmin();
//formAdmin.Show();
//// 隐藏登录界面
this.Hide();
}
}
else
{
MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
// 点击“退出”按钮则退出应用程序
点击录入职员按钮进入Form2
Form2 Form = new Form2();
Form.Show();
点击注册按钮注册
String id = this.textBox1.Text.Trim();
String name = this.textBox2.Text.Trim();
String password = this.textBox3.Text.Trim();
String gender = this.comboBox1.Text.ToString();
String department = this.comboBox2.Text.ToString();
String role = this.comboBox3.Text.ToString();
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["Attendance"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "insert into employee(ID, NAME, GENDER, DEPARTMENT, ROLE, PASSWORD) values(@id, @name, @gender, @department, @role, @password)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
String sqlSt = "select MAX(id+1) as id from employee";
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.Parameters.Add(new SqlParameter("@gender", gender));
cmd.Parameters.Add(new SqlParameter("@department", department));
cmd.Parameters.Add(new SqlParameter("@role", role));
cmd.Parameters.Add(new SqlParameter("@password", password));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否插入成功
if (res != 0)
{
MessageBox.Show("职员信息录入成功");
}
else
{
MessageBox.Show("职员信息录入失败");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
}
private void Form2_Load(object sender, EventArgs e)
{
}
6职员打卡
点击打开串口按钮
// 串口已打开,此时需要关闭
if (serialPort1.IsOpen)
{
serialPort1.Close();
this.toolStripStatusLabel1.Text = "已关闭串口" + serialPort1.PortName.ToString();
buttonOpenCOM.Text = "打开";
return;
}
// 否则打开串口
else
{
serialPort1.PortName = comboBoxCOMList.Text;
InitSerialPort();
try
{
serialPort1.Open();
this.toolStripStatusLabel1.Text = "已打开串口" + serialPort1.PortName.ToString();
buttonOpenCOM.Text = "关闭";
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = "打开串口失败,原因:" + ex.Message;
return;
}
}