//初学C#,写了个简单的身份证信息查询的小程序
//以下为部分源代码
//Visual Studio 2005下通过测试
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.OleDb;
usingSystem.Configuration;
namespaceIDCardCheck
...{
publicpartialclassForm1:Form
...{
stringstrIDCardNum;
boolwrongFlag=false;
publicForm1()
...{
InitializeComponent();
}
privatevoidbutton1_Click(objectsender,EventArgse)
...{
wrongFlag=false;
textBoxAddr.Text=null;
textBoxBirth.Text=null;
textBoxSex.Text=null;
textBoxCheckInfor.Text=null;
strIDCardNum=textBoxIDCardNum.Text;
if(string.IsNullOrEmpty(strIDCardNum))
...{
MessageBox.Show("请输入你要查询的身份证号码!");
textBoxIDCardNum.Focus();
}
elseif(strIDCardNum.Length==15)
...{
MessageBox.Show("对不起,该软件暂不支持15位的身份证号码!");
}
elseif(strIDCardNum.Length<18)
...{
MessageBox.Show("请检查你输入的身份证号码是否为18位!");
}
elseif(strIDCardNum.Substring(2,2)=="00"||strIDCardNum.Substring(4,2)=="00")
...{
MessageBox.Show("请输入有效的身份证号码!");
}
else
...{
for(inti=0;i<=16;i++)
...{
if(strIDCardNum[i]<'0'||strIDCardNum[i]>'9')
...{
MessageBox.Show("您输入的身份证号码中含有非法数字或字符,请检查!");
return;
}
}
if((strIDCardNum[17]>='0'&&strIDCardNum[17]<='9')||strIDCardNum[17]=='x'||strIDCardNum[17]=='X')
...{
GetAddr();
GetBirth();
GetSex();
GetCheckInfor();
}
}
}
privatevoidGetAddr()//从身份证号码中取出地址码(第1-第6位)
...{
stringstrAddr=null;
stringstrProvince,strCity,strTown;
strProvince=strIDCardNum.Substring(0,2)+"0000";
strCity=strIDCardNum.Substring(0,4)+"00";
strTown=strIDCardNum.Substring(0,6);
ConnectionStringSettingssettings;
settings=ConfigurationManager.ConnectionStrings["IDCardCheck.Properties.Settings.addrConnectionString"];
OleDbConnectionconn=newOleDbConnection(settings.ConnectionString+";JetOLEDB:databasepassword=icemanstudio2006");
DataSetds=newDataSet();
conn.Open();
stringSQLstr="SELECT*FROMProvinceWHERECode='"+strProvince+"'";
OleDbDataAdapterOleDbDA=newOleDbDataAdapter(SQLstr,conn);
OleDbDA.Fill(ds);
if(ds!=null)
...{
DataRowCollectionRows=ds.Tables[0].Rows;
if(Rows.Count<=0)...{MessageBox.Show("没检索到此记录,请检查你的输入是否有误!");wrongFlag=true;}
if(Rows.Count>0)
...{
strAddr=Rows[0]["Address"].ToString();//取出省
ds.Clear();

SQLstr="SELECT*FROMCityWHERECode='"+strCity+"'";
OleDbDA=newOleDbDataAdapter(SQLstr,conn);
OleDbDA.Fill(ds);
if(ds!=null)
...{
Rows=ds.Tables[0].Rows;
if(Rows.Count<=0)...{MessageBox.Show("没检索到此记录,请检查你的输入是否有误!");wrongFlag=true;}
if(Rows.Count>0)
...{
strAddr+=Rows[0]["Address"].ToString();//取出市
ds.Clear();

SQLstr="SELECT*FROMTownWHERECode='"+strTown+"'";
OleDbDA=newOleDbDataAdapter(SQLstr,conn);
OleDbDA.Fill(ds);
if(ds!=null)
...{
Rows=ds.Tables[0].Rows;
if(Rows.Count<=0)...{MessageBox.Show("没检索到此记录,请检查你的输入是否有误!");wrongFlag=true;}
if(Rows.Count>0)
...{
stringtemp;
temp=Rows[0]["Address"].ToString();
strAddr+=temp.Trim('');//取出省+市+县
ds.Clear();
conn.Close();
textBoxAddr.Text=strAddr;
}
}
}
}
}
}
}
privatevoidGetBirth()//从身份证号码中取出生日期码(第7-第14位)
...{
if(wrongFlag)return;
stringstrYear,strMonth,strDay;
strYear=strIDCardNum.Substring(6,4);
strMonth=strIDCardNum.Substring(10,2);
strDay=strIDCardNum.Substring(12,2);
textBoxBirth.Text=strYear+"年"+strMonth+"月"+strDay+"日";
}
privatevoidGetSex()//从身份证号码中取顺序码(第15-第17位)从而判定性别
...{
if(wrongFlag)return;
stringstrSex;
strSex=strIDCardNum.Substring(16,1);
if(System.Convert.ToInt16(strSex)%2==0)
...{
textBoxSex.Text="女";
}
else
...{
textBoxSex.Text="男";
}
}
privatevoidGetCheckInfor()//从身份证号码中取校验码(第18位)从而判断该身份证号码是否合法
...{
if(wrongFlag)return;
stringstrCheck;
string[]strWi=...{"7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2","1"};
inti,check=0;
stringstrCheckFlag=null;
strCheck=strIDCardNum.Substring(17,1);
if(strCheck=="X")strCheck="x";
for(i=0;i<=16;i++)
...{
check+=System.Convert.ToInt16(strIDCardNum.Substring(i,1))*System.Convert.ToInt16(strWi[i]);
check%=11;
}
switch(check)
...{
case0:strCheckFlag="1";break;
case1:strCheckFlag="0";break;
case2:strCheckFlag="x";break;
case3:strCheckFlag="9";break;
case4:strCheckFlag="8";break;
case5:strCheckFlag="7";break;
case6:strCheckFlag="6";break;
case7:strCheckFlag="5";break;
case8:strCheckFlag="4";break;
case9:strCheckFlag="3";break;
case10:strCheckFlag="2";break;
}
if(strCheck==strCheckFlag)
...{
textBoxCheckInfor.Text="校验码正确!应该是合法身份证。";
}
else
...{
textBoxCheckInfor.Text="校验码错误!可能是非法身份证。";
}
}
}
}
//app.config
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<addname="IDCardCheck.Properties.Settings.addrConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=|DataDirectory|addr.mdb"
providerName="System.Data.OleDb"/>
</connectionStrings>
</configuration>
本文介绍了一个使用C#编写的简单身份证信息查询程序。该程序可以解析并验证18位身份证号码,提取地址、出生日期和性别信息,并检查校验码的有效性。
2081

被折叠的 条评论
为什么被折叠?



