学习阶段的收获

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;
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值