Explorar o código

Merge branch 'master' of ssh://10.10.100.21/source/mes-client

Hcsy %!s(int64=7) %!d(string=hai) anos
pai
achega
135f96ccbd

+ 22 - 20
UAS_CheckWork/Form1.Designer.cs

@@ -36,11 +36,11 @@
             this.btnConnect = new System.Windows.Forms.Button();
             this.lblState = new System.Windows.Forms.Label();
             this.showDataGrid = new System.Windows.Forms.DataGridView();
-            this.人员编号 = new System.Windows.Forms.DataGridViewTextBoxColumn();
-            this.考勤时间 = new System.Windows.Forms.DataGridViewTextBoxColumn();
             this.getAllData = new System.Windows.Forms.Button();
             this.allData = new System.Windows.Forms.BindingSource(this.components);
             this.dataGridViewCheckBoxColumn1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
+            this.cl_cardcode = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.cl_time = new System.Windows.Forms.DataGridViewTextBoxColumn();
             ((System.ComponentModel.ISupportInitialize)(this.showDataGrid)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.allData)).BeginInit();
             this.SuspendLayout();
@@ -112,8 +112,8 @@
             this.showDataGrid.AllowUserToDeleteRows = false;
             this.showDataGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
             this.showDataGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
-            this.人员编号,
-            this.考勤时间});
+            this.cl_cardcode,
+            this.cl_time});
             this.showDataGrid.Enabled = false;
             this.showDataGrid.Location = new System.Drawing.Point(17, 59);
             this.showDataGrid.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
@@ -121,20 +121,7 @@
             this.showDataGrid.RowTemplate.Height = 30;
             this.showDataGrid.Size = new System.Drawing.Size(795, 377);
             this.showDataGrid.TabIndex = 16;
-            // 
-            // 人员编号
-            // 
-            this.人员编号.DataPropertyName = "cl_emcode";
-            this.人员编号.HeaderText = "人员编号";
-            this.人员编号.Name = "人员编号";
-            this.人员编号.Width = 150;
-            // 
-            // 考勤时间
-            // 
-            this.考勤时间.DataPropertyName = "cl_time";
-            this.考勤时间.HeaderText = "考勤时间";
-            this.考勤时间.Name = "考勤时间";
-            this.考勤时间.Width = 200;
+            this.showDataGrid.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.showDataGrid_DataError);
             // 
             // getAllData
             // 
@@ -153,6 +140,21 @@
             this.dataGridViewCheckBoxColumn1.HeaderText = "勾选";
             this.dataGridViewCheckBoxColumn1.Name = "dataGridViewCheckBoxColumn1";
             // 
+            // cl_cardcode
+            // 
+            this.cl_cardcode.DataPropertyName = "cl_cardcode";
+            this.cl_cardcode.HeaderText = "卡号";
+            this.cl_cardcode.Name = "cl_cardcode";
+            this.cl_cardcode.Width = 200;
+            // 
+            // cl_time
+            // 
+            this.cl_time.DataPropertyName = "cl_time";
+            this.cl_time.HeaderText = "打卡时间";
+            this.cl_time.MinimumWidth = 250;
+            this.cl_time.Name = "cl_time";
+            this.cl_time.Width = 250;
+            // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
@@ -189,8 +191,8 @@
         private System.Windows.Forms.Button getAllData;
         private System.Windows.Forms.BindingSource allData;
         private System.Windows.Forms.DataGridViewCheckBoxColumn dataGridViewCheckBoxColumn1;
-        private System.Windows.Forms.DataGridViewTextBoxColumn 人员编号;
-        private System.Windows.Forms.DataGridViewTextBoxColumn 考勤时间;
+        private System.Windows.Forms.DataGridViewTextBoxColumn cl_cardcode;
+        private System.Windows.Forms.DataGridViewTextBoxColumn cl_time;
     }
 }
 

+ 38 - 36
UAS_CheckWork/Form1.cs

