SqlHelper的编写
static class SqlHelper { private static string strConn = ConfigurationManager.ConnectionStrings["连接字符串的名字"].ConnectionString; public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters) { using(SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); using(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } } public static Object ExecuteScalar(string sql,params SqlParameter[] parameters) { using(SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); using(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } } public static DataTable ExecuteDataTable(string sql,params SqlParameter[] parameters) { using(SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); using(SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet dataset = new DataSet(); adapter.Fill(dataset); return dataset.Tables[0]; } } } }