Data layer base class

xiaoxiao2021-03-06  42

public

Abstract

Class

DBOBJECT

{/ ** // ///

/// can create database connections /// /// 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 /// /// The stored procedure name /// This parameter does not include the return value //// instantiated command object 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 /// This parameter contains an INT type return value, representing the result of the execution / param> /// instantiated command object 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 /// /// executed SQL statement < / param> /// Received the number of rows that affect /// A representative success or not number 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 /// /// The stored procedure name /// The parameter of the stored procedure /// The number of affected data lines /// A number indicating success or failure to digital 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 /// /// To execute the SQL statement /// returned DataReader 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 /// /// stored procedure name /// Stored Procedure Parameter /// DataReader

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 /// /// SQL statement /// Specified DataTable Name /// Return to the obtained DataSet object 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 /// /// < Param name = "storedprocname"> Stored Procedure Name /// Parameter of the stored procedure /// Specified DataTable name /// returns DataSet object obtained 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 / // /// SQL statement /// Exes specified DataSet object /// Specified DataTable Name protected, dataset dataset, string tablename) {TRY {this.connection.open ();

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

New Post(0)