@@ -2,6 +2,8 @@
 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;
@@ -80,11 +82,11 @@ namespace UAS_CheckWork
                 MessageBox.Show("请先连接设备", "Error");
                 return;
             }
-       
             InitGetInfo = new Thread(InPrint);
             SetLoadingWindow stw = new SetLoadingWindow(InitGetInfo, "正在同步数据...");
             stw.StartPosition = FormStartPosition.CenterParent;
             stw.ShowDialog();
+
         }
 
         private void InPrint()
@@ -95,8 +97,13 @@ namespace UAS_CheckWork
             {
                 //循环获取
                 DataTable dt = new DataTable();
-                dt.Columns.Add("cl_emcode");
+                string LastDate = dh.getFieldDataByCondition("cardlog", "to_char(max(cl_time),'yyyy-MM-dd HH24:mi:ss')", "cl_address='考勤机数据导入'").ToString();
+                dt.Columns.Add("cl_cardcode");
                 dt.Columns.Add("cl_time");
+                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 
@@ -110,28 +117,41 @@ namespace UAS_CheckWork
                 List<string> cl_cardcode = new List<string>();
                 List<string> cl_time = new List<string>();
                 StringBuilder sql = new StringBuilder();
-                sql.Append("insert into cardlog (cl_id,cl_cardcode,cl_time) values (cardlog_seq.nextval,:cl_emcode,TO_DATE(:cl_time,'yyyy-MM-dd hh24:mi:ss'))");
+                sql.Append("insert into cardlog (cl_id,cl_cardcode,cl_time,cl_address) values (cardlog_seq.nextval,:cl_emcode,TO_DATE(:cl_time,'yyyy-MM-dd hh24:mi:ss'),'考勤机数据导入')");
                 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))
                 {
-                    cl_cardcode.Add(enrollNumber);
-                    cl_time.Add(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
-                    if (count % 1000 == 0)
+                    string logtime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
+                    if (Convert.ToDateTime(logtime) > Convert.ToDateTime(LastDate))
                     {
-                        dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time" }, cl_cardcode.ToArray(), cl_time.ToArray());
-                        cl_cardcode.Clear();
-                        cl_time.Clear();
+                        cl_cardcode.Add(enrollNumber);
+                        cl_time.Add(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
+                        if (count % 1000 == 0)
+                        {
+                            dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time" }, cl_cardcode.ToArray(), cl_time.ToArray());
+                            cl_cardcode.Clear();
+                            cl_time.Clear();
+                        }
+                        count = count + 1;
+                        //将数据加入DataTable中
+                        DataRow dr = dt.NewRow();
+                        dr["cl_cardcode"] = enrollNumber;
+                        dr["cl_time"] = logtime;
+                        dt.Rows.Add(dr);
                     }
-                    count = count + 1;
-                    //将数据加入DataTable中
-                    DataRow dr = dt.NewRow();
-                    dr["cl_emcode"] = enrollNumber;
-                    dr["cl_time"] = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
-                    dt.Rows.Add(dr);
                 }
-                dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time" }, cl_cardcode.ToArray(), cl_time.ToArray());
+                if (cl_time.Count > 0)
+                    dh.BatchInsert(sql.ToString(), new string[] { "cl_cardcode", "cl_time" }, cl_cardcode.ToArray(), cl_time.ToArray());
                 allData.DataSource = dt;
                 showDataGrid.DataSource = (DataTable)allData.DataSource;
+                if (dt.Rows.Count > 0)
+                {
+                    MessageBox.Show("数据同步成功");
+                }
+                else
+                {
+                    MessageBox.Show("没有需要同步的数据");
+                }
             }
             else
             {
@@ -140,27 +160,9 @@ namespace UAS_CheckWork
             }
         }
 
