winform+SQL server数据库制作上下班打卡小程序;如果已打卡;点击再上班打卡按钮时提示已打卡;并且判断打卡时间范围;晚上23点-早上7点30为休息时间,打卡视为无效并提示;8点到8点30分打卡为正常打卡;8点30分之后打卡为迟到,并计算迟到时长;具体实现过程以及代码
时间: 2024-03-13 10:42:33 浏览: 28
好的,这是一个比较复杂的问题,需要进行详细的分析和设计。以下是我的建议:
1. 数据库设计
根据需求,我们需要记录员工的上下班打卡时间,打卡状态和迟到时长。因此需要在数据库中创建一个名为"打卡记录"的表,包含以下字段:
- 打卡记录ID(自增长主键)
- 员工ID
- 打卡时间
- 打卡状态(上班打卡、下班打卡、迟到)
- 迟到时长
2. 界面设计
使用WinForm开发界面,设计一个主界面,包含以下控件:
- 一个ComboBox控件,用于选择员工
- 一个Label控件,显示当前时间
- 一个Button控件,用于进行上班打卡/下班打卡操作
3. 代码实现
在代码中,我们需要实现以下功能:
- 选择员工时,从数据库中读取该员工的打卡记录,并显示在界面上
- 点击打卡按钮时,判断当前时间是否在打卡时间范围内,如果不在范围内,则提示无效打卡;如果在范围内,则判断是否已经打过卡,如果已经打过卡,则提示已打卡;如果没有打过卡,则添加一条打卡记录到数据库中,并更新界面上的打卡记录
- 计算迟到时长时,需要查询该员工的上班时间,如果打卡时间晚于上班时间,则计算迟到时长,并更新打卡记录中的迟到时长字段
以下是实现代码的详细过程:
1. 数据库连接及操作
首先需要创建一个数据库连接,并实现对数据库的操作,包括读取员工信息和打卡记录,以及添加打卡记录等功能。以下是一个示例代码:
```csharp
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace WinFormDemo
{
public class DBHelper
{
private static string connString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
public static DataTable GetEmployees()
{
string sql = "SELECT * FROM Employees";
return ExecuteQuery(sql);
}
public static DataTable GetAttendanceRecords(int employeeId)
{
string sql = "SELECT * FROM AttendanceRecords WHERE EmployeeId = " + employeeId;
return ExecuteQuery(sql);
}
public static bool AddAttendanceRecord(int employeeId, string punchTime, string punchType, int lateMinutes)
{
string sql = "INSERT INTO AttendanceRecords(EmployeeId, PunchTime, PunchType, LateMinutes) VALUES(" + employeeId + ", '" + punchTime + "', '" + punchType + "', " + lateMinutes + ")";
return ExecuteNonQuery(sql);
}
private static DataTable ExecuteQuery(string sql)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
}
}
private static bool ExecuteNonQuery(string sql)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
return result > 0;
}
}
}
}
```
2. 界面操作及事件处理
在WinForm中,我们可以通过控件的事件来实现相应的操作。以下是一个示例代码:
```csharp
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
namespace WinFormDemo
{
public partial class MainForm : Form
{
private int _employeeId;
public MainForm()
{
InitializeComponent();
// 初始化ComboBox控件
DataTable dtEmployees = DBHelper.GetEmployees();
cbEmployees.DisplayMember = "Name";
cbEmployees.ValueMember = "Id";
cbEmployees.DataSource = dtEmployees;
}
private void MainForm_Load(object sender, EventArgs e)
{
// 定时器,每秒钟更新一次当前时间
timer1.Interval = 1000;
timer1.Tick += Timer1_Tick;
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
// 更新当前时间
lblCurrentTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void cbEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
// 选择员工时,读取该员工的打卡记录,并显示在界面上
_employeeId = (int)cbEmployees.SelectedValue;
DataTable dtAttendanceRecords = DBHelper.GetAttendanceRecords(_employeeId);
dgvAttendanceRecords.DataSource = dtAttendanceRecords;
}
private void btnPunch_Click(object sender, EventArgs e)
{
// 点击打卡按钮
string punchType = "";
DateTime punchTime = DateTime.Now;
// 判断当前时间是否在打卡时间范围内
if (punchTime.Hour < 8 || (punchTime.Hour == 8 && punchTime.Minute < 30) || punchTime.Hour >= 23 || (punchTime.Hour == 7 && punchTime.Minute >= 30))
{
MessageBox.Show("当前时间不在打卡时间范围内!");
return;
}
// 判断是否已经打过卡
DataTable dtAttendanceRecords = DBHelper.GetAttendanceRecords(_employeeId);
if (dtAttendanceRecords.AsEnumerable().Any(row => row.Field<DateTime>("PunchTime").Date == punchTime.Date && row.Field<string>("PunchType") == "上班打卡"))
{
MessageBox.Show("今天已经打过上班卡!");
return;
}
else if (dtAttendanceRecords.AsEnumerable().Any(row => row.Field<DateTime>("PunchTime").Date == punchTime.Date && row.Field<string>("PunchType") == "下班打卡"))
{
MessageBox.Show("今天已经打过下班卡!");
return;
}
// 判断打卡类型
if (punchTime.Hour < 8 || (punchTime.Hour == 8 && punchTime.Minute < 30))
{
punchType = "上班打卡";
}
else
{
punchType = "下班打卡";
}
// 计算迟到时长
int lateMinutes = 0;
if (punchType == "上班打卡" && punchTime.TimeOfDay > new TimeSpan(8, 0, 0))
{
lateMinutes = (int)(punchTime.TimeOfDay - new TimeSpan(8, 0, 0)).TotalMinutes;
}
// 添加打卡记录
bool result = DBHelper.AddAttendanceRecord(_employeeId, punchTime.ToString("yyyy-MM-dd HH:mm:ss"), punchType, lateMinutes);
if (result)
{
MessageBox.Show("打卡成功!");
// 更新打卡记录
dtAttendanceRecords = DBHelper.GetAttendanceRecords(_employeeId);
dgvAttendanceRecords.DataSource = dtAttendanceRecords;
}
else
{
MessageBox.Show("打卡失败!");
}
}
}
}
```
以上就是实现一个WinForm+SQL Server数据库制作上下班打卡小程序的详细过程和代码。