C# Class Habanero.DB.DatabaseConnection

A super-class to manage a database connection and execute sql commands
Inheritance: System.MarshalByRefObject, IDatabaseConnection
Afficher le fichier Open project: Chillisoft/habanero Class Usage Examples

Protected Properties

Свойство Type Description
_sqlFormatter Habanero.Base.SqlFormatter

Private Properties

Свойство Type Description
AppendOrderBy void
CreateCommand IDbCommand
CreateDataColumns void
CreateDatabaseConnection IDbConnection
ExecuteSqlInternal int

Méthodes publiques

Méthode Description
BeginTransaction ( IDbConnection openConnection ) : IDbTransaction

Creates a transaction using the IsolationLevel set as the IsolationLevel. If this doesn't work, creates a transaction using the default IsolationLevel. Uses the connection passed in to create the transaction on - this should be open (so use GetOpenConnectionForReading to get the connection first). Override this method to do special transaction creation logic.

CreateParameterNameGenerator ( ) : IParameterNameGenerator

Creates an IParameterNameGenerator for this database connection. This is used to create names for parameters added to an ISqlStatement because each database uses a different naming convention for their parameters.

CreateSqlStatement ( ) : ISqlStatement

Creates a ISqlStatement initialised with this IDatabaseConnection

ErrorSafeConnectString ( ) : string

Returns a connection string with the password removed. This method serves as a secure way of displaying an error message in the case of a connection error, without compromising confidentiality.

ExecuteRawSql ( string sql ) : int

Executes a sql command as before, but with the full sql string provided, rather than with a sql statement object

ExecuteRawSql ( string sql, IDbTransaction transaction ) : int

Executes a sql command that returns no result set and takes no parameters, using the provided connection. This method can be used effectively where the database vendor supports the execution of several sql statements in one ExecuteNonQuery. However, for database vendors like Microsoft Access and MySql, the sql statements will need to be split up and executed as separate transactions.

ExecuteRawSqlScalar ( string sql ) : object

Executes a raw sql statement. BEWARE OF SQL INJECTION ATTACKS. Use for hardcoded sql only.

ExecuteSql ( IEnumerable statements ) : int

Executes a sql command that returns no result set and takes no parameters, using the provided connection

ExecuteSql ( IEnumerable statements, IDbTransaction transaction ) : int

Executes a collection of sql commands that returns no result set and takes no parameters, using the provided connection. This method can be used effectively where the database vendor supports the execution of several sql statements in one ExecuteNonQuery. However, for database vendors like Microsoft Access and MySql, the sql statements will need to be split up and executed as separate transactions.

ExecuteSql ( ISqlStatement sql ) : int

Executes a single sql statement object

ExecuteStoredProcNonQuery ( string procName, IEnumerable @params ) : int

Executes a stored proc with the params given.

ExecuteStoredProcNonQuery ( string procName, IEnumerable @params, int timeout ) : int

Executes a stored proc with the params given using the timeout length given.

GetConnection ( ) : IDbConnection

Returns the first closed connection available or returns a new connection object. Throws an exception and adds a message to the log if there is an error opening a connection.

GetDataTable ( IDataReader reader ) : DataTable

Returns the DataTable for the DataReader.

GetDataTable ( IDataReader reader, string dataTableName ) : DataTable

Returns a dataTable with the data from the reader and the columns names and field types set up.

GetLastAutoIncrementingID ( string tableName, IDbTransaction tran, IDbCommand command ) : long

Gets the value of the last auto-incrementing number. This called after doing an insert statement so that the inserted auto-number can be retrieved. The table name, current IDbTransaction and IDbCommand are passed in so that they can be used if necessary. Note_, this must be overridden in subclasses to include support for this feature in different databases - otherwise a NotImplementedException will be thrown.

LoadDataReader ( ISqlStatement selectSql ) : IDataReader

Loads a data reader

LoadDataReader ( ISqlStatement selectSql, string strOrderByCriteria ) : IDataReader

Loads a data reader and specifies an order-by clause

LoadDataReader ( string selectSql ) : IDataReader

Loads a data reader with the given raw sql select statement

LoadDataReader ( string selectSql, IDbTransaction transaction ) : IDataReader

Loads a data reader with the given raw sql select statement for the specified transaction