-    
-
-        private void saveData_Click(object sender, EventArgs e)
+        private void showDataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
         {
-            //点击保存数据
-            string lastTime = dh.getFieldDataByCondition("cardlog", "TO_CHAR(max(wl_indate),'yyyy-MM-dd hh24:mi:ss')", "1=1").ToString();
-            //筛选数据
-            DataTable fd = dh.filterDataTable((DataTable)allData.DataSource, "cl_time > '" + lastTime + "'");
-            //插入数据库
-            //新增的数据
-            List<string> cl_emcode = new List<string>();
-            List<string> cl_time = new List<string>();
-            for (int i = 0; i < fd.Rows.Count; i++)
-            {
-                cl_emcode.Add(fd.Rows[i]["cl_emcode"].ToString());
-                cl_time.Add(fd.Rows[i]["cl_time"].ToString());
-            }
-            //批量插入
-            StringBuilder sql = new StringBuilder();
-            sql.Append("insert into cardlog (cl_id,cl_emcode,cl_time) values (cardlog_seq.nextval,:cl_emcode,TO_DATE(:cl_time,'yyyy-MM-dd hh24:mi:ss'))");
-            dh.BatchInsert(sql.ToString(), new string[] { "cl_emcode", "cl_time" }, cl_emcode.ToArray(), cl_time.ToArray());
+
         }
     }
 }

+ 2 - 8
UAS_CheckWork/Form1.resx

@@ -117,16 +117,10 @@
   <resheader name="writer">
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
-  <metadata name="人员编号.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+  <metadata name="cl_cardcode.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
     <value>True</value>
   </metadata>
-  <metadata name="考勤时间.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="人员编号.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
-    <value>True</value>
-  </metadata>
-  <metadata name="考勤时间.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+  <metadata name="cl_time.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
     <value>True</value>
   </metadata>
   <metadata name="allData.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">

+ 243 - 0
UAS_CheckWork/Form2.Designer.cs

