SQL中的方法封装成类
public class SqlHelp
{
public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
{
string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
return cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
public static object ExecuteScalar(string sql, params SqlParameter[] pms)
{
string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
return cmd.ExecuteScalar();
conn.Close();
conn.Dispose();
}
public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
{
string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
return cmd.ExecuteReader();
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
{
string conStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql, conStr);
adapter.Fill(table);
return table;
}
}