Using System.Data.SqlClient;
Using System.Configuration;
Reference: System.Configuration
The connection string is placed in the configuration file
Create a new class and write the following methods to manipulate the data
//ExecuteNonQuery Construction Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] Parameters) { stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn. Open (); using(SqlCommand cmd =Conn. CreateCommand ()) {Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd. Parameters.Add (PAR); } returncmd. ExecuteNonQuery (); } } }//use of the ExecuteNonQuery methodPrivate voidButton1_Click (Objectsender, EventArgs e)//Click events{ Try{sqlhelper.executenonquery ("INSERT INTO T_age (name,age) VALUES (@n,@a)",NewSqlParameter ("@N", TxtLogin.Text.Trim ()),NewSqlParameter ("@A", TxtAge.Text.Trim ())); MessageBox.Show ("Insert Success! "); } Catch(Exception ex) {MessageBox.Show (string. Format ("Insert failed! : {0}", ex)); return; }}//-----------------------------------------------------------------------------//ExecuteScalar Construction Public Static ObjectExeccutescalar (stringSqlparamssqlparameter[] Parameters) { stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn. Open (); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd. Parameters.Add (PAR); } returncmd. ExecuteScalar (); }}//use of the ExecuteScalar methodPrivate voidButton2_Click (Objectsender, EventArgs e)//Click events{ inti = Convert.ToInt32 (Sqlhelper.execcutescalar ("Select COUNT (*) T_phone")); MessageBox.Show (Convert.ToString (i));//-----------------------------------------------------------------------------//ExecuteReader Construction//You cannot use the using Public StaticSqlDataReader SqlDataReader (stringSqlparamssqlparameter[] Parameters) { stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; SqlConnection Conn=NewSqlConnection (STR); Conn. Open (); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd. Parameters.Add (PAR); } SqlDataReader Dr=cmd. ExecuteReader (commandbehavior.closeconnection); Cmd. Parameters.clear (); returnDr;}//use of the ExecuteReader methodPrivate voidButton4_Click (Objectsender, EventArgs e)//Click events{SqlDataReader Dr= Sqlhelper.sqldatareader_using ("SELECT * from T_age"); while(Dr. Read ()) {MessageBox.Show (convert.tostring (dr["Name"]));//read the value of the Name field } }//-----------------------------------------------------------------------------//the construction of ExecuteDataset Public StaticDataTable ExecuteDataset (stringSqlparamssqlparameter[] sqlparmeter) { stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn. Open (); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchSqlparmeter)//traversing increases the Sqlparmeter parameter{cmd. Parameters.Add (PAR); } DataSet DataSet=NewDataSet (); SqlDataAdapter Adapter=NewSqlDataAdapter (CMD); Adapter. Fill (DataSet); returnDataSet. tables[0]; }}//use of the ExecuteDataset methodPrivate voidButton5_click (Objectsender, EventArgs e) {DataTable dt= SqlHelper.ExecuteDataset ("SELECT * from T_age"); for(inti =0; i < dt. Rows.Count; i++) {DataRow row=dt. Rows[i]; stringName =convert.tostring (row["Name"]); MessageBox.Show (name); }}//-----------------------------------------------------------------------------Note: SqlDataReader If you useusing, it will not be executed because the error code cannot be read when the data is read by using the following Public StaticSqlDataReader executereader_using (stringSqlparamssqlparameter[] Parameters) { stringSTR = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; using(SqlConnection conn =NewSqlConnection (STR)) {Conn. Open (); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtext=SQL; foreach(SqlParameter parinchparameters) {cmd. Parameters.Add (PAR); } returncmd. ExecuteReader (); }} //-----------------------------------------------------------------------------
"ADO" 6, SqlHelper simple Package