C# Class DataAccessFramework.DataTool

An abstract class providing helper functionality to connect to a database.

This class provides functions that can simplfy common database access functionality, e.g. executing a stored procedure.

The tool itself is not tied to any specific database provider. Using this tool to execute database operations separates the application from the actual database server software.

The client code should also use functions in this class to create parameters to pass to the stored procedure. That will ensure that the parameters are compatible with the database provider.

Inheritance: IDisposable
Mostrar archivo Open project: PeteProgrammer/DataAccessFramework

Public Methods

Method Description
BeginTransaction ( ) : void

Starts a new transaction. Specialized classes should override this function and implement custom transaction handling

CommitTransaction ( ) : void

Commits the transaction. Specialized classes should override this function and implement custom transaction handling

CreateBinaryParameter ( string parameterName, Stream dataStream ) : IDataParameter
CreateBinaryParameter ( string parameterName, byte value, int length ) : IDataParameter

Creates database parameter for a binary value.

The implementation will check if the length of the string is actually within the acceptable length. If not, a BinaryParameterTooLongException is thrown.

CreateBoolParameter ( string parameterName, bool value ) : IDataParameter

Creates a database parameter for a Boolean value.

CreateDateTimeParameter ( string parameterName, System.DateTime value ) : IDataParameter

Creates a database parameter for a datetime value

CreateDecimalParameter ( string parameterName, decimal value ) : IDataParameter

Creates database parameter for a decimal value.

CreateGuidParameter ( string parameterName, System.Guid value ) : IDataParameter

Creates database parameter for a GUID value.

CreateIntParameter ( string parameterName, int value ) : IDataParameter

Creates database parameter for an integer value.

CreateLongParameter ( string parameterName, long value ) : IDataParameter

Creates database parameter for an 64 bit integer value.

CreateMoneyParameter ( string parameterName, decimal value ) : IDataParameter

Creates a database parameter for a decimal value used for carrying monetary amount.

CreateStampParameter ( string parameterName, object stamp ) : IDataParameter

Creates a database parameter for a stamp value.

CreateStringParameter ( string parameterName, string value, int length ) : IDataParameter

Creates database parameter for a string value.

The implementation will check if the length of the string is actually within the acceptable length. If not, a StringParameterTooLongException is thrown.

Dispose ( ) : void

Disposes the current instance.

ExecuteNonQuery ( string spName ) : int

Executes a stored procedure that does not return a result.

ExecuteQuery ( Query query ) : IDataReader

Executes a query represendted by a Query instance

ExecuteReader ( string spName ) : IDataReader

Executes a stored procedure that returns a single result set.

ExecuteReaderSingleRow ( string spName ) : IDataReader

Executes a stored procedure that returns a multiple result sets.

Executes a stored procedure that returns a single row.

ExecuteScalar ( string spName ) : object

Executes a stored procedure that returns a single value.

ExecuteSqlNonQuery ( string sql ) : int

Executes an sql statement that does not return a result.

ExecuteSqlReader ( string sql ) : IDataReader

Executes an SQL statement that returns an IDataReader

ExecuteSqlReaderSingleRow ( string sql ) : IDataReader

Executes an SQL statement that returns a single row.

ExecuteSqlScalar ( string sql ) : object

Executes an SQL text statement that returns a single value.

RollbackTransaction ( ) : void

Rolls back the transaction. Specialized classes should override this function and implement custom transaction handling

Protected Methods

Method Description
AssertNotDisposed ( ) : void

Verifies that the object has not been disposed.

CreateCommand ( ) : IDbCommand

Template function for creating a command object.

The DataTool class will call this internally when executing a query

Dispose ( bool disposing ) : void

Template function for disposing resources.

GetConnection ( ) : IDbConnection

Template function for getting a connection object.

The DataTool class will call this internally when executing a query

GetTransaction ( ) : IDbTransaction

Template function for getting a transaction

The DataTool class will call this internally when executing a query

Private Methods

Method Description
ExecuteNonQuery ( string commandText, CommandType commandType ) : int
ExecuteReader ( string sql, CommandType commandType, CommandBehavior behavior ) : IDataReader
ExecuteScalar ( string commandText, CommandType commandType ) : object

Method Details

AssertNotDisposed() protected method

Verifies that the object has not been disposed.
/// The object has been disposed. ///
protected AssertNotDisposed ( ) : void
return void

BeginTransaction() public abstract method

Starts a new transaction. Specialized classes should override this function and implement custom transaction handling
public abstract BeginTransaction ( ) : void
return void

CommitTransaction() public abstract method

Commits the transaction. Specialized classes should override this function and implement custom transaction handling
public abstract CommitTransaction ( ) : void
return void

CreateBinaryParameter() public method

public CreateBinaryParameter ( string parameterName, Stream dataStream ) : IDataParameter
parameterName string
dataStream Stream
return IDataParameter

CreateBinaryParameter() public abstract method

Creates database parameter for a binary value.
The implementation will check if the length of the string is actually within the acceptable length. If not, a BinaryParameterTooLongException is thrown.
/// The size of value exceeds that specified by /// length ///
public abstract CreateBinaryParameter ( string parameterName, byte value, int length ) : IDataParameter
parameterName string /// The name of the parameter. ///
value byte /// The value of the parameter ///
length int /// The length of the parameter as it is specified by the stored procedure. ///
return IDataParameter

