Using system.data; using system.data.sqlclient;
Summary description of Namespace SysclassLibrary {/// /// dataaccess. /// Data processing base class, call mode: dataAccess.dataset (String) SQLSTR); or DataAccess.DataSet (String) SQLSTR, REF DATASET DS); description> /// summary> public class DataAccess {#region property protected static SqlConnection conn = new SqlConnection (); protected static SqlCommand comm = new SqlCommand (); #endregion public DataAccess () {// init ();} #region internal static method does not function Execute a DataAccess () constructor
/// /// Open database connection /// summary> private static void OpenConnection () {if (conn.State == ConnectionsTate.closed) {//sysconfig.connectionstring is connected to the system configuration class. Strings, such as "Server = localhost; database = DatabaseName; UID = SA; PWD =;"
Conn.connectionstring = sysconfig.connectionstring; comm.connection = conn; try {conn.open ();} catch (exception e) {throw new exception (E.MESSAGE);}}} /// ///// Close Current Database Connection /// Summary> Private Static Void CloseConnection () {IF (Conn.State == ConnectionsTate.open) Conn.close (); conn.dispose (); comm.dispose ();} #ENDREGION /// /// Execute SQL query statement /// summary> /// incoming SQL statement param> public static void executesql (string sqlstr) {TRY {openConnection (); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; comm.ExecuteNonQuery ();} catch (Exception e) {throw new Exception (e.Message);} finally {closeConnection ();}}
/// /// Execute stored procedure /// summary> /// stored procedure name param> /// SQLParameters collection param> public static void executeporcedure (String procname, sqlparameter [] coll) {{openconnection (); for (int i = 0; i // / Execute a stored procedure and return data set /// summary> /// Stored Procedure Name param> //// SQLParameter Collection param> /// dataset param> public static void executeporcedure (String Procname, Sqlparameter [] COLL, REF DATASET DS) {TRY { Sqldataadapter Da = new SqldataAdapter (); OpenConnection (); for (int i = 0; i
/// /// execute SQL query statement and returns the first record of the first line, the return value needs to be removed when the value is Object ,-> unbox /// summary> /// Inferred SQL statement param> /// Object Return Value returns> public static object executescalar (string sqlstr) {Object Obj = new object (); try {openconnection (); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; obj = comm.ExecuteScalar ();} catch (Exception e) {throw new Exception (e.Message);} finally {closeConnection ();} return obj;} /// /// execute SQL query statement, while transaction /// summary> /// Inferred SQL statement param> public static void ExecuteSQLWITHTRANSACTION string sqlstr) {SqlTransaction trans; trans = conn.BeginTransaction (); comm.Transaction = trans; try {openConnection (); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; comm.ExecuteNonQuery (); trans.Commit ();} Catch {trans. Rollback ();
/// /// Returns the SQLDATAREADER of the specified SQL statement, please note that the object is turned off after use, and will automatically call CloseConnection () to close the database connection // method to turn off the database connection /// Summary> /// Intrinscent SQL statement param> /// SqlDataReader object returns> public static sqldataareader DataReader (SQLDATAREADER DR = null; try { openConnection (); comm.CommandText = sqlstr; comm.CommandType = CommandType.Text; dr = comm.ExecuteReader (CommandBehavior.CloseConnection);} catch {try {dr.Close (); closeConnection ();} catch {}} return DR;} /// /// Returns the SQLDataReader of the specified SQL statement, please note that the object is turned off after use, and will automatically call closeConnection () to turn off the database connection // method to turn off the database connection //// / summary> /// Inferred SQL statement param> /// Incoming Ref DataReader object param> public static void DataReader String Sqlstr, Ref SqldataReader DR) {Try {OpenConnection (); comm.commandtext = SQLSTR; comm.commandtype = CommandType.Text; DR = Comm.ExecuteReader (Commandbehavior.CloseConnecti) ON);} catch {try {if (DR! = null&&! Dr.isclosed) Dr.close ();} catch {} finally {closeConnection ();}}}
/// /// Returns a DataSet /// summary> /// incoming SQL statement param> /// Dataset < / returns> public static DataSet dataSet (string sqlstr) {DataSet ds = new DataSet (); SqlDataAdapter da = new SqlDataAdapter (); try {openConnection (); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da. SELECTCOMMAND = Comm; DA.FILL (DS);} catch (exception e) {throw new exception (E.MESSAGE);} finally {closeConnection ();} return
/// /// Returns DataSet /// summary> ///// summary> /// incoming SQL statement param name = " ds "> DataSet object reference passed param> public static void dataSet (string sqlstr, ref DataSet ds) {SqlDataAdapter da = new SqlDataAdapter (); try {openConnection (); comm.CommandType = CommandType.Text; comm. Commandtext = SQLSTR; da.selectcommand = communication; da.fill (ds);} catch (exception e) {throw new exception (E.MESSAGE);} finally {closeConnection ();}} /// // / Return DataTable /// summary> /// incoming SQL statement param> /// DataTable returns> public static datatable dataatable string sqlstr) {SqlDataAdapter da = new SqlDataAdapter (); DataTable datatable = new DataTable (); try {openConnection (); comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill ( DATATABLE);} catch (exception e) {throw new exception (E.MESSAGE);} finally {c LoseConnection ();} Return DataTable;
/// /// Execute the specified SQL statement, and assign a value to the incoming DataTable /// summary> /// Inferred SQL statement param> // / ref DataTable dt param> public static void dataTable (string sqlstr, ref DataTable dt) {SqlDataAdapter da = new SqlDataAdapter (); try {openConnection (); comm.CommandType = CommandType.Text Comm.commandtext = SQLSTR; da.selectcommand = communication; da.fill (dt);} catch (exception e) {throw new exception (E.MESSAGE);} finally {closeconnection ();}} /// // / Perform a parameter stored procedure and return data set /// summary> /// stored procedure name param> /// SQLParameterCollection input parameters param> /// returns> public static DataTable dataTable (string procName, SqlParameterCollection parameters) {SqlDataAdapter da = new SqlDataAdapter (); DataTable datatable = new DataTable (); try {openConnection (); Comm.Parameters.clear (); comm.commandtype = commandtype.storedprocedure; comm.commandte Xt = procname; foreach (SqlParameter Para in parameters) {SQLParameter P = (SQLParameter) Para; Comm.Parameters.Add (p);} da.selectcommand = communication; da.fill (DataTable);} catch (Exception E) { THROW New Exception (E.MESSAGE);} finally {closeConnection ();
public static DataView dataView (string sqlstr) {SqlDataAdapter da = new SqlDataAdapter (); DataView dv = new DataView (); DataSet ds = new DataSet (); try {openConnection (); comm.CommandType = CommandType.Text; comm.CommandText = SQLSTR; DA.SELECTCOMMAND = COMM; DA.FILL (DS); DV = DS.TABLES [0] .defaultview;} catch (exception e) {throw new exception (E.MESSAGE);} finally {closeConnection (); } Return DV;}}}