using Check.DataOperate; using System; using System.Collections.Generic; using System.Data; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using UAS_MES.PublicForm; namespace UAS_CheckWork { public partial class Form1 : Form { DataHelper dh; //Create Standalone SDK class dynamicly. public zkemkeeper.CZKEMClass axCZKEM1 = new zkemkeeper.CZKEMClass(); private bool bIsConnected = false;//the boolean value identifies whether the device is connected private int iMachineNumber = 1;//the serial number of the device.After connecting the device ,this value will be changed. //开启线程 Thread InitGetInfo; List IPAddress = new List(); public Form1() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; } private void Form1_Load(object sender, EventArgs e) { IPAddress.Add("192.168.2.230"); IPAddress.Add("192.168.2.231"); IPAddress.Add("192.168.2.232"); IPAddress.Add("192.168.1.4"); IPAddress.Add("192.168.1.241"); IPAddress.Add("192.168.1.153"); IPAddress.Add("192.168.1.182"); IPAddress.Add("192.168.1.183"); IPAddress.Add("192.168.1.75"); IPAddress.Add("192.168.1.181"); IPAddress.Add("192.168.1.219"); IPAddress.Add("192.168.1.244"); CheckForIllegalCrossThreadCalls = false;//可跨线程 txtPort.Text = Properties.Settings.Default.Port; dh = new DataHelper(); DataTable dt = (DataTable)dh.ExecuteSql("select ma_user,ma_function from master", "select"); Master.DataSource = dt; Master.DisplayMember = "ma_function"; Master.ValueMember = "ma_user"; } private void getAllData_Click(object sender, EventArgs e) { string master = Master.SelectedValue.ToString(); DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + master + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=yitoadt.wicp.io)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));"; dh = new DataHelper(); int idwErrorCode = 0; for (int i = 0; i < IPAddress.Count; i++) { bIsConnected = axCZKEM1.Connect_Net(IPAddress.ToArray()[i], Convert.ToInt32(txtPort.Text)); if (bIsConnected) { lblState.Text = "状态:已连接"; iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1. axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) InitGetInfo = new Thread(InPrint); SetLoadingWindow stw = new SetLoadingWindow(InitGetInfo, "正在同步数据..."); stw.StartPosition = FormStartPosition.CenterParent; stw.ShowDialog(); } else { axCZKEM1.GetLastError(ref idwErrorCode); OperateResult.AppendText("无法连接设备,"+ IPAddress.ToArray()[i] + "ErrorCode=" + idwErrorCode.ToString() + "\n"); } } } private void InPrint() { int idwErrorCode = 0; //获取所有的考勤记录 if (axCZKEM1.ReadGeneralLogData(iMachineNumber)) { //循环获取 DataTable dt = new DataTable(); dt.Columns.Add("cl_cardcode"); dt.Columns.Add("cl_time"); string SerialNum = ""; axCZKEM1.GetSerialNumber(axCZKEM1.MachineNumber, out SerialNum); string LastDate = dh.getFieldDataByCondition("cardlog", "to_char(max(cl_time),'yyyy-MM-dd HH24:mi:ss')", "cl_address='考勤机数据导入' and cl_code='" + SerialNum + "'").ToString(); if (LastDate == "") { LastDate = "1990-01-01 00:00:00"; } string enrollNumber = "";//记录用户ID int verifyCode = 0;//记录用户验证方式 0 为密码验证,1 为指纹验证,2 为卡验证 int inoutMode = 0;//考勤状态,具体含义如下: 默认 0—Check-In 1—Check-Out 2—Break-Out 3—Break-In 4—OT-In 5—OT-Out int year = 0;//记录考勤年份 int month = 0;//记录考勤月份 int day = 0;//记录考勤日期 int hour = 0;//记录考勤小时 int minute = 0;//记录考勤分钟 int second = 0;//记录考勤秒 int workCode = 0; //记录workCode List cl_cardcode = new List(); List cl_time = new List(); List cl_machinenum = new List(); StringBuilder sql = new StringBuilder(); sql.Append("insert into cardlog (cl_id,cl_cardcode,cl_time,cl_address,cl_code) values (cardlog_seq.nextval,:cl_emcode,TO_DATE(:cl_time,'yyyy-MM-dd hh24:mi:ss'),'考勤机数据导入',:cl_machinenum)"); int count = 0; while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out enrollNumber, out verifyCode, out inoutMode, out year, out month, out day, out hour, out minute, out second, ref workCode)) { string logtime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; if (Convert.ToDateTime(logtime) > Convert.ToDateTime(LastDate)) { cl_cardcode.Add(enrollNumber); cl_time.Add(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second); cl_machinenum.Add(SerialNum); if (count % 1000 == 0) { dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray()); cl_cardcode.Clear(); cl_time.Clear(); cl_machinenum.Clear(); } count = count + 1; //将数据加入DataTable中 DataRow dr = dt.NewRow(); dr["cl_cardcode"] = enrollNumber; dr["cl_time"] = logtime; dt.Rows.Add(dr); } } if (cl_time.Count > 0) dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time", "cl_machinenum" }, cl_cardcode.ToArray(), cl_time.ToArray(), cl_machinenum.ToArray()); if (dt.Rows.Count > 0) { OperateResult.AppendText(SerialNum + "数据同步成功\n"); } else { OperateResult.AppendText(SerialNum + "没有需要同步的数据\n"); } axCZKEM1.Disconnect(); bIsConnected = false; } else { axCZKEM1.GetLastError(ref idwErrorCode); OperateResult.AppendText("Unable to get data,ErrorCode=" + idwErrorCode.ToString() + "\n"); } } private void showDataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e) { } private void ExportEmp_Click(object sender, EventArgs e) { if (!bIsConnected) { OperateResult.AppendText("请先连接设备\n"); return; } folderBrowserDialog1.Description = "选择导出的路径"; DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { string FolderPath = folderBrowserDialog1.SelectedPath; string EMenrollNumber = ""; string Name = ""; string Pwd = ""; int Privilege = 0; bool Enable = false; DataTable dt = new DataTable(); dt.Columns.Add("卡号"); dt.Columns.Add("姓名"); dt.Columns.Add("密码"); dt.Columns.Add("权限"); dt.Columns.Add("是否可用"); while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out EMenrollNumber, out Name, out Pwd, out Privilege, out Enable)) { DataRow dr = dt.NewRow(); dr["卡号"] = EMenrollNumber; dr["姓名"] = Name; dr["密码"] = Pwd; dr["权限"] = Privilege; dr["是否可用"] = Enable; dt.Rows.Add(dr); } ExcelHandler ex = new ExcelHandler(); ex.ExportExcel(dt, FolderPath); } } } }