Database Access Layer (C #)

xiaoxiao2021-03-06  43

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 //}

///

/// /// string /// Return Int 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 ();}}

///

/// /// To execute the SQL statement, string type String /// returns implementation, shaping int 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 /// /// (string) /// (DataSet) 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 /// /// (string) /// (int) 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 /// /// (string) /// (object) 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 ();}}

///

/// //// /// string /// int 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}}}

转载请注明原文地址:https://www.9cbs.com/read-55026.html

New Post(0)