@@ -0,0 +1,243 @@
+namespace UAS_CheckWork
+{
+    partial class Form2
+    {
+        /// <summary>
+        /// Required designer variable.
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        /// Clean up any resources being used.
+        /// </summary>
+        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows Form Designer generated code
+
+        /// <summary>
+        /// Required method for Designer support - do not modify
+        /// the contents of this method with the code editor.
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.lblState = new System.Windows.Forms.Label();
+            this.label2 = new System.Windows.Forms.Label();
+            this.label1 = new System.Windows.Forms.Label();
+            this.txtPort = new System.Windows.Forms.TextBox();
+            this.txtIP = new System.Windows.Forms.TextBox();
+            this.btnConnect = new System.Windows.Forms.Button();
+            this.startListening = new System.Windows.Forms.Button();
+            this.logInfo = new System.Windows.Forms.DataGridView();
+            this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.dataGridViewTextBoxColumn5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.UserID = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.AttState = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.VerifyMethod = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.Time = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            this.WorkCode = new System.Windows.Forms.DataGridViewTextBoxColumn();
+            ((System.ComponentModel.ISupportInitialize)(this.logInfo)).BeginInit();
+            this.SuspendLayout();
+            // 
+            // lblState
+            // 
+            this.lblState.AutoSize = true;
+            this.lblState.ForeColor = System.Drawing.Color.Crimson;
+            this.lblState.Location = new System.Drawing.Point(360, 23);
+            this.lblState.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.lblState.Name = "lblState";
+            this.lblState.Size = new System.Drawing.Size(71, 12);
+            this.lblState.TabIndex = 21;
+            this.lblState.Text = "状态:未连接";
+            // 
+            // label2
+            // 
+            this.label2.AutoSize = true;
+            this.label2.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.label2.Location = new System.Drawing.Point(168, 19);
+            this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label2.Name = "label2";
+            this.label2.Size = new System.Drawing.Size(42, 21);
+            this.label2.TabIndex = 20;
+            this.label2.Text = "Port";
+            // 
+            // label1
+            // 
+            this.label1.AutoSize = true;
+            this.label1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.label1.Location = new System.Drawing.Point(25, 17);
+            this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
+            this.label1.Name = "label1";
+            this.label1.Size = new System.Drawing.Size(25, 21);
+            this.label1.TabIndex = 19;
+            this.label1.Text = "IP";
+            // 
+            // txtPort
+            // 
+            this.txtPort.Location = new System.Drawing.Point(216, 19);
+            this.txtPort.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+            this.txtPort.Name = "txtPort";
+            this.txtPort.Size = new System.Drawing.Size(37, 21);
+            this.txtPort.TabIndex = 18;
+            this.txtPort.Text = "4370";
+            // 
+            // txtIP
+            // 
+            this.txtIP.Location = new System.Drawing.Point(59, 19);
+            this.txtIP.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+            this.txtIP.Name = "txtIP";
+            this.txtIP.Size = new System.Drawing.Size(99, 21);
+            this.txtIP.TabIndex = 17;
+            this.txtIP.Text = "192.168.253.201";
+            // 
+            // btnConnect
+            // 
+            this.btnConnect.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.btnConnect.Location = new System.Drawing.Point(280, 16);
+            this.btnConnect.Margin = new System.Windows.Forms.Padding(2, 1, 2, 1);
+            this.btnConnect.Name = "btnConnect";
+            this.btnConnect.Size = new System.Drawing.Size(50, 25);
+            this.btnConnect.TabIndex = 16;
+            this.btnConnect.Text = "连接";
+            this.btnConnect.UseVisualStyleBackColor = true;
+            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
+            // 
+            // startListening
+            // 
+            this.startListening.BackColor = System.Drawing.SystemColors.ControlLightLight;
+            this.startListening.Location = new System.Drawing.Point(451, 18);
+            this.startListening.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
+            this.startListening.Name = "startListening";
+            this.startListening.Size = new System.Drawing.Size(85, 24);
+            this.startListening.TabIndex = 22;
+            this.startListening.Text = "开始实时事件";
+            this.startListening.UseVisualStyleBackColor = false;
+            this.startListening.Click += new System.EventHandler(this.startListening_Click);
+            // 
+            // logInfo
+            // 
+            this.logInfo.AllowUserToAddRows = false;
+            this.logInfo.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
+            this.logInfo.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
+            this.UserID,
+            this.AttState,
+            this.VerifyMethod,
+            this.Time,
+            this.WorkCode});
+            this.logInfo.Location = new System.Drawing.Point(29, 65);
+            this.logInfo.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
+            this.logInfo.Name = "logInfo";
+            this.logInfo.RowTemplate.Height = 30;
+            this.logInfo.Size = new System.Drawing.Size(507, 254);
+            this.logInfo.TabIndex = 23;
+            // 
+            // dataGridViewTextBoxColumn1
+            // 
+            this.dataGridViewTextBoxColumn1.HeaderText = "用户ID";
+            this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
+            // 
+            // dataGridViewTextBoxColumn2
+            // 
+            this.dataGridViewTextBoxColumn2.HeaderText = "考勤状态";
+            this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
+            // 
+            // dataGridViewTextBoxColumn3
+            // 
+            this.dataGridViewTextBoxColumn3.HeaderText = "验证方式";
+            this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
+            // 
+            // dataGridViewTextBoxColumn4
+            // 
+            this.dataGridViewTextBoxColumn4.HeaderText = "考勤时间";
+            this.dataGridViewTextBoxColumn4.Name = "dataGridViewTextBoxColumn4";
+            // 
+            // dataGridViewTextBoxColumn5
+            // 
+            this.dataGridViewTextBoxColumn5.HeaderText = "WorkCode";
+            this.dataGridViewTextBoxColumn5.Name = "dataGridViewTextBoxColumn5";
+            // 
+            // UserID
+            // 
+            this.UserID.DataPropertyName = "UserID";
+            this.UserID.HeaderText = "用户ID";
+            this.UserID.Name = "UserID";
+            // 
+            // AttState
+            // 
+            this.AttState.DataPropertyName = "AttState";
+            this.AttState.HeaderText = "考勤状态";
+            this.AttState.Name = "AttState";
+            // 
+            // VerifyMethod
+            // 
+            this.VerifyMethod.DataPropertyName = "VerifyMethod";
+            this.VerifyMethod.HeaderText = "验证方式";
+            this.VerifyMethod.Name = "VerifyMethod";
+            // 
+            // Time
+            // 
+            this.Time.DataPropertyName = "Time";
+            this.Time.HeaderText = "考勤时间";
+            this.Time.Name = "Time";
+            // 
+            // WorkCode
+            // 
+            this.WorkCode.DataPropertyName = "WorkCode";
+            this.WorkCode.HeaderText = "WorkCode";
+            this.WorkCode.Name = "WorkCode";
+            // 
+            // Form2
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(564, 338);
+            this.Controls.Add(this.logInfo);
+            this.Controls.Add(this.startListening);
+            this.Controls.Add(this.lblState);
+            this.Controls.Add(this.label2);
+            this.Controls.Add(this.label1);
+            this.Controls.Add(this.txtPort);
+            this.Controls.Add(this.txtIP);
+            this.Controls.Add(this.btnConnect);
+            this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
+            this.Name = "Form2";
+            this.Text = "实时获取考勤";
+            this.Load += new System.EventHandler(this.Form2_Load);
+            ((System.ComponentModel.ISupportInitialize)(this.logInfo)).EndInit();
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.Label lblState;
+        private System.Windows.Forms.Label label2;
+        private System.Windows.Forms.Label label1;
+        private System.Windows.Forms.TextBox txtPort;
+        private System.Windows.Forms.TextBox txtIP;
+        private System.Windows.Forms.Button btnConnect;
+        private System.Windows.Forms.Button startListening;
+        private System.Windows.Forms.DataGridView logInfo;
+        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
+        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
+        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
+        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
+        private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
+        private System.Windows.Forms.DataGridViewTextBoxColumn UserID;
+        private System.Windows.Forms.DataGridViewTextBoxColumn AttState;
+        private System.Windows.Forms.DataGridViewTextBoxColumn VerifyMethod;
+        private System.Windows.Forms.DataGridViewTextBoxColumn Time;
+        private System.Windows.Forms.DataGridViewTextBoxColumn WorkCode;
+    }
+}

