App. Config file
1<?xml version="1.0"encoding="Utf-8"?>2<configuration>3<startup>4<supportedruntime version="v4.0"sku=". netframework,version=v4.5"/>5</startup>6<connectionStrings>7<add connectionstring="Data Source =.; Initial Catalog = mysql; Integrated Security = True;"Name="Constr"/>8</connectionStrings>9</configuration>
SqlHelper Encapsulation Class Library code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 //Create a new App.confit file that references the following two namespaces7 usingSystem.Configuration;8 usingSystem.Data.SqlClient;9 Ten namespacePackage One { A Public classSqlHelper - { - //reading the connection string the Private Static ReadOnly stringstr = configurationmanager.connectionstrings["Constr"]. ConnectionString; - //three methods, the key is the third method, query, conditional judgment more - /// <summary> - ///This method can do additions and deletions to change + /// </summary> - /// <param name= "SQL" >SQL statements</param> + /// <param name= "PS" >parameters in the SQL statement</param> A /// <returns>returns the number of rows affected, int type</returns> at Private Static intExecuteNonQuery (stringSqlparamssqlparameter[] PS) - { - using(SqlConnection con =NewSqlConnection (str)) - { - using(SqlCommand cmd =NewSqlCommand (sql, con)) - { in con. Open (); - if(PS! =NULL) to { + cmd. Parameters.addrange (PS); - } the * returncmd. ExecuteNonQuery (); $ }Panax Notoginseng } - } the /// <summary> + ///This method is used on the query A /// </summary> the /// <param name= "SQL" >SQL statements</param> + /// <param name= "PS" >parameters in the SQL statement</param> - /// <returns>returns the first row of columns, object type</returns> $ Private Static ObjectExecuteScalar (stringSqlparamssqlparameter[] PS) $ { - using(SqlConnection con =NewSqlConnection (str)) - { the using(SqlCommand cmd =NewSqlCommand (sql, con)) - {Wuyi con. Open (); the if(PS! =NULL) - { Wu cmd. Parameters.addrange (PS); - } About returncmd. ExecuteScalar (); $ } - } - } - /// <summary> A ///This method is used for querying, reading data + /// </summary> the /// <param name= "SQL" >SQL statements</param> - /// <param name= "PS" >parameters in the SQL statement</param> $ /// <returns>returns the SqlDataReader object with data</returns> the Private StaticSqlDataReader ExecuteReader (stringSqlparamssqlparameter[] PS) the { the //This place doesn't have a using, because I don't know when to release theSqlConnection con =NewSqlConnection (str); - using(SqlCommand cmd =NewSqlCommand (Sql,con)) in { the if(ps!=NULL) the { About cmd. Parameters.addrange (PS); the } the Try the { + con. Open (); - //This overload will automatically close when DataReader is closed connection the returncmd. ExecuteReader (System.Data.CommandBehavior.CloseConnection);Bayi } the Catch(Exception ex) the { - con. Close (); - con. Dispose (); the Throwex; the } the } the } - } the}
SQL additions and deletions to seal installation