Database Access Helper Class SqlHelper

Source: Internet
Author: User
Tags connectionstrings

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.