usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Text;namespaceself-encapsulated sqlhelper{ Public Static classSqlHelper {//content of the App. config configuration file//<?xml version= "1.0" encoding= "Utf-8"?>//<configuration>//<connectionStrings>//<add name= "sqlconnstr" connectionstring= "Data source=.; Initial catalog=;integrated security =true; " />//</connectionStrings>//</configuration> ///to get a database link string from a configuration file Private Static ReadOnly stringConstr = configurationmanager.connectionstrings["Sqlconnstr"]. ConnectionString; //private static readonly string constr = "Data source=.; Initial catalog=;integrated security =true; "; /// <summary> ///Insert (insert), delete (delete), update (updated)/// </summary> /// <param name= "SQL" >SQL command Statements</param> /// <param name= "PMS" >the collection of parameters used to execute the command</param> /// <returns></returns> Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] PMS) { using(SqlConnection con =NewSqlConnection (CONSTR)) { using(SqlCommand cmd =NewSqlCommand (sql, con)) { //variable parameter PMS if the user does not pass the time, it is an array of length 0. is not NULL. if(PMS! =NULL) {cmd. Parameters.addrange (PMS); } con. Open (); returncmd. ExecuteNonQuery (); } } } /// <summary> ///returns a single value, the first column of the first row, returns a single value of the/// </summary> /// <param name= "SQL" >SQL command Statements</param> /// <param name= "PMS" >the collection of parameters used to execute the command</param> /// <returns></returns> Public Static ObjectExecuteScalar (stringSqlparamssqlparameter[] PMS) { using(SqlConnection con =NewSqlConnection (CONSTR)) { using(SqlCommand cmd =NewSqlCommand (sql, con)) { //variable parameter PMS if the user does not pass the time, it is an array of length 0. is not NULL. if(PMS! =NULL) {cmd. Parameters.addrange (PMS); } con. Open (); returncmd. ExecuteScalar (); } } } /// <summary> ///Select queries multi-row multiple columns, calling ExecuteReader () to implement. Note: Reader will call Close () to close the object when it returns. /// </summary> /// <param name= "SQL" >SQL command Statements</param> /// <param name= "PMS" >the collection of parameters used to execute the command</param> /// <returns></returns> Public StaticSqlDataReader ExecuteReader (stringSqlparamssqldatareader[] PMS) {SqlConnection con=NewSqlConnection (CONSTR); { using(SqlCommand cmd =NewSqlCommand (sql, con)) { if(PMS! =NULL) {cmd. Parameters.addrange (PMS); } Try{con. Open (); //The parameter System.Data.CommandBehavior.CloseConnection indicates that when the close () method of the DataReader object is called externally, within the close () method, The close () method of the connection associated with the DataReader is automatically called returncmd. ExecuteReader (System.Data.CommandBehavior.CloseConnection); } Catch { ////Close here do not write in Finally, because you need to return a SqlDataReaderCon. Close ();////Out error, close the database link in time and put it in "Pool"con. Dispose (); Throw;////continue to throw an error message above } } } } /// <summary> ///return result set DataTable/// </summary> /// <param name= "SQL" >SQL command Statements</param> /// <param name= "PMS" >the collection of parameters used to execute the command</param> /// <returns></returns> Public StaticDataTable executetable (stringSqlparamssqlparameter[] PMS) {DataTable dt=NewDataTable (); using(SqlDataAdapter adapter =NewSqlDataAdapter (SQL, CONSTR)) {Adapter. Fill (DT); } returnDT; } }}
Self-encapsulated SqlHelper