C#實現(xiàn)員工ID卡的識別功能
更新時間:2023年01月03日 10:04:51 作者:芝麻粒兒
這篇文章主要為大家詳細介紹了C#如何實現(xiàn)識別員工ID卡的功能,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實踐過程
效果
代碼
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate int HookProc(int nCode, int wParam, IntPtr lParam); static int hHook = 0; public const int WH_KEYBOARD_LL = 13; //LowLevel鍵盤截獲,如果是WH_KEYBOARD=2,并不能對系統(tǒng)鍵盤截取,Acrobat Reader會在你截取之前獲得鍵盤。 HookProc KeyBoardHookProcedure; [DllImport("kernel32")] public static extern int Beep(int dwFreq, int dwDuration);//讓計算機蜂鳴 string DataPath = "";//數(shù)據(jù)庫路徑 OleDbConnection con;//OleDbConnection對象,連接數(shù)據(jù)庫 OleDbCommand cmd;//OleDbCommand對象,執(zhí)行SQL語句 //鍵盤Hook結(jié)構(gòu)函數(shù) [StructLayout(LayoutKind.Sequential)] public class KeyBoardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } [DllImport("user32.dll")] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] //抽掉鉤子 public static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll")] //調(diào)用下一個鉤子 public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string name); public string getNum(string code) { string flag = ""; switch (code) { case "048": flag="0"; break; case "049": flag = "1"; break; case "050": flag = "2"; break; case "051": flag = "3"; break; case "052": flag = "4"; break; case "053": flag = "5"; break; case "054": flag = "6"; break; case "055": flag = "7"; break; case "056": flag = "8"; break; case "057": flag = "9"; break; } return flag; } public void Hook_Start() { // 安裝鍵盤鉤子 if (hHook == 0) { KeyBoardHookProcedure = new HookProc(KeyBoardHookProc); hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); //如果設(shè)置鉤子失敗. if (hHook == 0) { Hook_Clear(); } } } //取消鉤子事件 public void Hook_Clear() { bool retKeyboard = true; if (hHook != 0) { retKeyboard = UnhookWindowsHookEx(hHook); hHook = 0; } //如果去掉鉤子失敗. if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed."); } //這里可以添加自己想要的信息處理 string NumCode=""; public int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam) { if (nCode >= 0) { if (wParam == 0x0104 || wParam == 0x0100) { KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct)); int flag = kbh.vkCode; switch (flag) { case 96: NumCode += "0"; break; case 97: NumCode += "1"; break; case 98: NumCode += "2"; break; case 99: NumCode += "3"; break; case 100: NumCode += "4"; break; case 101: NumCode += "5"; break; case 102: NumCode += "6"; break; case 103: NumCode += "7"; break; case 104: NumCode += "8"; break; case 105: NumCode += "9"; break; } if (flag == 13) { if (NumCode.Length != 0) { string c = ""; string id = ""; for (int i = 0; i <= NumCode.Length - 3; i += 3) { string b = NumCode.Substring(i, 3); c += getNum(b); } id = c; if (id.Length == 8)//如果卡號為8位 { //實例化OleDbConnection對象 con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + DataPath); con.Open();//打開數(shù)據(jù)庫連接 //實例化OleDbCommand對象,根據(jù)ID卡號檢索數(shù)據(jù)表 cmd = new OleDbCommand("select * from tb_UserInfo where CardID='" + id + "'", con); OleDbDataReader sdr = cmd.ExecuteReader();//實例化OleDbDataReader對象 sdr.Read();//讀取記錄 txtShowCardID.Text = id;//獲取ID卡號 txtShowName.Text = sdr["UName"].ToString();//獲取員工姓名 cbbShowSex.Text = sdr["USex"].ToString();//獲取員工性別 cbbShowDep.Text = sdr["UDep"].ToString();//獲取員工部門 con.Close();//關(guān)閉數(shù)據(jù)庫連接 Beep(3000, 100);//計算機蜂鳴 } NumCode = ""; } } } } return CallNextHookEx(hHook, nCode, wParam, lParam); } private void Form1_Load(object sender, EventArgs e) { cbbdep.SelectedIndex = 0;//設(shè)置部門下拉框的第一項被選中 cbbsex.SelectedIndex = 0;//設(shè)置性別下拉框的第一項被選中 //獲取數(shù)據(jù)庫路徑 DataPath = Application.StartupPath.ToString(); DataPath = DataPath.Substring(0, DataPath.LastIndexOf("\\")); DataPath = DataPath.Substring(0, DataPath.LastIndexOf("\\")); DataPath += @"\db.mdb"; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { groupBox1.Enabled = true; Hook_Clear(); } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { groupBox1.Enabled = false; Hook_Start(); } } private void button2_Click(object sender, EventArgs e) { txtIdcard.Text = ""; txtName.Text = ""; } private void button1_Click(object sender, EventArgs e) { if (txtIdcard.Text == "" || txtName.Text == "")//如果沒有輸入ID卡號和員工姓名 { //彈出警告信息 if (MessageBox.Show("請將數(shù)據(jù)填寫完整!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { if (txtIdcard.Text == "")//如果沒有輸入ID卡號 txtIdcard.Focus();//則光標處在輸入ID卡號的文本框 if (txtName.Text == "")//如果沒有輸入員工姓名 txtName.Focus();//則光標處在輸入員工姓名的文本框 if (txtIdcard.Text == "" && txtName.Text == "")//如果都沒輸入數(shù)據(jù) txtIdcard.Focus();//則光標處在輸入ID卡號的文本框 } } else//如果輸入了數(shù)據(jù) { if (txtIdcard.Text.Trim().Length != 8)//如果輸入的ID卡號不是8位 { //彈出警告信息 if (MessageBox.Show("ID卡號必須為8位!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { txtIdcard.Text = "";//清空輸入ID卡號的文本框 txtIdcard.Focus();//讓光標處在輸入ID卡號的文本框上 } } else//如果輸入的ID卡號為8位 { //實例化OleDbConnection對象 con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + DataPath); con.Open();//打開連接 //實例化OleDbCommand對象 cmd = new OleDbCommand("select count(*) from tb_UserInfo where CardID='"+txtIdcard.Text.Trim()+"'", con); int flag =Convert.ToInt32(cmd.ExecuteScalar());//判斷是否已經(jīng)添加過此ID卡號 if (flag > 0)//如果大于0則說明已經(jīng)添加過 { //彈出警告信息 if (MessageBox.Show("ID卡號已經(jīng)添加過了!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK) { button2_Click(sender, e);//清空輸入ID卡號和員工姓名的文本框 } } else//如果小于0說明沒有添加過 { //實例化OleDbCommand對象 cmd = new OleDbCommand("insert into tb_UserInfo(CardID,UName,USex,UDep) values ('" + txtIdcard.Text.Trim() + "','" + txtName.Text.Trim() + "','" + cbbsex.Text.Trim() + "','" + cbbdep.Text.Trim() + "')", con); int k = cmd.ExecuteNonQuery();//執(zhí)行insert語句,將輸入的信息添加到數(shù)據(jù)庫中 if (k > 0)//如果大于0則操作成功 { //彈出提示信息 if (MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK) { button2_Click(sender, e);//清空輸入ID卡號和員工姓名的文本框 } } } con.Close();//關(guān)閉數(shù)據(jù)庫連接 } } } private void txtIdcard_KeyPress(object sender, KeyPressEventArgs e) { if (!(e.KeyChar <= '9' && e.KeyChar >= '0') && e.KeyChar != '\r' && e.KeyChar != '\b') { e.Handled = true; } } }
partial class Form1 { /// <summary> /// 必需的設(shè)計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設(shè)計器生成的代碼 /// <summary> /// 設(shè)計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(nèi)容。 /// </summary> private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.txtIdcard = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.cbbdep = new System.Windows.Forms.ComboBox(); this.label4 = new System.Windows.Forms.Label(); this.cbbsex = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); this.txtName = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.cbbShowDep = new System.Windows.Forms.ComboBox(); this.label5 = new System.Windows.Forms.Label(); this.cbbShowSex = new System.Windows.Forms.ComboBox(); this.label6 = new System.Windows.Forms.Label(); this.txtShowName = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.txtShowCardID = new System.Windows.Forms.TextBox(); this.label8 = new System.Windows.Forms.Label(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.txtIdcard); this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.cbbdep); this.groupBox1.Controls.Add(this.label4); this.groupBox1.Controls.Add(this.cbbsex); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.txtName); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(10, 31); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(435, 120); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "輸入員工信息"; // // txtIdcard // this.txtIdcard.Location = new System.Drawing.Point(69, 21); this.txtIdcard.Name = "txtIdcard"; this.txtIdcard.Size = new System.Drawing.Size(137, 21); this.txtIdcard.TabIndex = 4; this.txtIdcard.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtIdcard_KeyPress); // // button2 // this.button2.Location = new System.Drawing.Point(214, 91); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 9; this.button2.Text = "重置"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.Location = new System.Drawing.Point(131, 91); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 8; this.button1.Text = "添加"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // cbbdep // this.cbbdep.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbbdep.FormattingEnabled = true; this.cbbdep.Items.AddRange(new object[] { "C#部門", "ASP.NET部門", "基礎(chǔ)部", "VB部門", "VC部門", "JAVA部門"}); this.cbbdep.Location = new System.Drawing.Point(279, 57); this.cbbdep.Name = "cbbdep"; this.cbbdep.Size = new System.Drawing.Size(137, 20); this.cbbdep.TabIndex = 7; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(212, 60); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(65, 12); this.label4.TabIndex = 6; this.label4.Text = "所屬部門:"; // // cbbsex // this.cbbsex.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbbsex.FormattingEnabled = true; this.cbbsex.Items.AddRange(new object[] { "男職工", "女職工"}); this.cbbsex.Location = new System.Drawing.Point(69, 57); this.cbbsex.Name = "cbbsex"; this.cbbsex.Size = new System.Drawing.Size(137, 20); this.cbbsex.TabIndex = 5; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(6, 61); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(65, 12); this.label3.TabIndex = 4; this.label3.Text = "員工性別:"; // // txtName // this.txtName.Location = new System.Drawing.Point(279, 21); this.txtName.Name = "txtName"; this.txtName.Size = new System.Drawing.Size(137, 21); this.txtName.TabIndex = 3; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(212, 26); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 2; this.label2.Text = "員工姓名:"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(6, 26); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(65, 12); this.label1.TabIndex = 0; this.label1.Text = "ID卡編號:"; // // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.Checked = true; this.radioButton1.Location = new System.Drawing.Point(10, 8); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(95, 16); this.radioButton1.TabIndex = 1; this.radioButton1.TabStop = true; this.radioButton1.Text = "添加員工信息"; this.radioButton1.UseVisualStyleBackColor = true; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // radioButton2 // this.radioButton2.AutoSize = true; this.radioButton2.Location = new System.Drawing.Point(10, 157); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(95, 16); this.radioButton2.TabIndex = 2; this.radioButton2.Text = "獲取員工信息"; this.radioButton2.UseVisualStyleBackColor = true; this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged); // // groupBox2 // this.groupBox2.Controls.Add(this.cbbShowDep); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.cbbShowSex); this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.txtShowName); this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.txtShowCardID); this.groupBox2.Controls.Add(this.label8); this.groupBox2.Enabled = false; this.groupBox2.Location = new System.Drawing.Point(10, 179); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(435, 93); this.groupBox2.TabIndex = 3; this.groupBox2.TabStop = false; this.groupBox2.Text = "員工信息"; // // cbbShowDep // this.cbbShowDep.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbbShowDep.FormattingEnabled = true; this.cbbShowDep.Items.AddRange(new object[] { "C#部門", "ASP.NET部門", "基礎(chǔ)部", "VB部門", "VC部門", "JAVA部門"}); this.cbbShowDep.Location = new System.Drawing.Point(279, 57); this.cbbShowDep.Name = "cbbShowDep"; this.cbbShowDep.Size = new System.Drawing.Size(137, 20); this.cbbShowDep.TabIndex = 7; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(212, 60); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(65, 12); this.label5.TabIndex = 6; this.label5.Text = "所屬部門:"; // // cbbShowSex // this.cbbShowSex.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cbbShowSex.FormattingEnabled = true; this.cbbShowSex.Items.AddRange(new object[] { "男職工", "女職工"}); this.cbbShowSex.Location = new System.Drawing.Point(69, 57); this.cbbShowSex.Name = "cbbShowSex"; this.cbbShowSex.Size = new System.Drawing.Size(137, 20); this.cbbShowSex.TabIndex = 5; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(6, 61); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(65, 12); this.label6.TabIndex = 4; this.label6.Text = "員工性別:"; // // txtShowName // this.txtShowName.Location = new System.Drawing.Point(279, 21); this.txtShowName.Name = "txtShowName"; this.txtShowName.Size = new System.Drawing.Size(137, 21); this.txtShowName.TabIndex = 3; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(212, 26); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(65, 12); this.label7.TabIndex = 2; this.label7.Text = "員工姓名:"; // // txtShowCardID // this.txtShowCardID.Location = new System.Drawing.Point(69, 22); this.txtShowCardID.Name = "txtShowCardID"; this.txtShowCardID.Size = new System.Drawing.Size(137, 21); this.txtShowCardID.TabIndex = 1; // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(6, 26); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(65, 12); this.label8.TabIndex = 0; this.label8.Text = "ID卡編號:"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(457, 276); this.Controls.Add(this.groupBox2); this.Controls.Add(this.radioButton2); this.Controls.Add(this.radioButton1); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "Form1"; this.Text = "使用ID卡識別員工編號"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button1; private System.Windows.Forms.ComboBox cbbdep; private System.Windows.Forms.Label label4; private System.Windows.Forms.ComboBox cbbsex; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtName; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.ComboBox cbbShowDep; private System.Windows.Forms.Label label5; private System.Windows.Forms.ComboBox cbbShowSex; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox txtShowName; private System.Windows.Forms.Label label7; private System.Windows.Forms.TextBox txtShowCardID; private System.Windows.Forms.Label label8; private System.Windows.Forms.TextBox txtIdcard; }
到此這篇關(guān)于C#實現(xiàn)員工ID卡的識別功能的文章就介紹到這了,更多相關(guān)C#識別員工ID卡內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# StreamReader類實現(xiàn)讀取文件的方法
這篇文章主要介紹了C# StreamReader類實現(xiàn)讀取文件的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01C#將PDF文檔轉(zhuǎn)換為Markdown文檔的代碼實現(xiàn)
將PDF文件轉(zhuǎn)換為Markdown格式是一個非常實用的需求,尤其是在需要將內(nèi)容從固定布局的PDF文件中提取出來,并轉(zhuǎn)換為更易于編輯和處理的文本格式時,本文將介紹如何通過C#代碼將PDF文檔轉(zhuǎn)換Markdown(MD)文檔,需要的朋友可以參考下2024-10-10C#可訪問級別Public,private,protected,internal
本文主要介紹了C#可訪問級別Public,private,protected,internal,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2006-08-08