c#中SqlHelper类的编写(三)

下面我们直接用可变长度参数的方式写一个完整的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;
      }
    }
}

 

你可能感兴趣的:(help)