纯自用,为了拯救懒癌晚期(*´﹃`*)
别说跨平台了,跨程序我都懒得再写一次_(:з」∠)_
发现一个bug,在程序运行足够快的情况下,GetRandomString方法会生成大量相同的数据
(现在已经添加了GetRandomSeed方法解决)
1.0版 初步发布
1.1版 添加生成随机种子,并修改方法命名规则
1.2版 新增方法,可直接填充一张表(DataSet类)
1.3版 使用using关键字
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; using System.Data; using System.Data.SqlClient; namespace Nekopara { public class Neko { private static string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];//从配置文件读取连接字符串 public static int Action(string Command)//增,删,改 { using (SqlConnection sql = new SqlConnection(ConnectionString)) { SqlCommand cmd = new SqlCommand(Command, sql); sql.Open(); int output = cmd.ExecuteNonQuery(); return output; } } public static object Read(string Command)//查(首行首列) { using (SqlConnection sql = new SqlConnection(ConnectionString)) { SqlCommand cmd = new SqlCommand(Command, sql); sql.Open(); object output = cmd.ExecuteScalar(); return output; } } public static DataSet ReadDataSet(string Command)//查(返回表) { using (SqlConnection sql = new SqlConnection(ConnectionString)) { DataSet output = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(Command, sql); sql.Open(); da.Fill(output); return output; } } public static string toSHA512(string strData)//SHA512加密 { byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(strData); SHA512 sha512 = new SHA512CryptoServiceProvider(); byte[] retVal = sha512.ComputeHash(bytValue); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } //取指定位数随机字符串,用于向表中添加大量随机记录 public static string GetRandomString(int n) { string s = "0123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ!@#$%^&*()_+"; string value = ""; Random rdm = new Random(GetRandomSeed()); while (value.Length < n) { string s1 = s[rdm.Next(0, s.Length)].ToString(); value += s1; } return value; } //一个骰子 public static int GetRandomSeed() { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider x = new System.Security.Cryptography.RNGCryptoServiceProvider(); x.GetBytes(bytes); return BitConverter.ToInt32(bytes,0); } } }
使用例子:
//增 int result = Neko.Action("insert into demo(name,passwd,id)values('root','admin','1001'"); //删 int result = Neko.Action("delete from demo where name='root'"); //改 int result = Neko.Action("update demo set passwd='123456' where name='root'"); //查 DataSet ds = Neko.ReadDataSet("select * from demo where name='root'") //加密用户密码 string result = Neko.toSHA512("password"); //取随机字符串 string str = Neko.GetRandomString(8); //掷骰子 int seed = Neko.GetRandomSeed();
请不要吐槽类名蟹蟹(灬ºωº灬)