以下是学习笔记:
一,考勤打卡
打卡效果如下:
打卡器:刷卡后,自动回车键
打卡成功:
实现:
1,后台部分:
在DAL--StudentService学员信息访问类添加代码
#region 查询学员【根据班级、学号、卡号】 /// <summary> /// 根据班级名称查询学员信息 /// </summary> /// <param name="className"></param> /// <returns></returns> public List<StudentExt> GetStudentByClass(string className) { string sql = "select StudentName,StudentId,Gender,Birthday,ClassName from Students"; sql += " inner join StudentClass on Students.ClassId=StudentClass.ClassId"; sql += " where ClassName='{0}'"; sql = string.Format(sql, className); SqlDataReader objReader = SQLHelper.GetReader(sql); List<StudentExt> list = new List<StudentExt>(); while (objReader.Read()) { list.Add(new StudentExt() { StudentId = Convert.ToInt32(objReader["StudentId"]), StudentName = objReader["StudentName"].ToString(), Gender = objReader["Gender"].ToString(), Birthday = Convert.ToDateTime(objReader["Birthday"]), ClassName = objReader["ClassName"].ToString() }); } objReader.Close(); return list; } /// <summary> ///根据学号查询学员对象 /// </summary> /// <param name="studentId"></param> /// <returns></returns> public StudentExt GetStudentById(string studentId) { string sql = "select StudentId,StudentName,Gender,Birthday,ClassName,StudentIdNo,PhoneNumber,StudentAddress,CardNo from Students"; sql += " inner join StudentClass on Students.ClassId=StudentClass.ClassId"; sql += " where StudentId=" + studentId; SqlDataReader objReader = SQLHelper.GetReader(sql); StudentExt objStudent = null; if (objReader.Read()) { objStudent = new StudentExt() { StudentId = Convert.ToInt32(objReader["StudentId"]), StudentName = objReader["StudentName"].ToString(), Gender = objReader["Gender"].ToString(), Birthday = Convert.ToDateTime(objReader["Birthday"]), ClassName = objReader["ClassName"].ToString(), CardNo = objReader["CardNo"].ToString(), StudentIdNo = objReader["StudentIdNo"].ToString(), PhoneNumber = objReader["PhoneNumber"].ToString(), StudentAddress = objReader["StudentAddress"].ToString() }; } objReader.Close(); return objStudent; } /// <summary> /// 根据卡号查询学生信息(请思考如何将上面的方法合并,像下面的方法这么简单) /// </summary> /// <param name="CardNo"></param> /// <returns></returns> public StudentExt GetStudentByCardNo(string CardNo) { string whereSql = string.Format(" where CardNo='{0}'", CardNo); return this.GetStudent(whereSql); } private StudentExt GetStudent(string whereSql) { string sql = "select StudentId,StudentName,Gender,Birthday,ClassName,"; sql += "StudentIdNo,PhoneNumber,StudentAddress,CardNo from Students"; sql += " inner join StudentClass on Students.ClassId=StudentClass.ClassId "; sql += whereSql; SqlDataReader objReader = SQLHelper.GetReader(sql); StudentExt objStudent = null; if (objReader.Read()) { objStudent = new StudentExt() { StudentId = Convert.ToInt32(objReader["StudentId"]), StudentName = objReader["StudentName"].ToString(), Gender = objReader["Gender"].ToString(), Birthday = Convert.ToDateTime(objReader["Birthday"]), ClassName = objReader["ClassName"].ToString(), CardNo = objReader["CardNo"].ToString(), StudentIdNo = objReader["StudentIdNo"].ToString(), PhoneNumber = objReader["PhoneNumber"].ToString(), StudentAddress = objReader["StudentAddress"].ToString() }; } objReader.Close(); return objStudent; } #endregion
2,前端UI部分:
public FrmAttendance()//窗体构造函数 { InitializeComponent(); timer1_Tick(null, null);//避免时间显示的延迟 } //显示当前时间 private void timer1_Tick(object sender, EventArgs e) { this.lblYear.Text = DateTime.Now.Year.ToString(); this.lblMonth.Text = DateTime.Now.Month.ToString(); this.lblDay.Text = DateTime.Now.Day.ToString(); this.lblTime.Text = DateTime.Now.ToLongTimeString(); switch (DateTime.Now.DayOfWeek) { case DayOfWeek.Tuesday: this.lblWeek.Text = "二"; break; case DayOfWeek.Wednesday: this.lblWeek.Text = "三"; break; case DayOfWeek.Thursday: this.lblWeek.Text = "四"; break; case DayOfWeek.Monday: this.lblWeek.Text = "一"; break; case DayOfWeek.Saturday: this.lblWeek.Text = "六"; break; case DayOfWeek.Friday: this.lblWeek.Text = "五"; break; case DayOfWeek.Sunday: this.lblWeek.Text = "日"; break; } } //学员打卡 private AttendanceService objAttendanceService = new AttendanceService(); private void txtStuCardNo_KeyDown(object sender, KeyEventArgs e) { if (this.txtStuCardNo.Text.Trim().Length != 0 && e.KeyValue == 13) { //显示学员信息 StudentExt objStu = new StudentService().GetStudentByCardNo(this.txtStuCardNo.Text.Trim()); if (objStu == null) { MessageBox.Show("卡号不正确!", "信息提示"); this.txtStuCardNo.SelectAll(); return; } this.lblStuName.Text = objStu.StudentName; this.lblStuClass.Text = objStu.ClassName; this.lblStuId.Text = objStu.StudentId.ToString(); //添加打卡信息 string result = objAttendanceService.AddRecord(this.txtStuCardNo.Text.Trim()); if (result != "success") { this.lblInfo.Text = "打卡失败!"; MessageBox.Show(result, "错误提示"); } else this.lblInfo.Text = "打卡成功!"; this.txtStuCardNo.Text = ""; //等待下一个打卡 this.txtStuCardNo.Focus(); } }
二,管理员修改密码:
1,后台部分
在DAL--SysAdminService管理数据访问类中添加
namespace DAL { /// <summary> /// 管理员数据访问类 /// </summary> public class SysAdminService { /// <summary> /// 根据登录账号和密码登录 /// </summary> /// <param name="objAdmin">封装了登录账号和密码的管理员对象</param> /// <returns>返回包含管理员信息的对象</returns> public SysAdmin AdminLogin(SysAdmin objAdmin) { //组合SQL语句 string sql = "select AdminName from Admins where LoginId={0} and LoginPwd='{1}'"; sql = string.Format(sql, objAdmin.LoginId, objAdmin.LoginPwd); //从数据库中查询 SqlDataReader objReader = SQLHelper.GetReader(sql); if (objReader.Read()) { objAdmin.AdminName = objReader["AdminName"].ToString(); } else { objAdmin = null;//如果登录不成功,则将当前对象清空 } objReader.Close(); //返回结果 return objAdmin; } /// <summary> /// 修改管理员密码 /// </summary> /// <param name="objAdmin"></param> /// <returns></returns> public int ModifyPwd(SysAdmin objAdmin) { string sql = "update Admins set LoginPwd='{0}' where LoginId={1}"; sql = string.Format(sql, objAdmin.LoginPwd, objAdmin.LoginId); try { return SQLHelper.Update(sql); } catch (SqlException) { throw new Exception("应用程序和数据库连接出现问题!"); } catch (Exception ex) { throw ex; } } } }
2,前端UI:
确认修改的按钮事件
//修改密码 private void btnModify_Click(object sender, EventArgs e) { #region 密码验证 if (this.txtOldPwd.Text.Trim().Length == 0) { MessageBox.Show("请输入原密码!", "提示信息"); this.txtOldPwd.Focus(); return; } if (this.txtOldPwd.Text.Trim() != Program.objCurrentAdmin.LoginPwd) { MessageBox.Show("请输入的原密码不正确!", "提示信息"); this.txtOldPwd.Focus(); this.txtOldPwd.SelectAll(); return; } if (this.txtNewPwd.Text.Trim().Length == 0) { MessageBox.Show("请输入不少于6位的新密码!", "提示信息"); this.txtNewPwd.Focus(); return; } if (this.txtNewPwd.Text.Trim().Length < 6) { MessageBox.Show("新密码长度不能少于6位!", "提示信息"); this.txtNewPwd.Focus(); return; } if (this.txtNewPwdConfirm.Text.Trim().Length == 0) { MessageBox.Show("请再次输入新密码!", "提示信息"); this.txtNewPwdConfirm.Focus(); return; } if (this.txtNewPwdConfirm.Text.Trim() != this.txtNewPwd.Text.Trim()) { MessageBox.Show("两次输入的新密码不一致!", "提示信息"); return; } #endregion //修改密码 try { SysAdmin objAdmin = new SysAdmin() { LoginId = Program.objCurrentAdmin.LoginId, LoginPwd = this.txtNewPwd.Text.Trim() }; if (new SysAdminService().ModifyPwd(objAdmin) == 1) { MessageBox.Show("密码修改成功,请妥善保管!", "成功提示"); //同时修改当前保存的用户密码 Program.objCurrentAdmin.LoginPwd = this.txtNewPwd.Text.Trim(); this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }