public
Abstract
Class
DBOBJECT
{/ ** // /// /// can create database connections /// summary> /// param> public Dbobject ( string newConnectionString) {this.connectionString = newConnectionString; this.Connection = new SqlConnection (this.connectionString);} // database connection string private string connectionString; public string ConnectionString {get {return this.connectionString;}} // database connection Object Private SqlConnection Connection; / ** // /// /// Returns a command object of a SQL statement, which uses a stored procedure // / The storage process is used to perform the SELECT statement /// Summary> /// The stored procedure name param> /// This parameter does not include the return value param> //// instantiated command object returns> private SqlCommand BuildQueryCommand (string storedProcName, IDataParameter [] parameters) {SqlCommand command = new SqlCommand (storedProcName, this.Connection); command.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parameter in parameters ) {Command.Parameters.Add (Par AMETER);} Return Command;} / ** //// // / creates a command object, the command object functions by a stored procedure /// This stored procedure only performs non-query statements /// < / summary> /// The stored procedure name param> /// This parameter contains an INT type return value, representing the result of the execution / param> /// instantiated command object returns> private SqlCommand BuildIntCommand (string storedProcName, IDataParameter [] parameters) {SqlCommand command = BuildQueryCommand (storedProcName, parameters); command.Parameters.Add (new SqlParameter ( ReturnValue
, SqldbType.int, 4, ParameterDirection.ReturnValue, False, 0, 0, String.empty, DataRowVersion.default, NULL); Return Command;} / ** /// /// // The function executes a non-query SQL statement, returns the number of success or not, the function gets the command object /// summary> /// executed SQL statement < / param> /// Received the number of rows that affect param> /// A representative success or not number returns> protected int RunsqlORder (String QueryText, Out Int rowsAffected) {int result; try {this.Connection.Open (); SqlCommand command = new SqlCommand (queryText, this.Connection); rowsAffected = command.ExecuteNonQuery (); result = (int) command.Parameters [ "ReturnValue"] .Value;} catch (sqlexception sqlexc) {throw sqlexc;} catch (exception eXC) {throw exc;} Finally {this.connection.close ();} RETURN RESULT;} /// This function performs a non-query stored procedure, returns the number of success or not, the function passes Call BuildintCommand Get Command Object /// Summary> /// The stored procedure name param> /// The parameter of the stored procedure Param> /// The number of affected data lines param> /// A number indicating success or failure to digital returns> protected int Runprocedure (String StoredProcname, IdataParameter) [] Parameters, Out Int RowsAffected) {int result;
try {this.Connection.Open (); SqlCommand command = BuildIntCommand (storedProcName, parameters); rowsAffected = command.ExecuteNonQuery (); result = (int) command.Parameters [ "ReturnValue"] Value;.} catch (SqlException sqlExc) {Throw sqlexc;} catch (exception eXC) {throw exc;} finally {this.connection.close ();} return result;} / ** //// / /// // execution a SQL Statement, return a DataReader /// summary> /// To execute the SQL statement param> /// returned DataReader returns> protected sqldataareader Runsqlorder (String Querytext) {SqlDataReader ReturnReader; try {this.connection.Open (); sqlcommand command = new SQLCommand (Qu eryText, this.Connection); returnReader = command.ExecuteReader (CommandBehavior.CloseConnection);} catch (SqlException sqlExc) {throw sqlExc;} catch (Exception exc) {throw exc;} return returnReader;} / ** //// /// execute a stored procedure, return a DataReader /// Summary> /// stored procedure name param> /// Stored Procedure Parameter param> /// DataReader RETURNS>
protected SqlDataReader RunProcedure (string storedProcName, IDataParameter [] parameters) {SqlDataReader returnReader; try {this.Connection.Open (); SqlCommand command = this.BuildQueryCommand (storedProcName, parameters); command.CommandType = CommandType.StoredProcedure; returnReader = command. ExecuteReader (Commandbehavior.CloseConnection);} catch (sqlexception sqlexc) {throw sqlexc;} catch (exception eXC) {throw exc;} return returnire;} / ** /// //// // / execution one SQL statement, get a DataSet, data stored in the data table name specified in this DataSet /// summary> /// SQL statement param> /// Specified DataTable Name param> /// Return to the obtained DataSet object returns> protected dataset runsqlorder (String querytext, String Ta bleName) {DataSet returnDataSet = new DataSet (); try {this.Connection.Open (); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter (); sqlDataAdapter.SelectCommand = new SqlCommand (queryText, this.Connection); sqlDataAdapter.Fill (returnDataSet, tableName );} Catch (sqlexception sqlexc) {throw sqlexc;} catch (exception eXC) {throw exc;} finally {this.connection.close ();
} RETURN RETURNDATASET;} / ** //// // Operate a stored procedure, get a DataSet, data stored in the data table name specified in the DataSet /// summary> /// < Param name = "storedprocname"> Stored Procedure Name param> /// Parameter of the stored procedure param> /// Specified DataTable name param> /// returns DataSet object obtained returns> protected DataSet RunProcedure (string storedProcName, IDataParameter [] parameters, string tableName) {DataSet returnDataSet = new DataSet (); try {this.Connection.Open () ; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter (); sqlDataAdapter.SelectCommand = BuildQueryCommand (storedProcName, parameters); sqlDataAdapter.Fill (returnDataSet, tableName);} catch (SqlException sqlExc) {throw sqlExc;} catch (Exception exc) {throw exc;} Finally { This.Connection.close ();} Return ReturndataT,} / ** // //// // / Perform a stored procedure, data is stored in the specified data table name of the existing DataSet / // summary> /// SQL statement param> /// Exes specified DataSet object param> /// Specified DataTable Name param> protected, dataset dataset, string tablename) {TRY {this.connection.open ();