/// <summary> ///Database Helper Classes/// </summary> Public classSQLHelper {Private Static stringConnStr ="server=.; database=***;uid=**;p wd=***"; //Defining Objects Private StaticSqlConnection conn =NULL; Private StaticSqlCommand cmd =NULL; Private StaticSqlDataAdapter SDA =NULL; Private StaticDataSet ds =NULL; /// <summary> ///querying multi-row multiple columns/// </summary> Public StaticDataSet Search (stringSqlparamssqlparameter[] Parameters) { Try { using(conn =NewSqlConnection (CONNSTR)) {Conn. Open (); CMD=NewSqlCommand (SQL, conn); foreach(SqlParameter Pinchparameters) {cmd. Parameters.Add (P); } SDA=NewSqlDataAdapter (CMD); DS=NewDataSet (); Sda. Fill (DS); returnds; } } Catch(Exception ex) {Throw NewException (ex. Message); } finally{Conn. Close (); } } /// <summary> ///Query single row/// </summary> Public Static ObjectExecuteScalar (stringSqlparamssqlparameter[] Parameters) { Try { using(conn =NewSqlConnection (CONNSTR)) {Conn. Open (); CMD=NewSqlCommand (SQL, conn); foreach(SqlParameter Pinchparameters) {cmd. Parameters.Add (P); } Objectobj =cmd. ExecuteScalar (); returnobj; } } Catch(Exception ex) {Throw NewException (ex. Message); } finally{Conn. Close (); } } /// <summary> ///Query single row (add and revise)/// </summary> Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] Parameters) { Try { using(conn =NewSqlConnection (CONNSTR)) {Conn. Open (); CMD=NewSqlCommand (SQL, conn); foreach(SqlParameter Pinchparameters) {cmd. Parameters.Add (P); } introw =cmd. ExecuteNonQuery (); returnRow; } } Catch(Exception ex) {Throw NewException (ex. Message); } finally{Conn. Close (); } } }
All-purpose SqlHelper help class