+ 155 - 0
UAS_CheckWork/Form2.cs

@@ -0,0 +1,155 @@
+using Check.DataOperate;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace UAS_CheckWork
+{
+    public partial class Form2 : Form
+    {
+        public Form2()
+        {
+            InitializeComponent();
+        }
+
+        DataHelper dh = new DataHelper();
+        //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.
+
+        private void Form2_Load(object sender, EventArgs e)
+        {
+            DataTable dt = new DataTable();
+            dt.Columns.Add("UserID");
+            dt.Columns.Add("AttState");
+            dt.Columns.Add("VerifyMethod");
+            dt.Columns.Add("Time");
+            dt.Columns.Add("WorkCode");
+            logInfo.DataSource = dt;
+        }
+
+        private void btnConnect_Click(object sender, EventArgs e)
+        {
+            if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")
+            {
+                MessageBox.Show("请先输入IP和端口", "Error");
+                return;
+            }
+            int idwErrorCode = 0;
+
+            Cursor = Cursors.WaitCursor;
+            if (btnConnect.Text == "断开连接")
+            {
+                axCZKEM1.Disconnect();
+                bIsConnected = false;
+                btnConnect.Text = "连接";
+                lblState.Text = "状态:未连接";
+                startListening.Text = "开始实时事件";
+                Cursor = Cursors.Default;
+                return;
+            }
+            bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text));
+            if (bIsConnected)
+            {
+                btnConnect.Text = "断开连接";
+                btnConnect.Refresh();
+                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)
+            }
+            else
+            {
+                axCZKEM1.GetLastError(ref idwErrorCode);
+                MessageBox.Show("无法连接设备,ErrorCode=" + idwErrorCode.ToString(), "Error");
+            }
+            Cursor = Cursors.Default;
+        }
+
+        private void startListening_Click(object sender, EventArgs e)
+        {
+            if (!bIsConnected)
+            {
+                MessageBox.Show("请先连接设备", "Error");
+                return;
+            }
+            if (startListening.Text == "正在实时监听")
+            {
+                MessageBox.Show("已启动监听", "Error");
+                return;
+            }
+            int idwErrorCode = 0;
+            //开始注册考勤机的监听事件
+            if (axCZKEM1.RegEvent(iMachineNumber, 1))//65535表示注册所有的事件
+            {
+                //注册事件
+                this.axCZKEM1.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(axCZKEM1_OnAttTransactionEx);
+                //注册成功提示用户
+                startListening.Text = "正在实时监听";
+            }
+            else
+            {
+                axCZKEM1.GetLastError(ref idwErrorCode);
+                MessageBox.Show("注册监听事件失败,ErrorCode=" + idwErrorCode.ToString(), "Error");
+            }
+        }
+        /// <summary>
+        /// 考勤机验证通过
+        /// </summary>
+        /// <param name="sEnrollNumber">UserID</param>
+        /// <param name="iIsInValid">1 为无效记录,0 为有效记录</param>
+        /// <param name="iAttState">0—Check-In   1—Check-Out  2—Break-Out  3—Break-In   4—OT-In   5—OT-Out </param>
+        /// <param name="iVerifyMethod">:0 为密码验证,1 为指纹验证,2 为卡验证 </param>
+        /// <param name="iYear"></param>
+        /// <param name="iMonth"></param>
+        /// <param name="iDay"></param>
+        /// <param name="iHour"></param>
+        /// <param name="iMinute"></param>
+        /// <param name="iSecond"></param>
+        /// <param name="iWorkCode">WorkCode 返回验证时 WorkCode 值,当机器无 Workcode 功能时,该值返回 0</param>
+        private void axCZKEM1_OnAttTransactionEx(string sEnrollNumber, int iIsInValid, int iAttState, int iVerifyMethod, int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond, int iWorkCode)
+        {
+            //实现实时获取考勤,取有效记录
+            if (iIsInValid == 0)
+            {
+                //显示
+                DataTable dt = (DataTable)logInfo.DataSource;
+                DataRow dr = dt.NewRow();//新建行
+                dr["UserID"] = sEnrollNumber;
+                switch (iVerifyMethod)
+                {
+                    case 0:
+                        dr["VerifyMethod"] = "密码";
+                        break;
+                    case 1:
+                        dr["VerifyMethod"] = "指纹";
+                        break;
+                    case 2:
+                        dr["VerifyMethod"] = "卡";
+                        break;
+                    default:
+                        break;
+                }
+                string Time = iYear + "-" + iMonth + "-" + iDay + " " + iHour + ":" + iMinute + ":" + iSecond;
+                dr["Time"] = Time;
+                dr["WorkCode"] = iWorkCode;
+                dt.Rows.Add(dr);
+                logInfo.DataSource = dt;
+                //更新数据库
+                dh.ExecuteSql("insert into cardlog (cl_id,cl_cardcode,cl_time,cl_address) values (cardlog_seq.nextval,'" + sEnrollNumber + "',TO_DATE('" + Time + "','yyyy-MM-dd hh24:mi:ss'),'考勤机数据导入')", "insert");
+            }
+            else
+            {
+                //记录验证无效
+            }
+        }
+    }
+}

