SELECT INTO和INSERT INTO SELECT的區別 相似aaa?a=1&b=2&c=3&d=4,如何將問號之後的數據變爲鍵值對 C# 獲取必定區間的隨即數 0、1兩個值除隨機數之外的取

SELECT INTO和INSERT INTO SELECT的區別

 

數據庫中的數據複製備份html

SELECT INTO:web

形式:sql

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

 

Table_2表存在,報錯:數據庫中已存在名爲 'Table_2' 的對象。數據庫

Table_2表不存在,自動建立表Table_2,成功導入數據dom

INSERT INTO SELECT:ide

形式:post

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

 

Table_2表不存在,報錯:對象名 'Table_2' 無效。學習

Table_2表存在:成功導入數據ui

 

 

相似aaa?a=1&b=2&c=3&d=4,如何將問號之後的數據變爲鍵值對

 
[csharp]  view plain copy print ?
  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的內部實現(字符串截取):加密

 

[csharp]  view plain copy print ?
  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  

[csharp]  view plain copy print ?
  1. Random random = new Random();  
  2. int j = random.Next(0, 9);  

 

0、1兩個值被取值的機率相等

 

[csharp]  view plain copy print ?
  1. int a = Math.Abs(Guid.NewGuid().GetHashCode()) % 2;  
  2.                 if (a == 0)  
  3.                  {}  
  4.                 else if(a==1)  
  5.                  {}  
[csharp]  view plain copy print ?
  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();//回滾我的筆記

相關文章
相關標籤/搜索