C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#识别员工ID卡

C#实现员工ID卡的识别功能

作者:芝麻粒儿

这篇文章主要为大家详细介绍了C#如何实现识别员工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,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。  
    HookProc KeyBoardHookProcedure;
    [DllImport("kernel32")]
    public static extern int Beep(int dwFreq, int dwDuration);//让计算机蜂鸣
    string DataPath = "";//数据库路径
    OleDbConnection con;//OleDbConnection对象,连接数据库
    OleDbCommand cmd;//OleDbCommand对象,执行SQL语句
    //键盘Hook结构函数  
    [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")]
    //调用下一个钩子  
    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);
            //如果设置钩子失败.  
            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();//打开数据库连接
                            //实例化OleDbCommand对象,根据ID卡号检索数据表
                            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();//关闭数据库连接
                            Beep(3000, 100);//计算机蜂鸣
                        }
                        NumCode = "";
                    }
                }

            }
        }
        return CallNextHookEx(hHook, nCode, wParam, lParam);
    } 



    private void Form1_Load(object sender, EventArgs e)
    {
        cbbdep.SelectedIndex = 0;//设置部门下拉框的第一项被选中
        cbbsex.SelectedIndex = 0;//设置性别下拉框的第一项被选中
        //获取数据库路径
        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("请将数据填写完整!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
            {
                if (txtIdcard.Text == "")//如果没有输入ID卡号
                    txtIdcard.Focus();//则光标处在输入ID卡号的文本框
                if (txtName.Text == "")//如果没有输入员工姓名
                    txtName.Focus();//则光标处在输入员工姓名的文本框
                if (txtIdcard.Text == "" && txtName.Text == "")//如果都没输入数据
                    txtIdcard.Focus();//则光标处在输入ID卡号的文本框
            }
        }
        else//如果输入了数据
        {
            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());//判断是否已经添加过此ID卡号
                if (flag > 0)//如果大于0则说明已经添加过
                {
                    //弹出警告信息
                    if (MessageBox.Show("ID卡号已经添加过了!", "警告", 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();//执行insert语句,将输入的信息添加到数据库中
                    if (k > 0)//如果大于0则操作成功
                    {
                        //弹出提示信息
                        if (MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                        {
                            button2_Click(sender, e);//清空输入ID卡号和员工姓名的文本框
                        }
                    }
                }
                con.Close();//关闭数据库连接
            }
        }
    }

    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>
    /// 必需的设计器变量。
    /// </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 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </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部门",
        "基础部",
        "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部门",
        "基础部",
        "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;
}

到此这篇关于C#实现员工ID卡的识别功能的文章就介绍到这了,更多相关C#识别员工ID卡内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文