程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#聯合數據庫完成驗證辨認ID卡內容的辦法

C#聯合數據庫完成驗證辨認ID卡內容的辦法

編輯:C#入門知識

C#聯合數據庫完成驗證辨認ID卡內容的辦法。本站提示廣大學習愛好者:(C#聯合數據庫完成驗證辨認ID卡內容的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#聯合數據庫完成驗證辨認ID卡內容的辦法正文


本文所述實例為C#聯合數據庫,來驗證所辨認的ID卡內容,經由過程本實例代碼,用戶可以輕松完成對ID卡會員信息的驗證。該實例代碼可完成讀取數據庫,進而慢慢完成數據庫銜接,數據庫讀取,ID卡讀取,ID卡信息與數據庫內容比對,終究前往成果並告之能否驗證勝利。

詳細功效代碼以下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
namespace IDCard
{
  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;
      }
    }
  }
}

該實例正文完美,便於浏覽,讀者還可以依據本身需求改良代碼,或許添加新的功效以知足本身運用的特性化需求。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved