ADO Data Access helper class SQLServeHelper.cs

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Configuration;usingSystem.Data.SqlClient;usingSystem.Data;namespacetest{/// <summary>    ///encapsulating operations on a database/// </summary>     Public Sealed classSqlserverdalhelper {Private Static stringsqlConnectionString = configurationmanager.connectionstrings["ConnectionString"].        ConnectionString; ///<summary>        ///Execute SQL statement///</summary>        ///<param name= "SQL" >SQL statements</param>         Public Static voidExecutesqlnonqurey (stringSQL) {SqlConnection Connection=NewSqlConnection (sqlconnectionstring); Connection.            Open (); SqlCommand Command=NewSqlCommand (sql,connection); Command.            ExecuteNonQuery (); Connection.        Close (); }        ///<summary>        ///Execute SQL statement returned by DataReader///</summary>        ///<param name= "SQL" >SQL statements</param>        ///<returns>DataReader</returns>         Public StaticSqlDataReader Exectesqlreader (stringSQL) {SqlConnection Connection=NewSqlConnection (sqlconnectionstring); Connection.            Open (); SqlCommand Command=NewSqlCommand (sql,connection); SqlDataReader Sqlreader=command.            ExecuteReader (); returnSqlreader; }        ///<summary>        ///Executing stored procedures///</summary>        ///<param name= "StoredProcedureName" >Stored Procedure name</param>        ///<param name= "Parameters" >no return value type</param>         Public Static voidExecuteprocedurenonquery (stringStoredProcedureName, idataparameter[] parameters) {SqlConnection Connection=NewSqlConnection (sqlconnectionstring); Connection.            Open (); SqlCommand Command=NewSqlCommand (storedprocedurename,connection); Command.commandtype=CommandType.StoredProcedure; //command. Connection = Connection;            if(Parameters! =NULL)            {                foreach(SqlParameter parameterinchparameters) {command.                Parameters.Add (parameter); }} command.            ExecuteNonQuery (); Connection.        Close (); }        ///<summary>        ///Executing stored procedures///</summary>        ///<param name= "StoredProcedureName" >Stored Procedure name</param>        ///<param name= "Parametes" >Parameters</param>        ///<returns>SqlDataReader</returns>         Public StaticSqlDataReader Executeprocedurereader (stringstoredprocedurename,idataparameter[] Parameters) {SqlConnection Connection=NewSqlConnection (sqlconnectionstring); Connection.            Open (); SqlCommand Command=NewSqlCommand (storedprocedurename,connection); Command.commandtype=CommandType.StoredProcedure; if(Parameters! =NULL)            {                foreach(SqlParameter parameterinchparameters) {command.                Parameters.Add (Parameters); }} SqlDataReader Sqlreader=command.            ExecuteReader (); returnSqlreader; }    }}

The data Access assistant divides commonly used database operations methods ExecuteScalar (), ExecuteNonQuery (), and ExecuteReader () into SQL statements and stored procedures in two ways.

When implemented with SQL statements, you can use code similar to the following:

Sqlserverhelper.executesqlnonquery (SQL);//sql as SQL statement

Through the stored procedure is realistic, you can use similar code as follows:

Sqlserverhelper.executesqlnonquery (proc,parameters);//proc stored procedure name, parameter

ADO Data Access helper class SQLServeHelper.cs

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.