LoadDataTable ( ISqlStatement selectSql, string strSearchCriteria, string strOrderByCriteria ) : DataTable

Loads data from the database into a DataTable object, using the sql statement object provided

SetTimeoutPeriod ( int timeoutSeconds ) : void

Set the time-out period in seconds, after which the connection attempt will fail

Méthodes protégées

Méthode Description
CreateCommand ( IDbConnection dbConnection ) : IDbCommand

Creates an IDbCommand using the provided IDbConnection. Sets the timout for the command based on what you have set using SetTimeoutPeriod

DatabaseConnection ( ) : System

Constructor that initialises a new set of null connections

DatabaseConnection ( string assemblyName, string className ) : System

Constructor that allows an assembly name and class name to be specified

DatabaseConnection ( string assemblyName, string className, string connectString ) : System

Constructor to initialise the connection with the assembly name, class name and connection string provided

DoAfterExecute ( SqlStatement statement, IDbCommand command, IDbTransaction transaction ) : void

Executes the necessary after execute logic for a particular SqlStatement.

GetOpenConnection ( IDbTransaction transaction = null ) : IDbConnection

Returns an open connection. If a IDbTransaction is provided then the connection for the transaction is used and opened if necessary.

GetOpenConnectionForReading ( ) : IDbConnection

Either finds a closed connection and opens and returns it, or creates a new connection and returns that. Throws an exception and adds a message to the log if there is an error opening a connection.

SetupCommand ( ISqlStatement statement, IDbCommand command, IDbTransaction transaction ) : void

Sets up an IDbCommand object with the provided statement.

SetupReadTransaction ( IDbCommand dbCommand ) : void

Setup the transaction on the connection to be used for read operations. This may be overridden for a specific provider (eg. sqlLite)

Private Methods

Méthode Description
AppendOrderBy ( ISqlStatement statement, string orderByCriteria ) : void

Appends an order-by clause to the sql statement. " ORDER BY " is automatically prefixed by this method.

CreateCommand ( IDbConnection connection, IDbTransaction transaction ) : IDbCommand

Creates an IDbCommand using the provided IDbConnection. The created command is subscribed to the provided IDbTransaction.

CreateDataColumns ( IDataRecord reader, DataTable dt ) : void
CreateDatabaseConnection ( ) : IDbConnection

Creates a database connection using the assembly name and class name provided.

ExecuteSqlInternal ( IEnumerable statements, IDbConnection openConnection, IDbTransaction transaction ) : int

Method Details

BeginTransaction() public méthode

Creates a transaction using the IsolationLevel set as the IsolationLevel. If this doesn't work, creates a transaction using the default IsolationLevel. Uses the connection passed in to create the transaction on - this should be open (so use GetOpenConnectionForReading to get the connection first). Override this method to do special transaction creation logic.
public BeginTransaction ( IDbConnection openConnection ) : IDbTransaction
openConnection IDbConnection
Résultat IDbTransaction

CreateCommand() protected méthode

Creates an IDbCommand using the provided IDbConnection. Sets the timout for the command based on what you have set using SetTimeoutPeriod
protected CreateCommand ( IDbConnection dbConnection ) : IDbCommand
dbConnection IDbConnection The connection to create the command with
Résultat IDbCommand

CreateParameterNameGenerator() public abstract méthode

Creates an IParameterNameGenerator for this database connection. This is used to create names for parameters added to an ISqlStatement because each database uses a different naming convention for their parameters.
public abstract CreateParameterNameGenerator ( ) : IParameterNameGenerator
Résultat IParameterNameGenerator

CreateSqlStatement() public méthode

Creates a ISqlStatement initialised with this IDatabaseConnection
public CreateSqlStatement ( ) : ISqlStatement
Résultat ISqlStatement

DatabaseConnection() protected méthode

Constructor that initialises a new set of null connections
protected DatabaseConnection ( ) : System
Résultat System

DatabaseConnection() protected méthode

Constructor that allows an assembly name and class name to be specified
protected DatabaseConnection ( string assemblyName, string className ) : System
assemblyName string The assembly name
className string The database class name
Résultat System

DatabaseConnection() protected méthode