+ 135 - 0
UAS_CheckWork/Form2.resx

@@ -0,0 +1,135 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <metadata name="UserID.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+    <value>True</value>
+  </metadata>
+  <metadata name="AttState.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+    <value>True</value>
+  </metadata>
+  <metadata name="VerifyMethod.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+    <value>True</value>
+  </metadata>
+  <metadata name="Time.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+    <value>True</value>
+  </metadata>
+  <metadata name="WorkCode.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+    <value>True</value>
+  </metadata>
+</root>

+ 70 - 3
UAS_CheckWork/Program.cs

@@ -1,6 +1,8 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Security.Principal;
+using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 
@@ -14,9 +16,74 @@ namespace UAS_CheckWork
         [STAThread]
         static void Main()
         {
-            Application.EnableVisualStyles();
-            Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new Form1());
+            try
+            {
+                WindowsIdentity identity = WindowsIdentity.GetCurrent();
+                WindowsPrincipal principal = new WindowsPrincipal(identity);
+                //设置应用程序处理异常方式:ThreadException处理
+                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
+                //处理UI线程异常
+                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
+                //处理非UI线程异常
+                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
+                #region 应用程序的主入口点
+                Application.EnableVisualStyles();
+                Application.SetCompatibleTextRenderingDefault(false);
+                ////处理全局事件
+                //GlobalEventsHandler g = new GlobalEventsHandler();
+                //添加全局事件的监听
+                //Application.AddMessageFilter(g);
+                if (principal.IsInRole(WindowsBuiltInRole.Administrator))
+                    Application.Run(new Form1());
+                else
+                {
+                    //创建启动对象
+                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
+                    // 设置运行文件
+                    startInfo.FileName = Application.ExecutablePath;
+                    //设置启动动作,确保以管理员身份运行
+                    startInfo.Verb = "runas";
+                    //如果不是管理员,则启动UAC
+                    System.Diagnostics.Process.Start(startInfo);
+                }
+                #endregion
+            }
+            catch (Exception ex)
+            {
+                string str = GetExceptionMsg(ex, string.Empty);
+                MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
+        }
+        //处理线程的异常
+        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
+        {
+            string str = GetExceptionMsg(e.Exception, e.ToString());
+            MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
+        }
+
+        //未处理的异常统一通过这里返回
+        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+        {
+            string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
+            MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
+        }
+
+        /// <summary>
+        /// 生成自定义异常消息
+        /// </summary>
+        /// <param name="ex">异常对象</param>
+        /// <param name="backStr">备用异常消息:当ex为null时有效</param>
+        /// <returns>异常字符串文本</returns>
+        static string GetExceptionMsg(Exception ex, string backStr)
+        {
+            StringBuilder sb = new StringBuilder();
+            if (ex != null)
+            {
+                sb.AppendLine(ex.Message);
+                //sb.AppendLine("【异常方法】:" + ex.StackTrace);
+            }
+            //else { sb.AppendLine("【未处理异常】:" + backStr); }
+            return sb.ToString();
         }
     }
 }

+ 9 - 0
UAS_CheckWork/UAS_CheckWork.csproj

@@ -61,6 +61,12 @@
     <Compile Include="Form1.Designer.cs">
       <DependentUpon>Form1.cs</DependentUpon>
     </Compile>
+    <Compile Include="Form2.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="Form2.Designer.cs">
+      <DependentUpon>Form2.cs</DependentUpon>
+    </Compile>
     <Compile Include="LoadingCircle.cs">
       <SubType>Component</SubType>
     </Compile>
@@ -79,6 +85,9 @@
     <EmbeddedResource Include="Form1.resx">
       <DependentUpon>Form1.cs</DependentUpon>
     </EmbeddedResource>
+    <EmbeddedResource Include="Form2.resx">
+      <DependentUpon>Form2.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Properties\Resources.resx">
       <Generator>ResXFileCodeGenerator</Generator>
       <LastGenOutput>Resources.Designer.cs</LastGenOutput>