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