Constructor to initialise the connection with the assembly name, class name and connection string provided
protected DatabaseConnection ( string assemblyName, string className, string connectString ) : System
assemblyName string The assembly name
className string The class name
connectString string The connection string. This can be /// generated by the various GetConnectionString() methods, tailored /// for the appropriate database vendors.
Résultat System

DoAfterExecute() protected méthode

Executes the necessary after execute logic for a particular SqlStatement.
protected DoAfterExecute ( SqlStatement statement, IDbCommand command, IDbTransaction transaction ) : void
statement SqlStatement The for which the after execute logic must be run.
command IDbCommand The that was used to execute the .
transaction IDbTransaction The under which the 's command was run.
Résultat void

ErrorSafeConnectString() public méthode

Returns a connection string with the password removed. This method serves as a secure way of displaying an error message in the case of a connection error, without compromising confidentiality.
public ErrorSafeConnectString ( ) : string
Résultat string

ExecuteRawSql() public méthode

Executes a sql command as before, but with the full sql string provided, rather than with a sql statement object
public ExecuteRawSql ( string sql ) : int
sql string The sql statement as a string
Résultat int

ExecuteRawSql() public méthode

Executes a sql command that returns no result set and takes no parameters, using the provided connection. This method can be used effectively where the database vendor supports the execution of several sql statements in one ExecuteNonQuery. However, for database vendors like Microsoft Access and MySql, the sql statements will need to be split up and executed as separate transactions.
Thrown if there is an /// error writing to the database. Also outputs error messages to the log. ///
public ExecuteRawSql ( string sql, IDbTransaction transaction ) : int
sql string A valid sql statement (typically "insert", /// "update" or "delete") as a string. Note_ that this assumes that the /// sqlCommand is not a stored procedure.
transaction IDbTransaction A valid transaction object in which the /// sql must be executed, or null
Résultat int

ExecuteRawSqlScalar() public méthode

Executes a raw sql statement. BEWARE OF SQL INJECTION ATTACKS. Use for hardcoded sql only.
public ExecuteRawSqlScalar ( string sql ) : object
sql string The sql statement to run as a scalar
Résultat object

ExecuteSql() public méthode

Executes a sql command that returns no result set and takes no parameters, using the provided connection
public ExecuteSql ( IEnumerable statements ) : int
statements IEnumerable A valid sql statement (typically "insert", /// "update" or "delete"). Note_ that this assumes that the /// sqlCommand is not a stored procedure.
Résultat int

ExecuteSql() public méthode

Executes a collection of sql commands that returns no result set and takes no parameters, using the provided connection. This method can be used effectively where the database vendor supports the execution of several sql statements in one ExecuteNonQuery. However, for database vendors like Microsoft Access and MySql, the sql statements will need to be split up and executed as separate transactions.
Thrown if there is an /// error writing to the database. Also outputs error messages to the log. ///
public ExecuteSql ( IEnumerable statements, IDbTransaction transaction ) : int
statements IEnumerable A valid sql statement object (typically "insert", /// "update" or "delete"). Note_ that this assumes that the /// sqlCommand is not a stored procedure.
transaction IDbTransaction A valid transaction object in which the /// sql must be executed, or null
Résultat int

ExecuteSql() public méthode

Executes a single sql statement object
public ExecuteSql ( ISqlStatement sql ) : int
sql ISqlStatement The sql statement object
Résultat int

ExecuteStoredProcNonQuery() public méthode

Executes a stored proc with the params given.
public ExecuteStoredProcNonQuery ( string procName, IEnumerable @params ) : int
procName string The stored proc name
@params IEnumerable
Résultat int

ExecuteStoredProcNonQuery() public méthode

Executes a stored proc with the params given using the timeout length given.
public ExecuteStoredProcNonQuery ( string procName, IEnumerable @params, int timeout ) : int
procName string The stored proc name
@params IEnumerable
timeout int The timeout in seconds
Résultat int

GetConnection() public méthode

Returns the first closed connection available or returns a new connection object. Throws an exception and adds a message to the log if there is an error opening a connection.
public GetConnection ( ) : IDbConnection
Résultat IDbConnection

GetDataTable() public méthode

Returns the DataTable for the DataReader.
public GetDataTable ( IDataReader reader ) : DataTable
reader IDataReader
Résultat System.Data.DataTable

GetDataTable() public static méthode

Returns a dataTable with the data from the reader and the columns names and field types set up.
public static GetDataTable ( IDataReader reader, string dataTableName ) : DataTable
reader IDataReader the Reader that the dataTable will be made from
dataTableName string the name of the DataTable
Résultat System.Data.DataTable

GetLastAutoIncrementingID() public méthode

Gets the value of the last auto-incrementing number. This called after doing an insert statement so that the inserted auto-number can be retrieved. The table name, current IDbTransaction and IDbCommand are passed in so that they can be used if necessary. Note_, this must be overridden in subclasses to include support for this feature in different databases - otherwise a NotImplementedException will be thrown.
public GetLastAutoIncrementingID ( string tableName, IDbTransaction tran, IDbCommand command ) : long
tableName string The name of the table inserted into
tran IDbTransaction The current transaction, the one the insert was done in
command IDbCommand The Command the did the insert statement
Résultat long

GetOpenConnection() protected méthode

Returns an open connection. If a IDbTransaction is provided then the connection for the transaction is used and opened if necessary.
protected GetOpenConnection ( IDbTransaction transaction = null ) : IDbConnection
transaction IDbTransaction The to use for the connection.
Résultat IDbConnection

GetOpenConnectionForReading() protected méthode

Either finds a closed connection and opens and returns it, or creates a new connection and returns that. Throws an exception and adds a message to the log if there is an error opening a connection.
protected GetOpenConnectionForReading ( ) : IDbConnection
Résultat IDbConnection

LoadDataReader() public méthode

Loads a data reader
Thrown when an error /// occurred while setting up the data reader. Also sends error /// output to the log.
public LoadDataReader ( ISqlStatement selectSql ) : IDataReader
selectSql ISqlStatement The sql statement object
Résultat IDataReader

LoadDataReader() public méthode

Loads a data reader and specifies an order-by clause
public LoadDataReader ( ISqlStatement selectSql, string strOrderByCriteria ) : IDataReader
selectSql ISqlStatement The sql statement object
strOrderByCriteria string A sql order-by clause
Résultat IDataReader

LoadDataReader() public méthode

Loads a data reader with the given raw sql select statement
Thrown when an error /// occurred while setting up the data reader. Also sends error /// output to the log.
public LoadDataReader ( string selectSql ) : IDataReader
selectSql string The sql statement as a string
Résultat IDataReader

LoadDataReader() public méthode

Loads a data reader with the given raw sql select statement for the specified transaction
Thrown when an error /// occurred while setting up the data reader. Also sends error /// output to the log.
public LoadDataReader ( string selectSql, IDbTransaction transaction ) : IDataReader
selectSql string The sql statement as a string
transaction IDbTransaction Thransaction that gives the context within which the sql statement should be executed
Résultat IDataReader

LoadDataTable() public méthode

Loads data from the database into a DataTable object, using the sql statement object provided
Thrown if there is an /// error reading the database. Also outputs error messages to the log. ///
public LoadDataTable ( ISqlStatement selectSql, string strSearchCriteria, string strOrderByCriteria ) : DataTable
selectSql ISqlStatement The sql statement object
strSearchCriteria string The search criteria as a string /// to append
strOrderByCriteria string The order by criteria as a string /// to append
Résultat System.Data.DataTable

SetTimeoutPeriod() public méthode

Set the time-out period in seconds, after which the connection attempt will fail
public SetTimeoutPeriod ( int timeoutSeconds ) : void
timeoutSeconds int The time-out period in seconds
Résultat void

SetupCommand() protected méthode

Sets up an IDbCommand object with the provided statement.
protected SetupCommand ( ISqlStatement statement, IDbCommand command, IDbTransaction transaction ) : void
statement ISqlStatement The to setup the command with.
command IDbCommand The to set up.
transaction IDbTransaction The that the command runs under.
Résultat void

SetupReadTransaction() protected méthode

Setup the transaction on the connection to be used for read operations. This may be overridden for a specific provider (eg. sqlLite)
protected SetupReadTransaction ( IDbCommand dbCommand ) : void
dbCommand IDbCommand The connection to be set up with a transaction
Résultat void

Property Details

_sqlFormatter protected_oe property

The SqlFormatter that is used to format the Swl for the database type represented by this database connection
protected SqlFormatter,Habanero.Base _sqlFormatter
Résultat Habanero.Base.SqlFormatter