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.mzph.cn/news/206671.shtml

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

相关文章

Java零基础——Elasticsearch篇

1.Elasticsearch简介 Elasticsearch是一个基于Lucene的一个开源的分布式、RESTful 风格的搜索和数据分析引擎。Elasticsearch是用Java语言开发的&#xff0c;并作为Apache许可条款下的开放源码发布&#xff0c;是一种流行的企业级搜索引擎。Elasticsearch用于云计算中&#xf…

【Ambari】Python调用Rest API 获取YARN HA状态信息并发送钉钉告警

&#x1f984; 个人主页——&#x1f390;开着拖拉机回家_Linux,大数据运维-CSDN博客 &#x1f390;✨&#x1f341; &#x1fa81;&#x1f341;&#x1fa81;&#x1f341;&#x1fa81;&#x1f341;&#x1fa81;&#x1f341; &#x1fa81;&#x1f341;&#x1fa81;&am…

二层交换原理

二层交换设备工作在OSI模型的第二层&#xff0c;即数据链路层&#xff0c;它对数据包的转发是建立在MAC&#xff08;Media Access Control &#xff09;地址基础之上的。二层交换设备不同的接口发送和接收数据独立&#xff0c;各接口属于不同的冲突域&#xff0c;因此有效地隔离…

【C/PTA —— 15.结构体2(课内实践)】

C/PTA —— 15.结构体2&#xff08;课内实践&#xff09; 7-1 计算职工工资7-2 计算平均成绩7-3 找出总分最高的学生7-4 通讯录的录入与显示 7-1 计算职工工资 #include<stdio.h> #include<stdlib.h> typedef struct GZ {char name[6];double j;double f;double z;…

记一次由 jedis 引发的离谱选学问题

背景 我的应用中&#xff0c;使用 jedis 作为连接 redis 的客户端&#xff0c;一直在用的好好的&#xff0c;后来有一个新的组件&#xff0c;也需要使用 redis&#xff0c;但是组件是内部封装的&#xff0c;我只能提供一个 StringReidsTempalte&#xff0c;所以我基于应用本身…

Java 多线程之 LockSupport (阻塞和唤醒线程)

文章目录 一、概述二、使用方法三、测试示例1四、测试示例2 一、概述 LockSupport 是Java并发包中的一个工具类&#xff0c;用于线程的阻塞和唤醒。它提供了一种基于线程的许可&#xff08;permit&#xff09;的方式来实现线程的阻塞和唤醒&#xff0c;而不需要显式地使用锁。例…

【无线网络技术】——无线广域网(学习笔记)

&#x1f4d6; 前言&#xff1a;无线广域网(WWAN)是指覆盖全国或全球范围内的无线网络&#xff0c;提供更大范围内的无线接入&#xff0c;与无线个域网、无线局域网和无线城域网相比&#xff0c;它更加强调的是快速移动性。典型的无线广域网&#xff1a;蜂窝移动通信系统和卫星…

Linux UUCP命令教程:如何在Linux系统中进行文件复制(附实例详解和注意事项)

Linux UUCP命令介绍 UUCP&#xff08;Unix-to-Unix Copy&#xff09;是一套允许远程执行命令和传输文件的程序。UUCP命令是该套件中的一个程序&#xff0c;它为请求文件复制操作提供了用户界面。UUCP套件还包括uux&#xff08;远程命令执行的用户界面&#xff09;、uucico&…

Java期末复习题之抽象类、接口

点击返回标题->23年Java期末复习-CSDN博客 第1题. 首先设计一个学生抽象类Student&#xff0c;其数据成员有name(姓名)、age(年龄)和degree(学位)&#xff0c;以及一个抽象方法show()。然后由Student类派生出本科生类Undergraduate和研究生类Graduate&#xff0c;本科生类Un…

js moment计算当前时间到24:00:00的剩余时间

