1.在C#中怎么判断是否读到数据库的末尾了
SqlDataReader reader=helper.ExecuteQuery(sql)
while(reader.Read())
{
}
2.截取字符串中的值
substring(startindex,endindex)
string str="12345abc789"
string str1=str.substring(0,3) 注:str1="123"
string str2=str.substring(str.length-1,1) 注 str2='9'
string str3=str.Remove(0,3) 注:str3=‘45abc789’
PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度
PadRight(int totalWidth, char paddingChar) //在字符串右边用 paddingChar 补足 totalWidth 长度
string str="123456789"
string str1=str.PadLeft(12,'0') 注 str1=”000123456789“
string str2=str.PadRight(12,'0') 注 str2='123456789000'
substring和 PadLeft可以连接的使用更方便
string str="513029199601195252"
string strProvice=(str.substring(0,2).PadRight(8,'0')) s] 注 strProvice=51000000
3.读取数据里的字段,通过身份证把标示省市县数据截取出来
namespace User
{
class Program
{
static void Main(string[] args)
{
SQLHelper helper = new SQLHelper();
string sql = "select *from AllUser where IDNo!='' and IDRz=1";
SqlDataReader reader = helper.ExecuteQuery(sql);
int j=0;
while (reader.Read())
{
if ((reader["IDNo"].ToString()) != "")
{
string userId = reader["userId"].ToString();
string idnumber = reader["IDNo"].ToString();
string ShowIdno = DecryptIDNo.ADecryptIDNo(idnumber).Trim();
string ProvinceCode = (ShowIdno.Substring(0, 2).PadRight(8, '0').ToString());
string CityCode = (ShowIdno.Substring(0, 4).PadRight(8, '0').ToString());
string CountyCode = (ShowIdno.Substring(0, 6).PadRight(8, '0').ToString());
string sqla = "update AllUser set ProvinceCode='" + ProvinceCode + "',CityCode='" + CityCode + "',CountyCode='" + CountyCode + "',ShowIdno='" + ShowIdno + "' where userId='" + userId + "'";
int i = helper.ExecuteSql(sqla);
}
j++;
}
Console.WriteLine(j);
Console.Read();
}
}
desc解密传入过来身份证字段 秘钥用Md5加密并只对前八位加密
namespace idjm
{
public class DecryptIDNo
{
public static string ADecryptIDNo(string idno)
{
return DESDecrypt(idno, "sfzno");
}
public static string DESDecrypt(string Text, string sKey)
{
try
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(MD5(sKey).Substring(0, 8));
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
}
byte[] bytes = ms.ToArray();
ms.Close();
inputByteArray = null;
string str = Encoding.Default.GetString(bytes);
bytes = null;
return str;
}
}
}
catch
{
return "";
}
}
public static string MD5(string str)
{
byte[] b = Encoding.Default.GetBytes(str);
byte[] a = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = BitConverter.ToString(a).Replace("-","").ToUpper();
return ret;
}
}
}