C # use the data connection pool for the general dbhelper class

Source: Internet
Author: User

In web. config

<connectionStrings><add name="dh_web" connectionString="Data Source=xxx.com;Initial Catalog=xx_db;User ID=xx;Password=**;            pooling=true;max pool size=200" providerName="System.Data.SqlClient"/></connectionStrings>

Public class dbhelper {Private Static string connectionstring = configurationmanager. connectionstrings ["dh_web"]. connectionstring; // The public static int executecommand (string safesql) {using (sqlconnection connection = new sqlconnection (connectionstring) {connection. open (); sqlcommand cmd = new sqlcommand (safesql, connection); Return cmd. executenonquery () ;}}// public static int executecommand (string SQL, Params sqlparameter [] values) {using (sqlconnection connection = new sqlconnection (connectionstring )) {connection. open (); sqlcommand cmd = new sqlcommand (SQL, connection); cmd. parameters. addrange (values); Return cmd. executenonquery () ;}} public static int getscalar (string safesql) {using (sqlconnection connection = new sqlconnection (connectionstring) {connection. open (); sqlcommand cmd = new sqlcommand (safesql, connection); Return convert. toint32 (CMD. executescalar () ;}} public static int getscalar (string SQL, Params sqlparameter [] values) {using (sqlconnection connection = new sqlconnection (connectionstring) {connection. open (); sqlcommand cmd = new sqlcommand (SQL, connection); cmd. parameters. addrange (values); Return convert. toint32 (CMD. executescalar () ;}} public static sqldatareader getreader (string safesql) {sqlconnection connection = new sqlconnection (connectionstring); connection. open (); sqlcommand cmd = new sqlcommand (safesql, connection); Return cmd. executereader (commandbehavior. closeconnection);} public static sqldatareader getreader (string SQL, Params sqlparameter [] values) {sqlconnection connection = new sqlconnection (connectionstring); connection. open (); sqlcommand cmd = new sqlcommand (SQL, connection); cmd. parameters. addrange (values); Return cmd. executereader (commandbehavior. closeconnection);} public static datatable getdataset (string safesql) {using (sqlconnection connection = new sqlconnection (connectionstring) {dataset DS = new dataset (); sqlcommand cmd = new sqlcommand (safesql, connection); sqldataadapter da = new sqldataadapter (CMD); DA. fill (DS); Return Ds. tables [0] ;}} public static datatable getdataset (string SQL, Params sqlparameter [] values) {using (sqlconnection connection = new sqlconnection (connectionstring )) {dataset DS = new dataset (); sqlcommand cmd = new sqlcommand (SQL, connection); cmd. parameters. addrange (values); sqldataadapter da = new sqldataadapter (CMD); DA. fill (DS); Return Ds. tables [0] ;}}

Note:

Commandbehavior. closeconnection solves the problem that the database connection cannot be effectively closed when the stream reads data. commandbehavior is used when a xxxdatareader object is generated. closeconnection, the database connection will be automatically closed when the xxxdatareader object is closed.

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.