C#winform上下班打卡系统Demo

C# winform上下班打卡系统Demo

系统效果如图所示
在这里插入图片描述
7个label控件(lblUsername、lblLoggedInEmployeeId、lab_IP、lblCheckOutTime、lblCheckInTime、lab_starttime、lab_endtime)、3个按钮、1个dataGridView控件、2个groupBox控件

C#代码实现

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class 员工打卡 : Form{private string loggedInUsername;private string loggedInEmployeeId;private string connectionString = "server=127.0.0.1;uid=sa;pwd=xyz@0123456;database=test";public 员工打卡(string username, string employeeId){InitializeComponent();loggedInUsername = username;loggedInEmployeeId = employeeId;CheckTodaysPunchInRecord();}[DllImport("user32.dll")]public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);[DllImport("user32.dll")]public static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);// 禁用窗口大小改变private const uint SC_SIZE = 0xF000;private const uint MF_BYCOMMAND = 0x0000;private const uint MF_GRAYED = 0x0001;protected override void OnLoad(EventArgs e){base.OnLoad(e);IntPtr hMenu = GetSystemMenu(this.Handle, false);if (hMenu != IntPtr.Zero){EnableMenuItem(hMenu, SC_SIZE, MF_BYCOMMAND | MF_GRAYED);}}private void 员工打卡_Load(object sender, EventArgs e){lblUsername.Text = "当前登录用户:" + loggedInUsername;lblLoggedInEmployeeId.Text = "工号:" + loggedInEmployeeId.ToString();// 设置日期控件的显示格式为年-月-日startTime.Format = DateTimePickerFormat.Custom;startTime.CustomFormat = "yyyy-MM-dd";// 设置日期控件的显示格式为年-月-日endTime.Format = DateTimePickerFormat.Custom;endTime.CustomFormat = "yyyy-MM-dd";//不显示出dataGridView1的最后一行空白dataGridView1_Result.AllowUserToAddRows = false;// 设置数据和列名居中对齐dataGridView1_Result.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;dataGridView1_Result.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;// 设置列名加粗dataGridView1_Result.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font(dataGridView1_Result.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold);// 设置列宽自适应dataGridView1_Result.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;LoadData();GetIP();}private void GetIP(){// 获取本机IP地址IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());foreach (IPAddress ip in ipHost.AddressList){if (ip.AddressFamily == AddressFamily.InterNetwork){lab_IP.Text = "IP地址:" + ip.ToString(); // 添加到label1的Text属性中}}}private void btnCheckIn_Click(object sender, EventArgs e){if (IsPunchInRecordExists()){MessageBox.Show("你已打过上班卡。", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);return;}DateTime currentTime = DateTime.Now;string punchInTime = currentTime.ToString("yyyy-MM-dd HH:mm:ss");string status = currentTime.TimeOfDay < new TimeSpan(8, 30, 0) ? "正常" : "迟到";InsertPunchInRecord(punchInTime, status);lblCheckInTime.Text = punchInTime;lblCheckInTime.Visible = true;// 刷新DataGridView显示最新打卡记录RefreshDataGridView();}private bool IsPunchInRecordExists(){DateTime currentDate = DateTime.Now.Date;string query = $"SELECT COUNT(*) FROM PunchIn WHERE emp_code='{loggedInEmployeeId}' AND punch_in_time >= '{currentDate}'";using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();using (SqlCommand command = new SqlCommand(query, connection)){int count = (int)command.ExecuteScalar();return count > 0;}}}private void RefreshDataGridView(){// 执行查询语句string query = $@"SELECT a.id,a1.username AS 用户名,a.emp_code AS 工号,CONVERT(VARCHAR(19), a.punch_in_time, 120) AS 上班打卡时间,a.status AS 上班打卡状态,CONVERT(VARCHAR(19), b.punch_out_time, 120) AS 下班打卡时间,b.status AS 下班打卡状态FROM (SELECT * FROM Employee) a1LEFT JOIN PunchIn a ON a1.emp_code = a.emp_codeLEFT JOIN PunchOut b ON a.emp_code = b.emp_code AND CONVERT(DATE, a.punch_in_time) = CONVERT(DATE, b.punch_out_time)AND b.punch_out_time = (SELECT MAX(punch_out_time)FROM PunchOutWHERE emp_code = a.emp_code AND CONVERT(DATE, punch_out_time) = CONVERT(DATE, a.punch_in_time))WHERE a.emp_code = '{loggedInEmployeeId}' AND MONTH(a.punch_in_time) = MONTH(GETDATE()) AND YEAR(a.punch_in_time) = YEAR(GETDATE())ORDER BY a.id, a.emp_code, a.punch_in_time";Console.WriteLine("执行的SQL语句是:" + query);// 执行查询并获取结果// 你可以使用适合你数据库的查询方法DataTable dataTable = ExecuteQuery(query);// 将查询结果绑定到DataGridView的数据源dataGridView1_Result.DataSource = dataTable;}private DataTable ExecuteQuery(string query){// 创建连接和命令对象并执行查询using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();using (SqlCommand command = new SqlCommand(query, connection)){// 创建适配器并填充数据到DataTableSqlDataAdapter adapter = new SqlDataAdapter(command);DataTable dataTable = new DataTable();adapter.Fill(dataTable);return dataTable;}}}private void InsertPunchInRecord(string punchInTime, string status){string query = $"INSERT INTO PunchIn (emp_code, punch_in_time, status) VALUES ('{loggedInEmployeeId}', '{punchInTime}', '{status}')";using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();using (SqlCommand command = new SqlCommand(query, connection)){command.ExecuteNonQuery();}}}private void CheckTodaysPunchInRecord(){DateTime currentDate = DateTime.Now.Date;string query = $"SELECT punch_in_time FROM PunchIn WHERE emp_code='{loggedInEmployeeId}' AND punch_in_time >= '{currentDate}'";using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();using (SqlCommand command = new SqlCommand(query, connection)){object result = command.ExecuteScalar();if (result != null){string punchInTime = ((DateTime)result).ToString("yyyy-MM-dd HH:mm:ss");lblCheckInTime.Text = punchInTime;lblCheckInTime.Visible = true;}}}}private void btnCheckOut_Click(object sender, EventArgs e){DateTime currentTime = DateTime.Now;string punchOutTime = currentTime.ToString("yyyy-MM-dd HH:mm:ss");if (IsInvalidPunchOutTime(currentTime)){MessageBox.Show("21点30到23:59:59点打下班卡无效。");return;}string status = currentTime.TimeOfDay < new TimeSpan(18, 0, 0) ? "早退" : "正常"; // 判断下班打卡时间是否在18:00之前InsertPunchOutRecord(punchOutTime, status);lblCheckOutTime.Text = punchOutTime;lblCheckOutTime.Visible = true;// 刷新DataGridView显示最新打卡记录RefreshDataGridView();}private bool IsInvalidPunchOutTime(DateTime currentTime){TimeSpan startTime = new TimeSpan(21, 30, 0);TimeSpan endTime = new TimeSpan(23, 59, 59);TimeSpan currentTimeOfDay = currentTime.TimeOfDay;return currentTimeOfDay >= startTime && currentTimeOfDay <= endTime;}private void InsertPunchOutRecord(string punchOutTime, string status){string query = $"INSERT INTO PunchOut (emp_code, punch_out_time, status) VALUES ('{loggedInEmployeeId}', '{punchOutTime}', '{status}')";using (SqlConnection connection = new SqlConnection(connectionString)){connection.Open();using (SqlCommand command = new SqlCommand(query, connection)){command.ExecuteNonQuery();}}}private void btn_Serch_Click(object sender, EventArgs e){// 获取所选时间范围DateTime startDate = startTime.Value.Date;DateTime endDate = endTime.Value.Date.AddDays(1).AddSeconds(-1);// 构建 SQL 查询语句string query = $@"SELECT a.id,a1.username AS 用户名,a.emp_code AS 工号,CONVERT(VARCHAR(19), a.punch_in_time, 120) AS 上班打卡时间,a.status AS 上班打卡状态,CONVERT(VARCHAR(19), b.punch_out_time, 120) AS 下班打卡时间,b.status AS 下班打卡状态FROM (SELECT * FROM Employee) a1LEFT JOIN PunchIn a ON a1.emp_code = a.emp_codeLEFT JOIN PunchOut b ON a.emp_code = b.emp_code AND CONVERT(DATE, a.punch_in_time) = CONVERT(DATE, b.punch_out_time)AND b.punch_out_time = (SELECT MAX(punch_out_time)FROM PunchOutWHERE emp_code = a.emp_code AND CONVERT(DATE, punch_out_time) = CONVERT(DATE, a.punch_in_time))WHERE a.punch_in_time BETWEEN @StartDate AND @EndDateAND a.emp_code = '{loggedInEmployeeId}'ORDER BY a.id, a.emp_code, a.punch_in_time";using (SqlConnection connection = new SqlConnection(connectionString)){using (SqlCommand command = new SqlCommand(query, connection)){// 添加查询参数command.Parameters.AddWithValue("@StartDate", startDate);command.Parameters.AddWithValue("@EndDate", endDate);Console.WriteLine("查询的SQL语句:" + query);// 打开数据库连接connection.Open();// 创建数据适配器和数据表SqlDataAdapter adapter = new SqlDataAdapter(command);DataTable table = new DataTable();// 填充数据表adapter.Fill(table);// 关闭数据库连接connection.Close();// 绑定数据表到 DataGridView 控件dataGridView1_Result.DataSource = table;}}}private void LoadData(){// 获取所选时间范围DateTime startDate = startTime.Value.Date;DateTime endDate = endTime.Value.Date.AddDays(1).AddSeconds(-1);string query = $@"SELECT a.id,a1.username AS 用户名,a.emp_code AS 工号,CONVERT(VARCHAR(19), a.punch_in_time, 120) AS 上班打卡时间,a.status AS 上班打卡状态,CONVERT(VARCHAR(19), b.punch_out_time, 120) AS 下班打卡时间,b.status AS 下班打卡状态FROM (SELECT * FROM Employee) a1LEFT JOIN PunchIn a ON a1.emp_code = a.emp_codeLEFT JOIN PunchOut b ON a.emp_code = b.emp_code AND CONVERT(DATE, a.punch_in_time) = CONVERT(DATE, b.punch_out_time)AND b.punch_out_time = (SELECT MAX(punch_out_time)FROM PunchOutWHERE emp_code = a.emp_code AND CONVERT(DATE, punch_out_time) = CONVERT(DATE, a.punch_in_time))WHERE a.punch_in_time BETWEEN @StartDate AND @EndDateAND a.emp_code = '{loggedInEmployeeId}'ORDER BY a.id, a.emp_code, a.punch_in_time";Console.WriteLine("开始时间:" + startDate);Console.WriteLine("结束时间:" + endDate);using (SqlConnection connection = new SqlConnection(connectionString)){using (SqlCommand command = new SqlCommand(query, connection)){// 添加查询参数command.Parameters.AddWithValue("@StartDate", startDate);command.Parameters.AddWithValue("@EndDate", endDate);Console.WriteLine("一加载时获取数据查询的SQL语句:" + query);// 打开数据库连接connection.Open();// 创建数据适配器和数据表SqlDataAdapter adapter = new SqlDataAdapter(command);DataTable table = new DataTable();// 填充数据表adapter.Fill(table);// 关闭数据库连接connection.Close();// 绑定数据表到 DataGridView 控件dataGridView1_Result.DataSource = table;}}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/256817.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WEB组态编辑器(BY组态)介绍

