身份证号码15位转18位(新改)——c#篇

using System;
using System.Diagnostics;

namespace tm
{
    ///
    /// Class1 的摘要说明。
    ///
    public class IDCode
    {
        public DateTime Birthday;
        public string Sex=null;
        public string IDCode18;
        public string 地区="";
        public string 地区代码="";

        public IDCode(string code)
        {
            Birthday=DateTime.Today;

            try
            {
                if(code.Length==15)
                {
                    string ts="";
                    if(Convert.ToInt32(code.Substring(6,2))>20)
                        ts="19";
                    else
                        ts="20";
                    IDCode18=code.Substring(0,6)+ts+code.Substring(6);

                    int[] wi={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
                    string[] wf={"1","0","X","9","8","7","6","5","4","3","2"};
                    int s=0;
                    for(int i=0;i<17;i++)
                    {
                        s+=wi[i] * Convert.ToInt32(IDCode18.Substring(i,1));
                    }
                    IDCode18+=wf[Convert.ToInt32(s)%11];
        
                    Debug.Write(IDCode18);
                }
                else
                {
                    IDCode18=code;
                }

                Sex= Convert.ToInt32(IDCode18.Substring(16,1))%2==1?"男":"女";
                Birthday=Convert.ToDateTime(IDCode18.Substring(6,4)+"-"+IDCode18.Substring(10,2)+"-"+IDCode18.Substring(12,2));
                地区代码=IDCode18.Substring(0,6);

                try
                {
                    tmDataReader dr=new tmDataReader("select 名称 from TABLE_行政区划代码 where 编码='"+IDCode18.Substring(0,2)+"'");
                    if(dr.Read())
                        this.地区=(string)dr[0];
                    dr.Close();
                    dr=new tmDataReader("select 名称 from TABLE_行政区划代码 where 编码='"+IDCode18.Substring(0,4)+"'");
                    if(dr.Read())
                        this.地区+=(string)dr[0];
                    dr.Close();
                    dr=new tmDataReader("select 名称 from TABLE_行政区划代码 where 编码='"+IDCode18.Substring(0,6)+"'");
                    if(dr.Read())
                        this.地区+=(string)dr[0];
                    dr.Close();
                }
                catch(Exception ex)
                {
                    Debug.WriteLine("连接数据库取地区代码时出错!");
                }
            }
            catch(Exception ex)
            {
                Debug.Write(ex.Message);

                IDCode18=code;
            }
        }
    }

}


文章来自: 网络
引用通告: 查看所有引用 | 我要引用此文章
Tags: 身份证 C# ASP.NET
相关日志:

评论: 1 | 引用: 0 | 查看次数: 10831
回复回复vinin[2008-06-06 11:11 AM | del]
过.....
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启