Simple SqlHelper.

Source: Internet
Author: User

Now there are textbooks on the internet and many of the SqlHelper do not close the connection and release resources, so that in the development of the project, the site's response is getting slower, resulting in a crash.

Now I'm rolling out this sqlhelper using a use-from-release resource without our manual release.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Configuration;4 usingSystem.Data;5 usingSystem.Data.SqlClient;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 Ten namespaceDAL One { A      Public Static  classSqlHelper -     { -         //define a connection string the         //readonly modified variables, can only be copied at initialization time, and in the constructor to assign values, other places can only read cannot set the value -         Private Static ReadOnly stringConstr = configurationmanager.connectionstrings[""]. ConnectionString; -         /// <summary> -         ///ExecuteNonQuery +         /// </summary> -         /// <param name= "SQL" >the statement to execute/param> +         /// <param name= "type" >Specify the type (stored procedure or statement)</param> A         /// <param name= "PMS" >Parameters</param> at         /// <returns></returns> -          Public Static intExecuteNonQuery (stringSQL, CommandType type,paramssqlparameter[] PMS) -         { -             using(SqlConnection con =NewSqlConnection (constr)) -             { -                 using(SqlCommand cmd =NewSqlCommand (sql, con)) in                 { -                     //determine whether an SQL statement or a stored procedure is passed in toCmd.commandtype =type; +                     if(PMS! =NULL) -                     { the cmd. Parameters.addrange (PMS); *                     } $ con. Open ();Panax Notoginseng                     returncmd. ExecuteNonQuery (); -                 } the             } +         } A         /// <summary> the         ///returns a single value +         /// </summary> -         /// <param name= "SQL" ></param> $         /// <param name= "type" ></param> $         /// <param name= "PMS" ></param> -         /// <returns></returns> -          Public Static ObjectExecuteScalar (stringSQL, CommandType type,paramssqlparameter[] PMS) the         { -             using(SqlConnection con =NewSqlConnection (constr))Wuyi             { the                 using(SqlCommand cmd =NewSqlCommand (sql, con)) -                 { WuCmd.commandtype =type; -                     if(PMS! =NULL) About                     { $ cmd. Parameters.addrange (PMS); -                     } - con. Open (); -                     returncmd. ExecuteScalar (); A                 } +             } the         } -          Public StaticSqlDataReader ExecuteReader (stringSQL, CommandType type,paramssqlparameter[] PMS) $         { the             //using is not used here because the reader object cannot close the connection. When the reader object is in use, the connection must be guaranteed to be open.  theSqlConnection con =NewSqlConnection (constr); the             using(SqlCommand cmd =NewSqlCommand (sql, con)) the             { -Cmd.commandtype =type; in                 if(PMS! =NULL) the                 { the cmd. Parameters.addrange (PMS); About                 } the                 Try the                 { the con. Open (); +                     //use CommandBehavior.CloseConnection, which indicates that the associated connection object is closed while reader is turned off, after SqlDataReader is used in the future.  -                     returncmd. ExecuteReader (commandbehavior.closeconnection); the                 }Bayi                     //Exception Execution the                 Catch(Exception) the                 { - con. Close (); - con. Dispose (); the                     Throw; the                 } the             } the         } -          Public StaticDataTable excutedatatable (stringSQL, CommandType type,paramssqlparameter[] PMS) the         { theDataTable dt =NewDataTable (); the             using(SqlDataAdapter adapter =NewSqlDataAdapter (SQL, constr))94             { theAdapter.SelectCommand.CommandType =type; the                 if(pms!=NULL) the                 {98 adapter. SelectCommand.Parameters.AddRange (PMS); About                 } - adapter. Fill (DT);101             }102             returnDT;103         }104     } the}

Simple 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.