2023.12.7今天我学习了如何计算当前的时间到24:00:00剩下的时间&#xff0c;https://momentjs.cn/ const now moment(); // 获取当前时间const endOfDay moment().endOf(day); // 设置当天的 23:59:59const duration moment.duration(endOfDay.diff(now)); // 计算剩余时间的…

第 7 部分 — 增强 LLM 安全性的策略:数学和伦理框架

一、说明 增强大型语言模型 (LLM) 安全性的追求是技术创新、道德考虑和实际应用的复杂相互作用。这项努力需要一种深入而富有洞察力的方法&#xff0c;将先进的数学模型与道德原则和谐地融合在一起&#xff0c;以确保LLM的发展不仅在技术上稳健&#xff0c;而且在道德上合理且对…

C#winform点击按钮下载数据库中表的字段到Excel上

C#winform点击按钮下载数据库中表的字段到Excel上 需求&#xff1a;C#winform点击按钮下载数据库中表的字段到Excel&#xff0c;并计算下载消耗的时间以及文件存放位置。 C#实现 using System; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; u…

Flutter 如何更新showModalBottomSheet 中的数据

showDialog(context: context,builder: (context) {String label test;//StatefulBuilderreturn StatefulBuilder(//在这里为了区分&#xff0c;在构建builder的时候将setState方法命名为了setDialogState。builder: (context, setDialogState) {print(label $label);return …

【LeetCode】268. 丢失的数字

268. 丢失的数字 难度&#xff1a;简单 题目 给定一个包含 [0, n] 中 n 个数的数组 nums &#xff0c;找出 [0, n] 这个范围内没有出现在数组中的那个数。 示例 1&#xff1a; 输入&#xff1a;nums [3,0,1] 输出&#xff1a;2 解释&#xff1a;n 3&#xff0c;因为有 3…

[Makefile] include 关键字

在 Makefile 中&#xff0c;include 关键字的作用是引入其他文件的内容&#xff0c;通常用于将其他 Makefile 文件&#xff08;通常是头文件&#xff09;的内容包含到当前的 Makefile 中。这样可以实现模块化管理和代码重用。 include使用 使用 include 关键字的语法如下&…

网络攻击(一)--安全渗透简介

1. 安全渗透概述 目标 了解渗透测试的基本概念了解渗透测试从业人员的注意事项 1.1. 写在前面的话 在了解渗透测试之前&#xff0c;我们先看看&#xff0c;信息安全相关的法律是怎么样的 中华人民共和国网络安全法 《中华人民共和国网络安全法》由全国人民代表大会常务委员会…

Spring Cloud切换内嵌Tomcat为宝兰德Application Server

目录 替换Tomcat中间件Tomcat是什么Spring Cloud剔除tomcat引入宝兰德Application Server打包运行授权导入 替换Tomcat中间件 Tomcat是什么 Spring Cloud剔除tomcat <!--集成springmvc框架 --><dependency><groupId>org.springframework.boot</groupId&…

Boost:asio多io_service,多线程run

多io_service,多线程run,相当于多个线程分别处理注册在不同io_service上的回调,也就是每个线程排某个io_service的异步处理: //mio_mth.cpp #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp> #include <iostream>…

MAC PHP版本安装问题

安装php 7.4版本不成功 Error: php7.4 has been disabled because it is a versioned formula! 因为php7.4官方已经不再维护&#xff0c;所以Hombrew将该php版本移出了repository&#xff0c;所以安装不了。 解决办法 从第三方仓库中安装 //将第三方仓库加入brew brew tap sh…

7.1 C++11指针空值—nullptr

一、NULL和nullptr区别 NULL是宏定义&#xff0c;nullptr是关键字。 #ifndef NULL#ifdef __cplusplus#define NULL 0#else#define NULL ((void *)0)#endif #endif nullptr可以隐式转换为任意指针类型&#xff0c;而NULL需要显示转换 void func(char *) {std::cout <<…