CreateBoolParameter() public abstract method

Creates a database parameter for a Boolean value.
public abstract CreateBoolParameter ( string parameterName, bool value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value bool /// The value of the parameter ///
return IDataParameter

CreateCommand() protected abstract method

Template function for creating a command object.
The DataTool class will call this internally when executing a query
protected abstract CreateCommand ( ) : IDbCommand
return IDbCommand

CreateDateTimeParameter() public abstract method

Creates a database parameter for a datetime value
public abstract CreateDateTimeParameter ( string parameterName, System.DateTime value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value System.DateTime /// The value of the parameter ///
return IDataParameter

CreateDecimalParameter() public abstract method

Creates database parameter for a decimal value.
public abstract CreateDecimalParameter ( string parameterName, decimal value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value decimal /// The value of the parameter ///
return IDataParameter

CreateGuidParameter() public abstract method

Creates database parameter for a GUID value.
public abstract CreateGuidParameter ( string parameterName, System.Guid value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value System.Guid /// The value of the parameter ///
return IDataParameter

CreateIntParameter() public abstract method

Creates database parameter for an integer value.
public abstract CreateIntParameter ( string parameterName, int value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value int /// The value of the parameter ///
return IDataParameter

CreateLongParameter() public abstract method

Creates database parameter for an 64 bit integer value.
public abstract CreateLongParameter ( string parameterName, long value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value long /// The value of the parameter ///
return IDataParameter

CreateMoneyParameter() public abstract method

Creates a database parameter for a decimal value used for carrying monetary amount.
public abstract CreateMoneyParameter ( string parameterName, decimal value ) : IDataParameter
parameterName string /// The name of the parameter. ///
value decimal /// The value of the parameter ///
return IDataParameter

CreateStampParameter() public abstract method

Creates a database parameter for a stamp value.
public abstract CreateStampParameter ( string parameterName, object stamp ) : IDataParameter
parameterName string /// The name of the parameter. ///
stamp object /// The value of the parameter ///
return IDataParameter

CreateStringParameter() public abstract method

Creates database parameter for a string value.
The implementation will check if the length of the string is actually within the acceptable length. If not, a StringParameterTooLongException is thrown.
/// The length of value exceeds that specified by /// length ///
public abstract CreateStringParameter ( string parameterName, string value, int length ) : IDataParameter
parameterName string /// The name of the parameter. ///
value string /// The value of the parameter ///
length int /// The length of the parameter as it is specified by the stored procedure. ///
return IDataParameter

Dispose() public method

Disposes the current instance.
public Dispose ( ) : void
return void

Dispose() protected method

Template function for disposing resources.
protected Dispose ( bool disposing ) : void
disposing bool /// Boolean value indicating if we are calling from dispose or the finalizer. /// true indicates we are calling from Dispose; false indicates /// we are calling from the finalizer. ///
return void

ExecuteNonQuery() public method

Executes a stored procedure that does not return a result.
public ExecuteNonQuery ( string spName ) : int
spName string /// Name of the stored procedure to execute. ///
return int

ExecuteQuery() public method

Executes a query represendted by a Query instance
public ExecuteQuery ( Query query ) : IDataReader
query DataAccessFramework.Querying.Query
return IDataReader

ExecuteReader() public method

Executes a stored procedure that returns a single result set.
public ExecuteReader ( string spName ) : IDataReader
spName string /// Name of the stored procedure to execute. ///
return IDataReader

ExecuteReaderSingleRow() public method

Executes a stored procedure that returns a multiple result sets. Executes a stored procedure that returns a single row.
public ExecuteReaderSingleRow ( string spName ) : IDataReader
spName string ///// Name of the stored procedure to execute. /////
return IDataReader

ExecuteScalar() public method

Executes a stored procedure that returns a single value.
public ExecuteScalar ( string spName ) : object
spName string /// Name of the stored procedure to execute. ///
return object

ExecuteSqlNonQuery() public method

Executes an sql statement that does not return a result.
public ExecuteSqlNonQuery ( string sql ) : int
sql string /// The SQL statement to execute ///
return int

ExecuteSqlReader() public method

Executes an SQL statement that returns an IDataReader
public ExecuteSqlReader ( string sql ) : IDataReader
sql string
return IDataReader

ExecuteSqlReaderSingleRow() public method

Executes an SQL statement that returns a single row.
public ExecuteSqlReaderSingleRow ( string sql ) : IDataReader
sql string
return IDataReader

ExecuteSqlScalar() public method

Executes an SQL text statement that returns a single value.
public ExecuteSqlScalar ( string sql ) : object
sql string
return object

GetConnection() protected abstract method

Template function for getting a connection object.
The DataTool class will call this internally when executing a query
protected abstract GetConnection ( ) : IDbConnection
return IDbConnection

GetTransaction() protected abstract method

Template function for getting a transaction
The DataTool class will call this internally when executing a query
protected abstract GetTransaction ( ) : IDbTransaction
return IDbTransaction

RollbackTransaction() public abstract method

Rolls back the transaction. Specialized classes should override this function and implement custom transaction handling
public abstract RollbackTransaction ( ) : void
return void