VS Connect Sql--dbhelper

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Text;usingSystem.Configuration;namespacedal{ Public classDBHelper {Private Static stringdbconnectstring = configurationmanager.connectionstrings["defaultconnection"].        ConnectionString; Private StaticSqlConnection Conn; Private StaticSqlDataAdapter da; Private StaticSqlCommand cmd; Private StaticDBHelper DBHelper;  PublicDBHelper () {conn=NewSqlConnection (dbconnectstring); }        /// <summary>        ///instantiating a DBHelper object/// </summary>        /// <returns></returns>         Public Staticdbhelper Instance () {if(DBHelper = =NULL) {DBHelper=NewDBHelper (); }            returnDBHelper; }        /// <summary>        ///Open a database connection/// </summary>        voiddbopen () {if(Conn. state = =connectionstate.closed) {conn.            Open (); }        }        /// <summary>        ///To close a database connection/// </summary>        voiddbclose () {if(Conn. state = =ConnectionState.Open) {conn.            Close (); }        }        /// <summary>        ///Execute SQL statement get DataSet/// </summary>        /// <param name= "SQL" >SQL statements</param>        /// <returns>DataTable Data Set</returns>         PublicDataTable Getdatatablebysql (stringSQL)            {dbopen (); DataTable DT=NewDataTable (); Da=NewSqlDataAdapter (SQL, conn); Try{da.                Fill (DT); returnDT; }            Catch            {                return NULL; }            finally{dbclose (); }        }        /// <summary>        ///Execute SQL statement/// </summary>        /// <param name= "SQL" >SQL statements</param>        /// <returns>whether the execution succeeds</returns>         Public BOOLExcutesql (stringSQL)            {dbopen (); CMD=NewSqlCommand (SQL, conn); Try{cmd.                ExecuteNonQuery (); return true; }            Catch            {                return false; }            finally{dbclose (); }        }        /// <summary>        ///Executing stored procedures/// </summary>        /// <param name= "Proname" >Stored Procedure name</param>        /// <param Name= "Paras" >Stored Procedure Parameters</param>        /// <returns>whether the execution succeeds</returns>         Public BOOLExcuteprocedure (stringProname, Sqlparameter[] paras)            {dbopen (); CMD=NewSqlCommand (PRONAME, conn); Cmd.commandtype=CommandType.StoredProcedure;  for(inti =0; I < paras. Length; i++) {cmd.            Parameters.Add (Paras[i]); }            Try{cmd.                ExecuteNonQuery (); return true; }            Catch            {                return false; }            finally{dbclose (); }        }        /// <summary>        ///executing a stored procedure to get a dataset/// </summary>        /// <param name= "Proname" >Stored Procedure name</param>        /// <param Name= "Paras" >Stored Procedure Parameters</param>        /// <returns>DataTable Data Set</returns>         PublicDataTable Getdatatablebyprocedure (stringProname, Sqlparameter[] paras)            {dbopen (); CMD=NewSqlCommand (PRONAME, conn); Cmd.commandtype=CommandType.StoredProcedure; Da=NewSqlDataAdapter (CMD); DataTable DT=NewDataTable ();  for(inti =0; I < paras. Length; i++) {cmd.            Parameters.Add (Paras[i]); }            Try{da.                Fill (DT); returnDT; }            Catch            {                return NULL; }            finally{dbclose (); }        }    }}

VS Connect Sql--dbhelper

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.