Self-encapsulated SqlHelper

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Text;namespaceself-encapsulated sqlhelper{ Public Static classSqlHelper {//content of the App. config configuration file//<?xml version= "1.0" encoding= "Utf-8"?>//<configuration>//<connectionStrings>//<add name= "sqlconnstr" connectionstring= "Data source=.; Initial catalog=;integrated security =true; " />//</connectionStrings>//</configuration>        ///to get a database link string from a configuration file        Private Static ReadOnly stringConstr = configurationmanager.connectionstrings["Sqlconnstr"].        ConnectionString; //private static readonly string constr = "Data source=.; Initial catalog=;integrated security =true; ";        /// <summary>        ///Insert (insert), delete (delete), update (updated)/// </summary>        /// <param name= "SQL" >SQL command Statements</param>        /// <param name= "PMS" >the collection of parameters used to execute the command</param>        /// <returns></returns>         Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] PMS) {            using(SqlConnection con =NewSqlConnection (CONSTR)) {                using(SqlCommand cmd =NewSqlCommand (sql, con)) {                    //variable parameter PMS if the user does not pass the time, it is an array of length 0. is not NULL.                     if(PMS! =NULL) {cmd.                    Parameters.addrange (PMS); } con.                    Open (); returncmd.                ExecuteNonQuery (); }            }        }        /// <summary>        ///returns a single value, the first column of the first row, returns a single value of the/// </summary>        /// <param name= "SQL" >SQL command Statements</param>        /// <param name= "PMS" >the collection of parameters used to execute the command</param>        /// <returns></returns>         Public Static ObjectExecuteScalar (stringSqlparamssqlparameter[] PMS) {            using(SqlConnection con =NewSqlConnection (CONSTR)) {                using(SqlCommand cmd =NewSqlCommand (sql, con)) {                    //variable parameter PMS if the user does not pass the time, it is an array of length 0. is not NULL.                     if(PMS! =NULL) {cmd.                    Parameters.addrange (PMS); } con.                    Open (); returncmd.                ExecuteScalar (); }            }        }        /// <summary>        ///Select queries multi-row multiple columns, calling ExecuteReader () to implement.        Note: Reader will call Close () to close the object when it returns. /// </summary>        /// <param name= "SQL" >SQL command Statements</param>        /// <param name= "PMS" >the collection of parameters used to execute the command</param>        /// <returns></returns>         Public StaticSqlDataReader ExecuteReader (stringSqlparamssqldatareader[] PMS) {SqlConnection con=NewSqlConnection (CONSTR); {                using(SqlCommand cmd =NewSqlCommand (sql, con)) {                    if(PMS! =NULL) {cmd.                    Parameters.addrange (PMS); }                    Try{con.                        Open (); //The parameter System.Data.CommandBehavior.CloseConnection indicates that when the close () method of the DataReader object is called externally, within the close () method, The close () method of the connection associated with the DataReader is automatically called                        returncmd.                    ExecuteReader (System.Data.CommandBehavior.CloseConnection); }                    Catch                    {                        ////Close here do not write in Finally, because you need to return a SqlDataReaderCon. Close ();////Out error, close the database link in time and put it in "Pool"con.                        Dispose (); Throw;////continue to throw an error message above                    }                }            }        }        /// <summary>        ///return result set DataTable/// </summary>        /// <param name= "SQL" >SQL command Statements</param>        /// <param name= "PMS" >the collection of parameters used to execute the command</param>        /// <returns></returns>         Public StaticDataTable executetable (stringSqlparamssqlparameter[] PMS) {DataTable dt=NewDataTable (); using(SqlDataAdapter adapter =NewSqlDataAdapter (SQL, CONSTR)) {Adapter.            Fill (DT); }            returnDT; }    }}

Self-encapsulated 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.