C # connect to the database by writing SqlHelper
Static class SqlHelper {private static string connStr = ConfigurationManager. connectionStrings ["dbConStr"]. connectionString; // encapsulation method principle: Put unchanged changes in the method into the parameter list public static int ExecuteNonQuery (string SQL, params SqlParameter [] parameters) {using (SqlConnection coon = new SqlConnection (connStr) {coon. open (); using (SqlCommand cmd = coon. createCommand () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); return cmd. executeNonQuery () ;}} public static object ExecuteScalar (string SQL, params SqlParameter [] parameters) {using (SqlConnection coon = new SqlConnection (connStr) {coon. open (); using (SqlCommand cmd = coon. createCommand () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); return cmd. executeScalar () ;}}// you can directly use Table public static DataTable ExecuteDataTable (string SQL, params SqlParameter [] parameters) because the query is small) {using (SqlConnection coon = new SqlConnection (connStr) {coon. open (); using (SqlCommand cmd = coon. createCommand () {cmd. commandText = SQL; cmd. parameters. addRange (parameters); DataSet dataset = new DataSet (); SqlDataAdapter adapter = new SqlDataAdapter (cmd); adapter. fill (dataset); return dataset. tables [0] ;}}