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;
      }
    }
}

 

posted @ 2015-02-13 00:39  杯酒红尘  阅读(1399)  评论(2编辑  收藏  举报