基本信息
源码名称:根据身份证号码查询籍贯、性别、出生日期、星座、生肖、地区等信息
源码大小:11.36M
文件格式:.zip
开发语言:C#
更新时间:2019-09-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍

基于VS15 的 C# .NET Framework 4.5 用于查询身份证的籍贯、签发机关等基本信息,输入不正确时会提示出错误原因





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BMSIdCard
{
    public partial class Form1copy : Form
    {

        IDS[] id = new IDS[4400];//

        List<CardArea> objList = new List<CardArea>();
        private string yob;      //出生年份   算取生肖
        private string born;     //出生月份   与出生日相结合算取星座
        private string day;      //出生日期

        public Form1copy()
        {
            //Substring 截取字符串
            InitializeComponent();

            //不可编辑的项    此处为根据输入的身份证号获取,计算的结果
            this.txtsex.Enabled = false;
            this.txtbirthyear.Enabled = false;
            this.txtbirthmonth.Enabled = false;
            this.txtbirthdate.Enabled = false;
            this.txtarea.Enabled = false;
            this.txtvalidatecode.Enabled = false;
            this.txtconstellation.Enabled = false;
            this.txtzodiac.Enabled = false;
            this.txtage.Enabled = false;
            this.txtplace.Enabled = false;
            this.txtauthority.Enabled = false;

            //文本字体颜色
            txtidcard.ForeColor = txtage.ForeColor = txtarea.ForeColor = txtauthority.ForeColor = txtbirthdate.ForeColor 
                = txtbirthmonth.ForeColor = txtbirthyear.ForeColor = txtconstellation.ForeColor = txtzodiac.ForeColor = 
                txtplace.ForeColor = txtvalidatecode.ForeColor = txtsex.ForeColor = System.Drawing.Color.Black;

        }
        
        private void Form1copy_Load(object sender, EventArgs e)
        {
            Helper helper = new Helper();
            helper.Show();
            MessageBox.Show("请查看弹出的帮助提示窗体");

            //IO 读取csv文件(签发机关、地区编码、籍贯)
            //StreamReader sr = new StreamReader(Application.StartupPath "\\inif.csv", Encoding.Default);
            StreamReader sr = new StreamReader("D:\\NHCJ\\BMSIdCard\\BMSIdCard\\inif.csv", Encoding.Default);

            string str = "";
            string a, b, c, d, e2;
            string[] str_temp;
            int index = 0;
            while (!sr.EndOfStream)
            {
                str = sr.ReadLine().Trim();
                str_temp = str.Split(',');
                a = str_temp[0].Trim();
                b = str_temp[1].Trim();
                c = str_temp[2].Trim();
                d = str_temp[3].Trim();
                e2 = str_temp[4].Trim();
                id[index ] = new IDS(a, b, c, d, e2);
               
            }
            sr.Close();
        }

        public struct IDS
        {
            public string dmmc;
            public string dmzm;
            public string dmbz;
            public string dmxh;
            public string dmmcl;

            public IDS(string a, string b, string c, string d, string e)
            {
                this.dmmc = a;        //签发机关  "公安局"
                this.dmzm = b;        //所在地编码
                this.dmbz = c;        //签发机关简拼  比如永城市 hnyc
                this.dmxh = d;        //编号
                this.dmmcl = e;       //籍贯
            }
        }

        /// <summary>
        /// 验证身份证号码
        /// 获取生日、籍贯、签发机关、生肖、星座、地区编码等
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_verify_Click(object sender, EventArgs e)
        {
            if (!ValidateInput(this.txtidcard.Text))
            {
                return;
            }

            else
            {
                string idcard = this.txtidcard.Text.Trim();
                //string sexCode = string.Empty, areaCode = string.Empty, birthdayCode = string.Empty;
                //areaCode = idcard.Substring(0, 6);
                //birthdayCode = idcard.Substring(6, 8);
                //sexCode = idcard.Substring(16, 1);

                //展示信息
                this.txtsex.Text = (int.Parse(idcard.Substring(16, 1)) % 2 == 0) ? "女" : "男";

                //this.txtbirthyear.Text = idcard.Substring(6, 4) "-" idcard.Substring(10, 2) "-" idcard.Substring(12, 2);

                this.txtbirthyear.Text = idcard.Substring(6, 4);
                this.txtbirthmonth.Text = idcard.Substring(10, 2);
                this.txtbirthdate.Text = idcard.Substring(12, 2);

                #region 月份与日期核对验证

                if (int.Parse(idcard.Substring(10, 2)) > 12)
                {
                    txtidcard.ForeColor = System.Drawing.Color.Red;
                    txtbirthmonth.ForeColor = System.Drawing.Color.Red;
                    MessageBox.Show("出生月份错误,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                else
                {
                    txtidcard.ForeColor = System.Drawing.Color.Black;
                    txtbirthmonth.ForeColor = System.Drawing.Color.Black;
                }


                if (int.Parse(idcard.Substring(6, 4)) % 4 == 0 && int.Parse(idcard.Substring(6, 4)) % 100 != 0 || int.Parse(idcard.Substring(6, 4)) % 400 == 0)
                {

                    if (int.Parse(idcard.Substring(10, 2)) == 02 && int.Parse(idcard.Substring(12, 2)) > 29)
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Red;
                        txtbirthdate.ForeColor = System.Drawing.Color.Red;
                        MessageBox.Show("闰年02月出生日不可大于29天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Black;
                        txtbirthdate.ForeColor = System.Drawing.Color.Black;
                    }


                }
                else
                {
                    if (int.Parse(idcard.Substring(10, 2)) == 02 && int.Parse(idcard.Substring(12, 2)) > 28)
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Red;
                        txtbirthdate.ForeColor = System.Drawing.Color.Red;
                        MessageBox.Show("平年02月出生日不可大于28天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Black;
                        txtbirthdate.ForeColor = System.Drawing.Color.Black;
                    }
                }

                if (int.Parse(idcard.Substring(10, 2)) == 04 || int.Parse(idcard.Substring(10, 2)) == 06 || int.Parse(idcard.Substring(10, 2)) == 09 || int.Parse(idcard.Substring(10, 2)) == 11)
                {

                    if (int.Parse(idcard.Substring(12, 2)) > 30)
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Red;
                        txtbirthdate.ForeColor = System.Drawing.Color.Red;
                        MessageBox.Show("每年04、06、09、11月份出生日不可大于30天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Black;
                        txtbirthdate.ForeColor = System.Drawing.Color.Black;
                    }

                }
                else
                {
                    if (int.Parse(idcard.Substring(12, 2)) > 31)
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Red;
                        txtbirthdate.ForeColor = System.Drawing.Color.Red;
                        MessageBox.Show("每年01、03、05、07、08、10、12月份出生日不可大于31天,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        txtidcard.ForeColor = System.Drawing.Color.Black;
                        txtbirthdate.ForeColor = System.Drawing.Color.Black;
                    }
                }
                #endregion

                this.txtzodiac.Text = zodiac(yob);                         //生肖
                this.txtconstellation.Text = constellation(born, day);     //星座

                //this.txtarea.Text = GetAreaName(idcard.Substring(0, 6));

                this.txtarea.Text = idcard.Substring(0, 6);                //地区编码

                ///簽發機關,籍貫
                string keys = txtidcard.Text;
                string dmzm = keys.Substring(0, 6);
               
                bool flag = false;
                foreach (IDS i in id)
                {
                    if (i.dmzm.Equals(dmzm))
                    {
                        flag = true;
                        this.txtauthority.Text = i.dmmc "公安局";  //签发机关

                        this.txtplace.Text = i.dmmcl;                //籍贯

                        break;
                    }//if 
                }

                //算取年龄
                string sBirthday = idcard.Substring(6, 8);//获取取生日
                DateTime dtBirthday = DateTime.Parse(sBirthday.Substring(0, 4) "-" sBirthday.Substring(4, 2) "-" sBirthday.Substring(6, 2));

                //TimeSpan sp = DateTime.Now.Subtract(dtBirthday);
                //this.txtage.Text = (sp.TotalDays / 365).ToString();                 //算取详细年龄 保留所有小数

                TimeSpan ts = DateTime.Now.Subtract(Convert.ToDateTime(dtBirthday));
                this.txtage.Text = Math.Floor(ts.TotalDays / 365).ToString();        //算取年龄   向小取整


                this.txtvalidatecode.Text = CalculateValidateCode(idcard);
                if (CalculateValidateCode(idcard) != idcard.Substring(17, 1))
                {
                    txtidcard.ForeColor = System.Drawing.Color.Red;
                    txtvalidatecode.ForeColor = System.Drawing.Color.Red;
                    MessageBox.Show("验证码错误,身份证号码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    txtidcard.ForeColor = System.Drawing.Color.Black;
                    txtvalidatecode.ForeColor = System.Drawing.Color.Black;
                }
            }
        }
        
        /// <summary>
        /// 输入身份证号验证 身份证号必须是18位二代身份证
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        private bool ValidateInput(string inputString)
        {
            if (string.IsNullOrWhiteSpace(inputString))
            {
                MessageBox.Show("输入的身份证号码不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtidcard.Focus();
                return false;
            }
            if (inputString.Trim().Length != 18)
            {
                txtidcard.ForeColor = System.Drawing.Color.Red;
                MessageBox.Show("输入的身份证号码必须是18位,已用红色标注,请修改!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtidcard.Focus();
                this.txtidcard.SelectAll();
                return false;
            }
            
            else
            {
                txtidcard.ForeColor = System.Drawing.Color.Black;
                return true;
            }
        }

        /// <summary>
        /// 身份证校验码验证
        /// </summary>
        /// <param name="inputstring"></param>
        /// <returns></returns>
        private string CalculateValidateCode(string inputstring)
        {
            byte[] Validate = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
            byte[] Idcard = new byte[17];
            int[] multiplication = new int[17];
            for (int i = 0; i < inputstring.Trim().Length-1; i )
            {
                Idcard[i] = byte.Parse(inputstring[i].ToString());
                multiplication[i] = (byte)(Validate[i] * Idcard[i]);
            }
            switch (multiplication.Sum() % 11)
            {
                case 0:
                    return "1";
                case 1:
                    return "0";
                case 2:
                    return "X";
                case 3:
                    return "9";
                case 4:
                    return "8";
                case 5:
                    return "7";
                case 6:
                    return "6";
                case 7:
                    return "5";
                case 8:
                    return "4";
                case 9:
                    return "3";
                case 10:
                    return "2";
                default:
                    return null;
                    //break;
            }
            
        }

        
        /// <summary>
        /// 所属生肖
        /// </summary>
        /// <param name="y"></param>
        /// <returns></returns>
        public string zodiac(string yob)
        {

            yob = this.txtbirthyear.Text;
            uint temp_y = uint.Parse(yob);
            temp_y = (temp_y - 1900) % 12;
            switch (temp_y)
            {
                case 0:
                    return "鼠";
                    break;
                case 1:
                    return "牛";
                    break;
                case 2:
                    return "虎";
                    break;
                case 3:
                    return "兔";
                    break;
                case 4:
                    return "龙";
                    break;
                case 5:
                    return "蛇";
                    break;
                case 6:
                    return "马";
                    break;
                case 7:
                    return "羊";
                    break;
                case 8:
                    return "猴";
                    break;
                case 9:
                    return "鸡";
                    break;
                case 10:
                    return "狗";
                    break;
                case 11:
                    return "猪";
                    break;
                default:
                    return "not found";
                    break;
            }
            //鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
        }

        /// <summary>
        /// 星座
        /// </summary>
        /// <param name="born"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public string constellation(string born, string day)
        {
            born = this.txtbirthmonth.Text;
            day = this.txtbirthdate.Text;
            
            if (born == "01")
            {
                if (int.Parse(day) <= 19)
                    return "水瓶座";
                else
                    return "水瓶座";
            }

            else if (born == "02")
            {
                if (int.Parse(day) <= 19)
                    return "水瓶座";
                else
                    return "双鱼座";
            }
            else if (born == "03")
            {
                if (int.Parse(day) <= 20)
                    return "双鱼座";
                else
                    return "白羊座";
            }
            else if (born == "04")
            {
                if (int.Parse(day) <= 20)
                    return "白羊座";
                else
                    return "金牛座";
            }
            else if (born == "05")
            {
                if (int.Parse(day) <= 20)
                    return "金牛座";
                else
                    return "双子座";
            }
            else if (born == "06")
            {
                if (int.Parse(day) <= 21)
                    return "双子座";
                else
                    return "巨蟹座";
            }
            else if (born == "07")
            {
                if (int.Parse(day) <= 22)
                    return "巨蟹座";
                else
                    return "狮子座";
            }
            else if (born == "08")
            {
                if (int.Parse(day) <= 22)
                    return "狮子座";
                else
                    return "处女座";
            }
            else if (born == "09")
            {
                if (int.Parse(day) <= 22)
                    return "处女座";
                else
                    return "天平座";
            }
            else if (born == "10")
            {
                if (int.Parse(day) <= 22)
                    return "天平座";
                else
                    return "天蝎座";
            }
            else if (born == "11")
            {
                if (int.Parse(day) <= 21)
                    return "天蝎座";
                else
                    return "射手座";
            }
            else
            {
                if (int.Parse(day) <= 21)
                    return "射手座";
                else
                    return "魔羯座";
            }
        }
        
    }
}