Using system; using system.data.sqlclient; using system.configuration;
Namespace DBBASE {public abstract class base {
#Region "Fields of Base Calss"
Protected static string strconn = configurationSettings.appsettings ["strconnection"];
Protected stat String strsql;
#ndregion
#Region "Properties Of Base Class"}
#ndregion
#Region "Functions of Base Class" public base () {// // Todo: add constructor logic here //}
/// /// summary> /// string param> /// Return Int returns> protected static int ExecuteSql (string strSQL) {SqlConnection myCn = new SqlConnection (strConn); SqlCommand myCmd = new SqlCommand (strSQL, myCn); try {myCn.Open (); myCmd.ExecuteNonQuery (); return 0;} catch (System.Data. Sqlclient.sqlexception e) {throw new exception (E.MESSAGE);} finally {mycmd.dispose (); mycn.close ();}}
/// /// summance> /// To execute the SQL statement, string type String param> /// returns implementation, shaping int returns> protected static int ExecuteSqlEx (string strSQL) {SqlConnection myCn = new SqlConnection (strConn); SqlCommand myCmd = new SqlCommand (strSQL, myCn); try {myCn.Open (); SqlDataReader myReader = mycmd.executeReader (); if (MyReader.Read ()) {return 0;} else {throw new exception ("Value Unavailable!");}} catch (system.data.sqlclient.sqlexception e) {throw new exception (E.MESSAGE);} finally {mycmd.dispose (); mycn.close ();}} /// /// Get DataSet /// summary> /// (string) param> /// (DataSet) returns> protected static DataSet ExecuteSql4Ds (string strSQL) {SqlConnection myCn = new SqlConnection (strConn); try {myCn.Open (); SqlDataAdapter sda = New SqldataAdapter (strsql, mycn); DataSet DS = NE W Dataset ("DS"); SDA.FILL (DS); Return DS;} catch (system.data.sqlclient.sqlexception e) {throw new exception (E.MESSAGE);} finally {mycn.close () }
/// /// Get single value /// summary> /// (string) param> /// (int) returns> protected static int ExecuteSql4Value (string strSQL) {SqlConnection myCn = new SqlConnection (strConn); SqlCommand myCmd = new SqlCommand (strSQL, myCn); try {myCn.Open (); object r = myCmd.ExecuteScalar (); if (Object. Equals (r, null) {throw new exception ("Value Unavailable!");} Else {return (int) r;}} catch (system.data.sqlclient.sqlexception e) {throw new exception (E.MESSAGE) Finally {mycmd.dispose (); mycn.close ();}} /// /// Get Object /// summary> /// (string) param> /// (object) returns> protected static object ExecuteSql4ValueEx (string strSQL) {SqlConnection myCn = new SqlConnection (strConn); SqlCommand myCmd = new SqlCommand (strSQL, myCn); try {myCn. Open (); object r = mycmd.executescalar (); if (Object.equals (R, NULL)) {Throw new Exception ("Object Unavailable!");} else {return r;}} catch (system.data.sqlclient.sqlexception e) {throw new exception (E.MESSAGE);} finally { Mycmd.dispose (); mycn.close ();}}
/// /// //// summary> /// string param> /// int returns> protected static int INT ExecuteSqls (string [] strSQLs) {SqlConnection myCn = new SqlConnection (strConn); SqlCommand myCmd = new SqlCommand (); int j = strSQLs.Length; try {myCn.Open ();} catch (System.Data.SqlClient.SqlException e) {throw new exception (E.MESSAGE);} sqltransaction myTrans = mycn.begintransaction ();
Try {mycmd.connection = mycn; mycmd.transaction = myTrans;
foreach (string str in strSQLs) {myCmd.CommandText = str; myCmd.ExecuteNonQuery ();} myTrans.Commit (); return 0;} catch (System.Data.SqlClient.SqlException e) {myTrans.Rollback (); throw NEW EXCEPTION (E.MESSAGE);} finally {mycmd.dispose (); mycn.close ();}}
#ndregion}}}