using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace ConsoleApp2 { public static class SqlHelper { private static readonly string constr = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString; public static int ExecuteNonQuery(string sqlStr, CommandType commandType, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sqlStr, con)) { cmd.CommandType = commandType; if (pms != null) cmd.Parameters.AddRange(pms); con.Open(); object a= cmd.ExecuteNonQuery(); return 0; } } } public static object ExecuteScalar(string sqlStr, CommandType commandType, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sqlStr, con)) { cmd.CommandType = commandType; if (pms != null) { cmd.Parameters.Clear(); cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteScalar(); } } } public static SqlDataReader ExecuteReader(string sqlStr, CommandType commandType, params SqlParameter[] pms) { SqlConnection con = new SqlConnection(constr); using (SqlCommand cmd = new SqlCommand(sqlStr, con)) { cmd.CommandType = commandType; if (pms != null) { cmd.Parameters.Clear(); cmd.Parameters.AddRange(pms); } try { if (con.State == ConnectionState.Closed) con.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch (Exception) { if (con.State == ConnectionState.Open) { con.Close(); con.Dispose(); } throw; } } } public static DataTable ExecuteTable(string sqlStr, CommandType commandType, params SqlParameter[] pms) { DataTable table = new DataTable(); using (SqlConnection con = new SqlConnection(constr)) { using (SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, con)) { adapt.SelectCommand.CommandType = commandType; if (pms != null) { adapt.SelectCommand.Parameters.Clear(); adapt.SelectCommand.Parameters.AddRange(pms); } con.Open(); adapt.Fill(table); } } return table; } } }
private static void Insert() { List<string> list = new List<string>(); List<SqlParameter> pms = new List<SqlParameter>(); for (int i = 101; i < 1101; i++) { string name = "name" + i; string pwd = "pwd" + i; string str = $"('@{name}','@{pwd}')"; pms.Add(new SqlParameter($"@{name}",$"@{pwd}")); list.Add(str); } string sqlStr = $"insert into LoginData values {string.Join(",", list)}"; int ii = SqlHelper.ExecuteNonQuery(sqlStr, System.Data.CommandType.Text,pms.ToArray()); Console.WriteLine(ii); } private void Select() { Console.WriteLine("name:"); string name = Console.ReadLine(); Console.WriteLine("密码:"); string pwd = Console.ReadLine(); string sqlStr = "select count(0) from LoginData where name=@name and password=@pwd"; List<SqlParameter> pms = new List<SqlParameter>() { new SqlParameter("@name",name), new SqlParameter("@pwd",pwd) }; object num = SqlHelper.ExecuteScalar(sqlStr, CommandType.Text, pms.ToArray()); Console.WriteLine(num); }