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.pswp.cn/news/206671.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/206671.shtml
英文地址,請注明出處:http://en.pswp.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 <<…