. NET SqlHelper application code

Source: Internet
Author: User
Tags connectionstrings

You need to refer to the namespace first, and you also need to right-click the ' reference '---' Add reference '--' assembly '--' frame '--' system.configuration ', SqlHelper belongs to the DAL layer in layer three:

Using System.Data;

Using System.Data.SqlClient;

  Using System.Configuration;

  Public classSqlHelper {Private Static stringConstr = configurationmanager.connectionstrings["Constr"].        ToString ();  Public StaticSqlConnection Open ()//Stored Procedures{SqlConnection con=NewSqlConnection (CONSTR); Con.            Open (); returncon; }        /// <summary>        ///Insert Update Delete/// </summary>        /// <param name= "CommandText" >SQL</param>        /// <param name= "type" >Command Type</param>        /// <param name= "pars" >parameter list</param>        /// <returns></returns>         Public Static intExecutenoquery (stringCommandText, CommandType type,paramssqlparameter[] pars) {            intresult =0; using(SqlConnection con =NewSqlConnection (CONSTR)) {con.                Open (); SqlCommand cmd=NewSqlCommand (CommandText, con); Cmd.commandtype=type; if(Pars! =NULL) {cmd.                Parameters.addrange (pars); } result=cmd.            ExecuteNonQuery (); }            returnresult; }        /// <summary>        ///Select/// </summary>        /// <param name= "CommandText" ></param>        /// <param name= "type" ></param>        /// <param name= "Pars" ></param>        /// <returns></returns>         Public StaticDataTable executedatatable (stringCommandText, CommandType type,paramssqlparameter[] pars) {DataSet DS=NewDataSet (); DataTable DT=NewDataTable (); using(SqlConnection con =NewSqlConnection (CONSTR)) {con.                Open (); SqlCommand cmd=NewSqlCommand (CommandText, con); Cmd.commandtype=type; if(Pars! =NULL) {cmd.                Parameters.addrange (pars); } SqlDataAdapter adpt=NewSqlDataAdapter (CMD); Adpt.                Fill (DS); DT= ds. tables[0]; }            returnDT; }        Private StaticSqlConnection con =NULL;  Public Static voidClosecon () {if(Con! =NULL&& con. state = =connectionstate.open) {con.            Close (); }        }        /// <summary>        ///Select/// </summary>        /// <param name= "CommandText" ></param>        /// <param name= "type" ></param>        /// <param name= "Pars" ></param>        /// <returns></returns>         Public StaticSqlDataReader ExecuteReader (stringCommandText, CommandType type, sqlparameter[] pars) {SqlCommand cmd=NULL; SqlDataReader Reader=NULL; Try{con=NewSqlConnection (CONSTR); Con.                Open (); CMD=NewSqlCommand (CommandText, con); if(Pars! =NULL) {cmd.                Parameters.addrange (pars); } Reader=cmd.            ExecuteReader (); }            Catch(SqlException ex) {}returnreader; }         Public Static ObjectExecscalre (stringCommandText, CommandType type, sqlparameter[] pars) {            Objectobj =NULL; using(SqlConnection con =NewSqlConnection (CONSTR)) {con.                Open (); SqlCommand cmd=NewSqlCommand (CommandText, con); Cmd.commandtype=type; if(Pars! =NULL) {cmd.                Parameters.addrange (pars); } obj=cmd.            ExecuteScalar (); }            returnobj; }

The Web. config code is as follows:

<configuration>  <system.web>    <compilation debug="true" targetframework="4.5" />    "4.5  " />  </system.web>  <connectionStrings>    <add name=" Constr " connectionstring="Data source=ls--20161126tne;initial catalog=testone; User Id = sa; pwd=as123123"></add>  </connectionStrings></configuration>

. NET SqlHelper application code

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.