Category: utilities Component type: type


The DBConnection class provides a mechanism to manage ODBC connections to a database.


Defined in the DBConnection.h header file.

Refinement of


Associated types


Example: Connecting to a database

int main(int argc, char **argv)




Public Base Classes



X A type that is a model of DBConnection
a Object of type X

Expression semantics

Name Expression Precondition Semantics Postcondition
X a()
  Creates an empty database connection object. The connection object cannot be used until it has been intialized with a Connect() call  
Connect with DSN string
void Connect(const string &s, 

  bool bPrompt = false, 

  bool auto_commit_setting = false)
  Creates a database connection using the ODBC connect string contained in s.

If bPrompt is true it will use ODBC dialogs to prompt for a connection (DTL_NO_POOLING must be defined since ODBC does not allow connection pooling to be enabled when browsing for a connection).

If auto_commit_setting is true then DTL will not attempt to turn of auto-commit and will just leave the default driver commit setting instead of trying to turn auto-commit off. This is useful for connection types such as Excel which don't allow auto-commit to be turned off. (Of course, instead of using this flag you can just enclose Connect() in a try...catch block. DTL will still make the connection if auto-commit can't be turned off and flag the connection as IsConnected() to allow you to know that the connection succeded with errors).
valid database connection - throw if error encountered
Connect using previous DSN void Connect()   Connects using the previous DSN. valid database connection - throw if error encountered
Get the default connection object
static DBConnection &

  Get the default connection object. Returns a static member of the DBConnection class used to hold the default connection object for the library. This default connection object is used as the default connection object by DBStmt, DBView and IndexedDBView if no explicit connection parameter is specified in their constructors.  
Get the default environment object
static DBEnvironment &

  Get the environment object for this connection. Returns a static member of the DBConnection class used to hold the default environment object for the library. To manually release the environment, simply call DBEnvironment::Release().
Connection status bool IsConnected() const   Is this object referencing an active connection?  
Toggle autocommit void SetAutoCommit(bool commit)   Turns autocommit on or off for this connection.  
Get autocommit bool GetAutoCommit() const   Is autocommit enabled for this connection?  
Commit all outstanding transactions
void CommitAll()
  Commit all outstanding transactions against the database.  
Commit all outstanding transactions
void RollbackAll()
  Rollback all outstanding transactions against the database.  
Release this connection void Release()   Releases the connecton and all associated ODBC resources.  
Get the raw ODBC HENV handle for the connection
HENV GetHENV() const
  Returns the raw ODBC HENV handle.  
Get the raw ODBC HDBC handle for the connection
HDBC GetHDBC() const
  Returns the raw ODBC HDBC handle.  
Get the DSN string used by the connection.
string GetDSN() const
  Returns the raw ODBC DSN string used by the connection.  
Swap with another connection
void swap(DBConnection &other)
  Swap *this with other.  
Reference another connection void Share(HENV new_henv, HDBC new_hdbc)   Releases old connection and makes *this refer to the already active connection denoted by the passed in environment and connection handle pair. Once this connection is released, in order to reconnect using this same object, you must use the Connect(const string &) member.  
Get Quote Char const tstring &GetQuoteChar() const   Gets the "quote char" (the character used to mark the beginning and end of SQL identifiers) for this connection.  
Get a list of ODBC data sources.
vector< pair<tstring, tstring> > GetDataSources(SQLUSMALLINT SourceType = SQL_FETCH_FIRST)
  Returns a list of ODBC data source names on this machine by calling SQLDataSources.


  = SQL_FETCH_FIRST = get all sources

  = SQL_FETCH_FIRST_USER = get only user data sources

  = SQL_FETCH_FIRST_SYSTEM = get only system data sources



See also

DBStmt, DBView, IndexedDBView, ValidatedObject

[DTL Home]

Copyright 2002, Michael Gradman and Corwin Joy.

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appears in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Corwin Joy and Michael Gradman make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.

SourceForge Logo

This site written using the ORB. [The ORB]