Class AbstractDataConnection
- Namespace
- CMS.DataEngine
- Assembly
- CMS.DataEngine.dll
Represents SQL Server data connection.
public abstract class AbstractDataConnection : IDataConnection, INotCopyThreadItem
- Inheritance
-
objectAbstractDataConnection
- Implements
- Derived
- Extension Methods
Constructors
AbstractDataConnection(string)
Constructor.
protected AbstractDataConnection(string connectionString)
Parameters
connectionString
stringConnection string
Fields
mCommandTimeout
Command timeout.
protected int mCommandTimeout
Field Value
- int
mConnectionString
Connection string for specific provider.
protected string mConnectionString
Field Value
- string
mConnectionStringName
Connection string name
protected string mConnectionStringName
Field Value
- string
mLastError
Last connection error
protected string mLastError
Field Value
- string
mNativeConnection
Native connection object. It depends on provider type.
protected IDbConnection mNativeConnection
Field Value
- IDbConnection
mNestedConnections
Nested connections indexed by the connection string [connectionString -> IDbConnection]
protected SafeDictionary<string, IDataConnection> mNestedConnections
Field Value
- SafeDictionary<string, IDataConnection>
mNestedConnectionsList
List of nested connections
protected List<IDataConnection> mNestedConnectionsList
Field Value
- List<IDataConnection>
mNestedOpenConnections
List of nested connections that were automatically opened
protected List<IDataConnection> mNestedOpenConnections
Field Value
- List<IDataConnection>
mNestedOpenTransactions
List of nested connections that were automatically opened with transaction
protected List<IDataConnection> mNestedOpenTransactions
Field Value
- List<IDataConnection>
mUseScopeConnection
If true, the connection uses the scope connection
protected bool mUseScopeConnection
Field Value
- bool
Properties
CommandTimeout
Command timeout.
public int CommandTimeout { get; set; }
Property Value
- int
ConnectionString
Connection string for specific provider.
public string ConnectionString { get; protected set; }
Property Value
- string
KeepOpen
If true, the connection stays open even if close is requested.
public bool KeepOpen { get; set; }
Property Value
- bool
LocalKeepOpen
If true, the connection stays open even if close is requested.
protected bool LocalKeepOpen { get; set; }
Property Value
- bool
LocalTransaction
Transaction object of local instance.
protected IDbTransaction LocalTransaction { get; set; }
Property Value
- IDbTransaction
NativeConnection
Native connection object. It depends on provider type.
public IDbConnection NativeConnection { get; set; }
Property Value
- IDbConnection
NativeDBConnection
Native connection object. It depends on provider type.
protected IDbConnection NativeDBConnection { get; set; }
Property Value
- IDbConnection
Transaction
Transaction object.
public IDbTransaction Transaction { get; set; }
Property Value
- IDbTransaction
UseScopeConnection
If true, the connection uses the scope connection
public bool UseScopeConnection { get; set; }
Property Value
- bool
Methods
BeginTransaction()
Begins a new transaction.
public virtual void BeginTransaction()
BeginTransaction(IsolationLevel)
Begins a new transaction.
public virtual void BeginTransaction(IsolationLevel isolation)
Parameters
isolation
IsolationLevel
BulkInsert(DataTable, string, BulkInsertSettings)
Performs a bulk insert of the data into a target database table
public abstract void BulkInsert(DataTable sourceData, string targetTable, BulkInsertSettings insertSettings = null)
Parameters
sourceData
DataTableSource data table
targetTable
stringName of the target DB table
insertSettings
BulkInsertSettingsBulk insert configuration
CheckThreadSafety()
Checks the thread safety of current operation.
protected void CheckThreadSafety()
Close()
Closes connection to the database.
public virtual void Close()
CommitTransaction()
Commits current transaction.
public virtual void CommitTransaction()
CreateCommand(string)
Creates a new SQL command
protected abstract DbCommand CreateCommand(string cmdText)
Parameters
cmdText
stringCommand text
Returns
- DbCommand
CreateDataAdapter()
Creates a data adapter
protected abstract DbDataAdapter CreateDataAdapter()
Returns
- DbDataAdapter
CreateNativeConnection()
Creates a new native connection
protected abstract IDbConnection CreateNativeConnection()
Returns
- IDbConnection
CreateParameter(DataParameter)
Creates a new command parameter.
protected abstract DbParameter CreateParameter(DataParameter parameter)
Parameters
parameter
DataParameterQuery parameter
Returns
- DbParameter
Dispose()
Disposes the connection object.
public void Dispose()
ExecuteNonQuery(string, QueryDataParameters, QueryTypeEnum, bool)
Executes the query and returns the number of rows affected.
public virtual int ExecuteNonQuery(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, bool requiresTransaction)
Parameters
queryText
stringQuery or stored procedure to be run.
queryParams
QueryDataParametersQuery parameters.
queryType
QueryTypeEnumIndicates if query is a SQL query or stored procedure.
requiresTransaction
boolIf true, the query should run within transaction.
Returns
- int
ExecuteNonQueryAsync(string, QueryDataParameters, QueryTypeEnum, bool, CancellationToken)
An asynchronous version of ExecuteNonQuery(string, QueryDataParameters, QueryTypeEnum, bool) which executes the query asynchronously and returns the number of rows affected.
The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.
public virtual Task<int> ExecuteNonQueryAsync(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, bool requiresTransaction, CancellationToken cancellationToken)
Parameters
queryText
stringQuery or stored procedure to be run.
queryParams
QueryDataParametersQuery parameters.
queryType
QueryTypeEnumIndicates if query is a SQL query or stored procedure.
requiresTransaction
boolIf true, the query should run within transaction.
cancellationToken
CancellationTokenThe cancellation instruction.
Returns
- Task<int>
A task representing the asynchronous operation.
ExecuteQuery(string, QueryDataParameters, QueryTypeEnum, bool)
Returns result of the query.
public virtual DataSet ExecuteQuery(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, bool requiresTransaction)
Parameters
queryText
stringQuery or stored procedure to be run
queryParams
QueryDataParametersQuery parameters
queryType
QueryTypeEnumIndicates it query is a SQL query or stored procedure
requiresTransaction
boolIf true, the query should run within transaction
Returns
- DataSet
ExecuteReader(string, QueryDataParameters, QueryTypeEnum, CommandBehavior)
Executes the query and returns result of the query as a System.Data.Common.DbDataReader.
public virtual DbDataReader ExecuteReader(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, CommandBehavior commandBehavior)
Parameters
queryText
stringQuery or stored procedure to be run.
queryParams
QueryDataParametersQuery parameters.
queryType
QueryTypeEnumIndicates if query is a SQL query or stored procedure.
commandBehavior
CommandBehaviorCommand behavior.
Returns
- DbDataReader
Remarks
Opened connection is closed upon disposal of the data reader.
ExecuteReaderAsync(string, QueryDataParameters, QueryTypeEnum, CommandBehavior, CancellationToken)
An asynchronous version of ExecuteReader(string, QueryDataParameters, QueryTypeEnum, CommandBehavior) which executes the query asynchronously and returns result as a System.Data.Common.DbDataReader.
The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.
public virtual Task<DbDataReader> ExecuteReaderAsync(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, CommandBehavior commandBehavior, CancellationToken cancellationToken)
Parameters
queryText
stringQuery or stored procedure to be run.
queryParams
QueryDataParametersQuery parameters.
queryType
QueryTypeEnumIndicates if query is a SQL query or stored procedure.
commandBehavior
CommandBehaviorCommand behavior.
cancellationToken
CancellationTokenThe cancellation instruction.
Returns
- Task<DbDataReader>
A task representing the asynchronous operation.
Remarks
Opened connection is closed upon disposal of the data reader.
ExecuteScalar(string, QueryDataParameters, QueryTypeEnum, bool)
Executes the query and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
public virtual object ExecuteScalar(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, bool requiresTransaction)
Parameters
queryText
stringQuery or stored procedure to be run.
queryParams
QueryDataParametersQuery parameters.
queryType
QueryTypeEnumIndicates if query is a SQL query or stored procedure.
requiresTransaction
boolIf true, the query should run within transaction.
Returns
- object
ExecuteScalarAsync(string, QueryDataParameters, QueryTypeEnum, bool, CancellationToken)
An asynchronous version of ExecuteScalar(string, QueryDataParameters, QueryTypeEnum, bool), which executes the query asynchronously and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
The cancellation token can be used to request that the operation be abandoned before the command timeout elapses. Exceptions will be reported via the returned Task object.
public virtual Task<object> ExecuteScalarAsync(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, bool requiresTransaction, CancellationToken cancellationToken)
Parameters
queryText
stringQuery or stored procedure to be run.
queryParams
QueryDataParametersQuery parameters.
queryType
QueryTypeEnumIndicates if query is a SQL query or stored procedure
requiresTransaction
boolIf true, the query should run within transaction.
cancellationToken
CancellationTokenThe cancellation instruction.
Returns
- Task<object>
A task representing the asynchronous operation.
GetExecutingConnection(string, bool)
Gets the connection that executes the given query
public IDataConnection GetExecutingConnection(string connectionStringName, bool newConnection = false)
Parameters
connectionStringName
stringConnection string name
newConnection
boolIf true, a new connection instance is created
Returns
GetNestedConnection(string, bool)
Gets the nested connection with the given connection string
public IDataConnection GetNestedConnection(string connectionString, bool newConnection = false)
Parameters
connectionString
stringConnection string
newConnection
boolIf true, a new connection is created
Returns
HandleDbError(string, DbException)
Handles the database error. Returns false, if the error was not handled
protected virtual bool HandleDbError(string message, DbException ex)
Parameters
message
stringError message
ex
DbExceptionDatabase exception
Returns
- bool
HandleError(string, Exception)
Handles the error. Returns false, if the error was not handled
protected bool HandleError(string queryText, Exception ex)
Parameters
queryText
stringQuery text
ex
ExceptionException to handle
Returns
- bool
IsOpen()
Returns true if connection to the database is open.
public virtual bool IsOpen()
Returns
- bool
IsTransaction()
Indicates if transaction is running.
public virtual bool IsTransaction()
Returns
- bool
LoadDefaultConnectionString()
Loads the default connection string
protected void LoadDefaultConnectionString()
NativeConnectionExists()
Returns true if the native connection exists.
public virtual bool NativeConnectionExists()
Returns
- bool
NativeDBConnectionExists()
Returns true if the native connection exists.
public virtual bool NativeDBConnectionExists()
Returns
- bool
Open()
Opens connection to the database.
public virtual void Open()
PrepareCommand(string, QueryDataParameters, QueryTypeEnum, bool, ref bool, ref bool)
Prepares the SQL command for the query.
protected virtual DbCommand PrepareCommand(string queryText, QueryDataParameters queryParams, QueryTypeEnum queryType, bool allowTransaction, ref bool closeConnection, ref bool commitTransaction)
Parameters
queryText
stringQuery or stored procedure to be run
queryParams
QueryDataParametersQuery parameters
queryType
QueryTypeEnumIndicates it query is a SQL query or stored procedure
allowTransaction
boolAllow transaction for the command
closeConnection
boolClose connection
commitTransaction
boolCommit transaction
Returns
- DbCommand
ProcessOutParameters(DbParameterCollection, IEnumerable<DataParameter>)
Ensures propagation of output (or inputoutput) parameters after the SQL command execution to the query parameters collection.
protected virtual void ProcessOutParameters(DbParameterCollection commandParams, IEnumerable<DataParameter> queryParams)
Parameters
commandParams
DbParameterCollectionSQL command parameters
queryParams
IEnumerable<DataParameter>Query parameters
Remarks
Processing of output parameters is ensured for the following methods (and their async variants if exist): ExecuteQuery(string, QueryDataParameters, QueryTypeEnum, bool), ExecuteNonQuery(string, QueryDataParameters, QueryTypeEnum, bool), ExecuteScalar(string, QueryDataParameters, QueryTypeEnum, bool).
RollbackTransaction()
Rollbacks current transaction.
public virtual void RollbackTransaction()
SetParameterType(DbParameter, Type)
Sets the parameter type to a destination type.
protected virtual void SetParameterType(DbParameter param, Type type)
Parameters
param
DbParameterParameter to set
type
TypeDesired parameter type
ValidateTransaction(IDbTransaction)
Validates the transaction
protected void ValidateTransaction(IDbTransaction tr)
Parameters
tr
IDbTransactionTransaction to validate