SELECT INTO和INSERT INTO SELECT的区别
数据库中的数据复制备份
SELECT INTO:
形式:
- SELECT value1,value2,value3 INTO Table_2 FROM Table_1
Table_2表存在,报错:数据库中已存在名为 'Table_2' 的对象。
Table_2表不存在,自动创建表Table_2,成功导入数据
INSERT INTO SELECT:
形式:
- 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,如何将问号以后的数据变为键值对
- string result = "aaa?a=1&b=2&c=3&d=4";
- string[] array = result.Split('?');
- //string a = System.Web.HttpUtility.ParseQueryString(array[1]).Get("a");
- System.Collections.Specialized.NameValueCollection abcd = System.Web.HttpUtility.ParseQueryString(array[1]);
- string a = abcd.Get("a");
- string b = abcd.Get("b");
- string c = abcd.Get("c");
- string d = abcd.Get("d");
ParseQueryString的内部实现(字符串截取):
- int num = (s != null) ? s.Length : 0;
- System.Collections.Specialized.NameValueCollection a = new System.Collections.Specialized.NameValueCollection();
- for (int i = 0; i < num; i++)
- {
- int startIndex = i;
- int num4 = -1;
- while (i < num)
- {
- char ch = s[i];
- if (ch == '=')
- {
- if (num4 < 0)
- {
- num4 = i;
- }
- }
- else if (ch == '&')
- {
- break;
- }
- i++;
- }
- string str = null;
- string str2 = null;
- if (num4 >= 0)
- {
- str = s.Substring(startIndex, num4 - startIndex);
- str2 = s.Substring(num4 + 1, (i - num4) - 1);
- }
- else
- {
- str2 = s.Substring(startIndex, i - startIndex);
- }
- a.Add(str, str2);
- if ((i == (num - 1)) && (s[i] == '&'))
- {
- a.Add(null, string.Empty);
- }
- }
点击链接学习:膜拜高手
C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)
获取随机数
举例:0-9
- Random random = new Random();
- int j = random.Next(0, 9);
0、1两个值被取值的概率相等
- int a = Math.Abs(Guid.NewGuid().GetHashCode()) % 2;
- if (a == 0)
- {}
- else if(a==1)
- {}
- /// <summary>
- /// 获取等概率的小于最大数的非负随机数
- /// </summary>
- /// <param name="n">最大数</param>
- /// <returns></returns>
- public static int Estimate(int n)
- {
- return Math.Abs(Guid.NewGuid().GetHashCode()) % n;
- }
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();//回滚个人笔记