- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
- [C#] Chia sẻ source code tạo hiệu ứng pixel Image trên winform
- [C#] Hướng dẫn kiểm tra số Container hợp lệ hay không
[C#] Chia sẻ source code Download dữ liệu máy chấm công Ronald Jack sử dụng thư viện Zkemkeeper.dll
Xin chào các bạn, bài viết này mình chia sẻ các bạn source code đọc dữ liệu máy chấm công Ronald Jack viết dưới dạng Console Windows, sử dụng thư viện Zkemkeeper.dll
[C#] Source code download AttLog Zkemkeeper.dll with SQLSERVER
Ứng dụng viết dưới dạng Console, giúp bạn có thể dễ dàng đưa vào CronJob
hay Scheduler
để đến giờ nó tự động tải dữ liệu về và upload dữ liệu lên SQL SERVER.
Đầu tiên, các bạn cần tạo cho mình một bảng Table AttLog
CREATE TABLE [dbo].[AttLog]
(
[id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_AttLog_id] DEFAULT (newsequentialid()),
[ipAddress] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[userID] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[dateTimeRecord] [datetime] NULL,
[machineID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AttLog] ADD CONSTRAINT [PK_AttLog] PRIMARY KEY CLUSTERED ([id]) ON [PRIMARY]
GO
Kết quả khi tải dữ liệu chúng ta sẽ có được data như hình bên dưới:
Bao gồm các thông tin cơ bản: IpAddress, userID, DateTimeRecord và MachineID
Dưới đây, là file cấu hình config.json
, để cấu hình thông tin cơ sở dữ liệu sqlsever và danh sách máy chấm công.
{
"server": "TMV2209068SQLEXPRESS",
"database": "WPFDemo",
"username": "sa",
"password": "LapTrinhVBNet@2022",
"numDateDownload": 7, // -1 kh�ng filter
"ronaljack": [
{
"ipaddress": "192.168.x.x",
"port": "4370",
"machineID": 15
},
{
"ipaddress": "192.168.x.x",
"port": "4370",
"machineID": 12
}
]
}
Thuộc tính numDateDownload = 7, là nó chỉ lưu dữ liệu chấm công trong vòng 7 ngày tính từ ngày hiện tại.
Nếu các bạn muốn lấy tất cả thì setup nó bằng: -1
Video demo ứng dụng tải dữ liệu:
Ở bài viết, mình có tạo effect typing
trên console, các bạn khi sử dụng có thể comment nó lại để tăng tốc độ xử lý.
Source code C#:
using ConsoleApp.Helpers;
using ConsoleApp.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VBSQLHelper;
using System.IO;
namespace ConsoleApp
{
public class AttHelper
{
DeviceManipulator manipulator = new DeviceManipulator();
public ZkemClient objZkeeper;
private bool isDeviceConnected = false;
private int MACHINE_NUMBER = 1;
private string _ipAddress;
public AttHelper(string ipAddress, string port, int machine_number)
{
this._ipAddress = ipAddress;
MACHINE_NUMBER = machine_number;
ConnectDevice(ipAddress, port);
}
public void DownloadATTLog(ConfigFile configFile) {
try
{
ICollection<MachineInfo> lstMachineInfo = manipulator.GetLogData(objZkeeper, MACHINE_NUMBER);
if (lstMachineInfo != null && lstMachineInfo.Count > 0)
{
ConsoleHelper.ShowSuccessMessage(lstMachineInfo.Count + " dữ liệu được tìm thấy !!");
var songayCanlay = configFile.numDateDownload;
if (songayCanlay > -1)
{
var listnewFilter = lstMachineInfo.Where(x => (DateTime.Now - x.DateOnlyRecord).TotalDays <= songayCanlay).ToList(); // filter lại theo số ngày tăng hiệu suất lưu sql
ConsoleHelper.ShowFinishMessage($"Số dòng đã Filter theo {songayCanlay} ngày: " + listnewFilter.Count + " dữ liệu được tìm thấy.");
// Xử lý lưu dữ liệu vào DataBase
SaveATTLogToDataBase(listnewFilter);
}
else {
// Xử lý lưu dữ liệu vào DataBase
SaveATTLogToDataBase(lstMachineInfo);
}
}
else
ConsoleHelper.ShowErrorMessage("Không tìm thấy dữ liệu");
}
catch (Exception ex)
{
ConsoleHelper.ShowErrorMessage("[LỖI]: "+ ex.Message);
}
}
private void SaveATTLogToDataBase(ICollection<MachineInfo> lstMachineInfo)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
string query = $@"INSERT INTO AttLog(ipAddress,
userID,
dateTimeRecord, machineID)
SELECT @ipAddress,
@userID,
@dateTimeRecord,
@machineID
WHERE NOT EXISTS ( SELECT 0 FROM AttLog WHERE ipAddress=@ipAddress and userID=@userID and dateTimeRecord=@dateTimeRecord)
";
using (var connection = new SqlConnection(SQLHelper.CONNECTION_STRINGS))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
{
using (var cmd = new SqlCommand(query, connection, transaction))
{
int result_count = 0;
cmd.CommandType = CommandType.Text;
cmd.Transaction = transaction;
try
{
foreach (var item in lstMachineInfo)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@ipAddress", _ipAddress);
cmd.Parameters.AddWithValue("@userID", item.IndRegID);
cmd.Parameters.AddWithValue("@dateTimeRecord", item.DateTimeRecord);
cmd.Parameters.AddWithValue("@machineID", item.MachineNumber);
result_count += cmd.ExecuteNonQuery();
}
transaction.Commit();
}
catch (Exception ex)
{
try
{
transaction.Rollback();
}
catch (SqlException exx)
{
if (transaction.Connection != null)
{
ConsoleHelper.ShowErrorMessage($"An exception of type {exx.GetType()} was encountered while attempting to roll back the transaction.");
}
}
ConsoleHelper.ShowErrorMessage($"Neither record was written to database.");
}
finally
{
connection.Close();
ConsoleHelper.ShowSuccessMessage($"Upload dữ liệu hoàn tất. Đã thêm mới được {result_count} tin.");
}
}
}
}
ConsoleHelper.ShowDefaultMessage(String.Format("{0} seconds with one transaction.", stopwatch.Elapsed.TotalSeconds));
}
private void ConnectDevice(string ipAddress, string port) {
try
{
if (IsDeviceConnected)
{
IsDeviceConnected = false;
return;
}
if (ipAddress == string.Empty || port == string.Empty)
ConsoleHelper.ShowErrorMessage("Địa chỉ IP và cổng của thiết bị là bắt buộc nhập !!");
int portNumber = 4370;
if (!int.TryParse(port, out portNumber))
ConsoleHelper.ShowErrorMessage("Cổng nhập phải là dạng số!");
bool isValidIpA = UniversalStatic.ValidateIP(ipAddress);
if (!isValidIpA)
ConsoleHelper.ShowErrorMessage("Địa chỉ IP không hợp lệ !!");
isValidIpA = UniversalStatic.PingTheDevice(ipAddress);
if (!isValidIpA)
ConsoleHelper.ShowErrorMessage("Thiết bị tại địa chỉ IP:" + ipAddress + " và Port:" + port + " không phản hồi !!");
objZkeeper = new ZkemClient(RaiseDeviceEvent);
IsDeviceConnected = objZkeeper.Connect_Net(ipAddress, portNumber);
if (IsDeviceConnected)
{
string deviceInfo = manipulator.FetchDeviceInfo(objZkeeper, MACHINE_NUMBER);
ConsoleHelper.ShowDefaultMessage($"===================== {ipAddress} ===============================");
ConsoleHelper.ShowSuccessMessage(deviceInfo);
ConsoleHelper.ShowDefaultMessage("===================================================================");
}
}
catch (Exception ex)
{
ConsoleHelper.ShowErrorMessage("[LỖI]: " + ex.Message);
}
}
private void RaiseDeviceEvent(object sender, string actionType)
{
switch (actionType)
{
case UniversalStatic.acx_Disconnect:
{
ConsoleHelper.ShowSuccessMessage("Thiết bị đã tắt");
break;
}
default:
break;
}
}
private bool IsDeviceConnected
{
get { return isDeviceConnected; }
set
{
isDeviceConnected = value;
if (isDeviceConnected)
{
ConsoleHelper.ShowSuccessMessage("Kết nối với máy chấm công thành công !!");
}
else
{
ConsoleHelper.ShowSuccessMessage("Máy chấm công đã ngắt !!");
objZkeeper.Disconnect();
}
}
}
}
}
Ở bài viết, khi lưu trữ dữ liệu tránh trùng lắp mình cũng đã sử dụng Sqlserver Transaction để tối ưu tốc độ và kiểm tra dữ liệu nào đã tải về thi không lưu vào cơ sở dữ liệu SQL nữa.
Thanks for watching!