BY组态是一款非常优秀的纯前端的【web组态插件工具】&#xff0c;可无缝嵌入到vue项目&#xff0c;react项目等&#xff0c;由于是原生js开发&#xff0c;对于前端的集成没有框架的限制。同时由于BY组态只是一个插件&#xff0c;不能独立运行&#xff0c;必须嵌入到你方软件平台…

【EI征稿中|ACM出版】2023 人工智能、系统与网络安全国际学术会议 (AISNS 2023)

2023 人工智能、系统与网络安全国际学术会议 (AISNS 2023&#xff09; 2023 International Conference on Artificial Intelligence, Systems and Network Security 由西南科技大学计算机科学与技术学院主办的2023人工智能、系统与网络安全国际学术会议 (AISNS 2023&#xff…

自动数据增广论文笔记 | AutoAugment: Learning Augmentation Strategies from Data

谷歌大脑出品 paper: https://arxiv.org/abs/1805.09501 这里是个论文的阅读心得&#xff0c;笔记&#xff0c;不等同论文全部内容 文章目录 一、摘要1.1 翻译1.2 笔记 二、(第3部分)自动增强:直接在感兴趣的数据集上搜索最佳增强策略2.1 翻译2.2 笔记 三、跳出论文&#xff0c…

UE蓝图 里的函数,编辑模式在Sequence里执行

在蓝图里创建CustomFunction &#xff0c;把蓝图拖入Sequence &#xff0c;添加事件 即可调用 如果需要在Sequence里K 蓝图里的变量&#xff0c;需要勾上向过场动画公开

第十四章 : Spring Boot 整合spring-session,使用redis共享

第十四章 &#xff1a; Spring Boot 整合spring-session,使用redis共享 前沿 本文重点讲述&#xff1a;spring boot工程中使用spring-session机制进行安全认证&#xff0c;并且通过redis存储session&#xff0c;满足集群部署、分布式系统的session共享。 基于SPringBoot 2.3.2…

uni-app 微信小程序之好看的ui登录页面(二)

文章目录 1. 页面效果2. 页面样式代码 更多登录ui页面 uni-app 微信小程序之好看的ui登录页面&#xff08;一&#xff09; uni-app 微信小程序之好看的ui登录页面&#xff08;二&#xff09; uni-app 微信小程序之好看的ui登录页面&#xff08;三&#xff09; uni-app 微信小程…

最新版本——Hadoop3.3.6单机版完全部署指南

大家好&#xff0c;我是独孤风&#xff0c;大数据流动的作者。 本文基于最新的 Hadoop 3.3.6 的版本编写&#xff0c;带大家通过单机版充分了解 Apache Hadoop 的使用。本文更强调实践&#xff0c;实践是大数据学习的重要环节&#xff0c;也能在实践中对该技术有更深的理解&…

如何使用ArcGIS Pro制作类似CAD的尺寸注记

经常使用CAD制图的朋友应该比较熟悉CAD内的尺寸标注&#xff0c;这样的标注看起来直观且简洁&#xff0c;那么在ArcGIS Pro内能不能制作这样尺寸注记呢&#xff0c;答案是肯定的&#xff0c;这里为大家介绍一下制作的方法&#xff0c;希望能对你有所帮助。 数据来源 本教程所…

node.js和浏览器之间的区别

node.js是什么 Node.js是一种基于Chrome V8引擎的JavaScript运行环境&#xff0c;可以在服务器端运行JavaScript代码 Node.js 在浏览器之外运行 V8 JavaScript 引擎。 这使得 Node.js 非常高效。 浏览器如何运行js代码 nodejs运行环境 在浏览器中&#xff0c;大部分时间你所…

SLAM算法与工程实践——SLAM基本库的安装与使用(3):Pangolin库

SLAM算法与工程实践系列文章 下面是SLAM算法与工程实践系列文章的总链接&#xff0c;本人发表这个系列的文章链接均收录于此 SLAM算法与工程实践系列文章链接 下面是专栏地址&#xff1a; SLAM算法与工程实践系列专栏 文章目录 SLAM算法与工程实践系列文章SLAM算法与工程实践…

2021年第十届数学建模国际赛小美赛A题气道阻力的评估解题全过程文档及程序

2021年第十届数学建模国际赛小美赛 A题 气道阻力的评估 原题再现&#xff1a; 气道阻力的定义是通过肺气道产生单位气流所需的经肺压力的变化。更简单地说&#xff0c;它是嘴和肺泡之间的压力差&#xff0c;除以气流。影响气道阻力的因素是多方面的&#xff0c;我们需要探讨这…

Java简易版:UDP协议实现群聊

要先 运行服务端&#xff0c;在运行客户端&#xff0c;否则会报错。 服务端&#xff1a; package 二十一章;import java.io.*; import java.net.*; import java.util.ArrayList; public class T{public static ServerSocket server_socket;public static ArrayList<Socket…