The action that the program accesses the database requires to create a connection to a specified database.
Then open the created connection, create the execution instruction (i.e. the SQL execution code),
Finally executes the instruction, closes the created connection, frees the resource.
ADO is a set of object-oriented class libraries that are used to interact with the data source through the provided classes
We can do a good job of working with the database.
In order to facilitate access to the database, we can write a number of library access to the auxiliary class, this helper class will be
We often package the database for additions and deletions and other operations.
SqlHelper Auxiliary class:
1 //Database access helper Classes2 Public Static classSqlHelper3 {4 //Database connection String5 Private Static stringConnStr = configurationmanager.connectionstrings["ConnStr"]. ConnectionString; 6 7 //increase, delete, change8 Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] Parameters)9 {Ten using(SqlConnection conn =NewSqlConnection (connstr)) One { A Conn. Open (); - using(SqlCommand cmd =Conn. CreateCommand ()) - { theCmd.commandtext =SQL; - cmd. Parameters.addrange (Parameters); - returncmd. ExecuteNonQuery (); - } + } - } + A //Enquiry at Public StaticDataTable executedatatable (stringSqlparamssqlparameter[] Parameters) - { - using(SqlConnection conn =NewSqlConnection (connstr)) - { - Conn. Open (); - using(SqlCommand cmd =Conn. CreateCommand ()) in { -Cmd.commandtext =SQL; to cmd. Parameters.addrange (Parameters); + -DataSet DataSet =NewDataSet ();//Data Set theSqlDataAdapter adapter =NewSqlDataAdapter (cmd); * adapter. Fill (DataSet); $ returndataset.tables[0];Panax Notoginseng } - the } + } A the //returns the contents of the first row column + Public Static ObjectExecuteScalar (stringSqlparamssqlparameter[] Parameters) - { $ using(SqlConnection conn =NewSqlConnection (connstr)) $ { - Conn. Open (); - using(SqlCommand cmd=Conn. CreateCommand ()) the { -Cmd.commandtext =SQL;Wuyi cmd. Parameters.addrange (Parameters); the returncmd. ExecuteScalar (); - } Wu } -}
Attached: Connection Database configuration file
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<connectionStrings>
<add name= "dbconnstr" connectionstring= "Data source=.; Initial Catalog=mydb; User Id=sa; password=123456 "/>
</connectionStrings>
</configuration>
The file name seems to specify that you want app. config, and then use that connection string in your program
1: References
Solution--reference--right-click to add--. net--system.configuration
You can then use the ConfigurationManager class in System.Configuration.
2: Use
String constr = configurationmanager.connectionstrings["Dbconnstr"]. ConnectionString;
ConfigurationManager needs to parse to get the namespace. Because there may be multiple connection strings, use connectionstrings because it has multiple properties, all with ConnectionString
The value of Constr is "Data source=.; Initial Catalog=mydb; User Id=sa; password=123456 "
Database Access Helper Class SqlHelper