下面我們直接用可變長度參數的方式寫一個完整的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;
}
}
}