下面我们直接用可变长度参数的方式写一个完整的SqlHelper
增删改
public static int ExecuteNonQuery(string sql,params Parameter[] parameters)
{
using(SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText=sql;
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
查
public static object ExecuteScalar(string sql,params Parameter[] parameters)
{
using(SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText=sql;
cmd.Parameters.AddRange(parameters);
SqlDataAdapter adpter = new SqlDataAdapter(cmd);
DataSet data = new DataSet();
adapter.Fill(data);
return data;
}
}
}