zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SELECT INTO和INSERT INTO SELECT的区别 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对 C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等) C# MD5 加密,解密 C#中DataTable删除多条数据

2023-09-11 14:14:39 时间

SELECT INTO和INSERT INTO SELECT的区别

 

数据库中的数据复制备份

SELECT INTO:

形式:

  1. SELECT value1,value2,value3 INTO Table_2 FROM Table_1  

 

Table_2表存在,报错:数据库中已存在名为 'Table_2' 的对象。

Table_2表不存在,自动创建表Table_2,成功导入数据

INSERT INTO SELECT:

形式:

  1. INSERT INTO Table_2 (v1,v2,v3) SELECT v4,v5,v6 FROM Table_1  

 

Table_2表不存在,报错:对象名 'Table_2' 无效。

Table_2表存在:成功导入数据

 

 

类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对

 
  1. string result = "aaa?a=1&b=2&c=3&d=4";  
  2. string[] array = result.Split('?');  
  3. //string a = System.Web.HttpUtility.ParseQueryString(array[1]).Get("a");  
  4. System.Collections.Specialized.NameValueCollection abcd = System.Web.HttpUtility.ParseQueryString(array[1]);  
  5. string a = abcd.Get("a");  
  6. string b = abcd.Get("b");  
  7. string c = abcd.Get("c");  
  8. string d = abcd.Get("d");  

ParseQueryString的内部实现(字符串截取):

 

  1. int num = (s != null) ? s.Length : 0;  
  2. System.Collections.Specialized.NameValueCollection a = new System.Collections.Specialized.NameValueCollection();   
  3.            for (int i = 0; i < num; i++)  
  4.            {  
  5.                int startIndex = i;  
  6.                int num4 = -1;  
  7.                while (i < num)  
  8.                {  
  9.                    char ch = s[i];  
  10.                    if (ch == '=')  
  11.                    {  
  12.                        if (num4 < 0)  
  13.                        {  
  14.                            num4 = i;  
  15.                        }  
  16.                    }  
  17.                    else if (ch == '&')  
  18.                    {  
  19.                        break;  
  20.                    }  
  21.                    i++;  
  22.                }  
  23.                string str = null;  
  24.                string str2 = null;  
  25.                if (num4 >= 0)  
  26.                {  
  27.                    str = s.Substring(startIndex, num4 - startIndex);  
  28.                    str2 = s.Substring(num4 + 1, (i - num4) - 1);  
  29.                }  
  30.                else  
  31.                {  
  32.                    str2 = s.Substring(startIndex, i - startIndex);  
  33.                }  
  34.   
  35.                a.Add(str, str2);  
  36.                 
  37.                if ((i == (num - 1)) && (s[i] == '&'))  
  38.                {  
  39.                    a.Add(null, string.Empty);  
  40.                }  
  41.            }  

点击链接学习:膜拜高手
 

C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)

 

获取随机数 

举例:0-9  

  1. Random random = new Random();  
  2. int j = random.Next(0, 9);  

 

0、1两个值被取值的概率相等

 

  1. int a = Math.Abs(Guid.NewGuid().GetHashCode()) % 2;  
  2.                 if (a == 0)  
  3.                  {}  
  4.                 else if(a==1)  
  5.                  {}  
  1. /// <summary>  
  2.        /// 获取等概率的小于最大数的非负随机数  
  3.        /// </summary>  
  4.        /// <param name="n">最大数</param>  
  5.        /// <returns></returns>  
  6.        public static int Estimate(int n)  
  7.        {  
  8.            return Math.Abs(Guid.NewGuid().GetHashCode()) % n;  
  9.        }  


 

C# MD5 加密,解密

 

 

//生成cs文件

public class MD5Help
{
  ///MD5加密 
  public static string MD5Encrypt(string pToEncrypt, string sKey)
  {
  DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  MemoryStream ms = new MemoryStream();
  CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  cs.Write(inputByteArray, 0, inputByteArray.Length);
  cs.FlushFinalBlock();
  StringBuilder ret = new StringBuilder();
  foreach (byte b in ms.ToArray())  
  {
  ret.AppendFormat("{0:X2}", b);
  }
  ret.ToString();
  return ret.ToString();

  }

  ///MD5解密 
  public static string MD5Decrypt(string pToDecrypt, string sKey)
  {
  DESCryptoServiceProvider des = new DESCryptoServiceProvider();

  byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  for (int x = 0; x < pToDecrypt.Length / 2; x++)
  {
  int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  inputByteArray[x] = (byte)i;
  }

  des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  MemoryStream ms = new MemoryStream();
  CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  cs.Write(inputByteArray, 0, inputByteArray.Length);
  cs.FlushFinalBlock();

  StringBuilder ret = new StringBuilder();

  return System.Text.Encoding.Default.GetString(ms.ToArray());
  }
}

-------------------------------------------------------------------------------------------------

使用:

string IPassword = MD5Help.MD5Encrypt(password, ConfigurationManager.AppSettings["sKey"].ToString()); //加密
string JPassword = MD5Help.MD5Decrypt(Password, ConfigurationManager.AppSettings["sKey"].ToString()); //解密

webConfig配置:

<!--Md5加密key-->
<add key="sKey" value="JUNDAOXT"/>

 

 

C#中DataTable删除多条数据

 

 //一般情况下我们会这么删除

                DataTable dt = new DataTable();

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    if (99 % i == 0)

                    {

                        dt.Rows.RemoveAt(i);

                    }

                }

                //但是这么删除会出现意外情况

                //当运行dt.Rows.RemoveAt(i)代码后DataTable的index会发生改变

                //且他的dt.Rows.Count也会改变

                //正确做法一

                for (int i = dt.Rows.Count - 1; i >= 0; i--)

                {

                    if (99 % i == 0)

                    {

                        dt.Rows.RemoveAt(i);

                    }

                }

                //正确做法二

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    if (99 % i == 0)

                    {

                        dt.Rows[i].Delete();

                    }

                }

                dt.AcceptChanges();//提交

                //dt.RejectChanges();//回滚个人笔记