description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CDataConnection class |
CDataConnection class |
03/27/2019 |
|
|
77432d85-4e20-49ec-a0b0-142137828471 |
Manages the connection with the data source.
class CDataConnection
Header: atldbcli.h
Name | Description |
---|---|
CDataConnection::CDataConnection |
Constructor. Instantiates and initializes a CDataConnection object. |
CDataConnection::Copy |
Creates a copy of an existing data connection. |
CDataConnection::Open |
Opens a connection to a data source using an initialization string. |
CDataConnection::OpenNewSession |
Opens a new session on the current connection. |
Name | Description |
---|---|
CDataConnection::operator BOOL |
Determines whether the current session is open or not. |
CDataConnection::operator bool |
Determines whether the current session is open or not. |
CDataConnection::operator CDataSource& |
Returns a reference to the contained CDataSource object. |
CDataConnection::operator CDataSource* |
Returns a pointer to the contained CDataSource object. |
CDataConnection::operator CSession& |
Returns a reference to the contained CSession object. |
CDataConnection::operator CSession* |
Returns a pointer to the contained CSession object. |
CDataConnection
is a useful class for creating clients because it encapsulates necessary objects (data source and session) and some of the work you need to do when connecting to a data source
Without CDataConnection
, you have to create a CDataSource
object, call its OpenFromInitializationString
method, then create an instance of a CSession
object, call its Open
method, then create a CCommand
object and call its Open
* methods.
With CDataConnection
, you only need to create a connection object, pass it an initialization string, then use that connection to open commands. If you plan on using your connection to the database repeatedly, it's a good idea to keep the connection open, and CDataConnection
provides a convenient way to do that.
Note
If you are creating a database application that needs to handle multiple sessions, you will need to use OpenNewSession
.
Instantiates and initializes a CDataConnection
object.
CDataConnection();
CDataConnection(const CDataConnection &ds);
ds
[in] A reference to an existing data connection.
The first override creates a new CDataConnection
object with default settings.
The second override creates a new CDataConnection
object with settings equivalent to the data connection object you specify.
Creates a copy of an existing data connection.
CDataConnection& Copy(const CDataConnection & ds) throw();
ds
[in] A reference to an existing data connection to copy.
Opens a connection to a data source using an initialization string.
HRESULT Open(LPCOLESTR szInitString) throw();
szInitString
[in] The initialization string for the data source.
A standard HRESULT
.
Opens a new session using the current connection object's data source.
HRESULT OpenNewSession(CSession & session) throw();
session
[in/out] A reference to the new session object.
The new session uses the current connection object's contained data source object as its parent, and can access all of the same information as the data source.
A standard HRESULT
.
Determines whether the current session is open or not.
operator BOOL() throw();
Returns BOOL
(MFC typedef) value. TRUE
means the current session is open; FALSE
means the current session is closed.
Determines whether the current session is open or not.
operator bool() throw();
Returns a bool
(C++ data type) value. true
means the current session is open; false
means the current session is closed.
Returns a reference to the contained CDataSource
object.
operator const CDataSource&() throw();
This operator returns a reference to the contained CDataSource
object, allowing you to pass a CDataConnection
object where a CDataSource
reference is expected.
If you have a function (such as func
below) that takes a CDataSource
reference, you can use CDataSource&
to pass a CDataConnection
object instead.
[!code-cppNVC_OLEDB_Consumer#3] [!code-cppNVC_OLEDB_Consumer#4]
Returns a pointer to the contained CDataSource
object.
operator const CDataSource*() throw();
This operator returns a pointer to the contained CDataSource
object, allowing you to pass a CDataConnection
object where a CDataSource
pointer is expected.
See operator CDataSource&
for a usage example.
Returns a reference to the contained CSession
object.
operator const CSession&();
This operator returns a reference to the contained CSession
object, allowing you to pass a CDataConnection
object where a CSession
reference is expected.
If you have a function (such as func
below) that takes a CSession
reference, you can use CSession&
to pass a CDataConnection
object instead.
[!code-cppNVC_OLEDB_Consumer#5] [!code-cppNVC_OLEDB_Consumer#6]
Returns a pointer to the contained CSession
object.
operator const CSession*() throw();
This operator returns a pointer to the contained CSession
object, allowing you to pass a CDataConnection
object where a CSession
pointer is expected.
See operator CSession&
for a usage example.
OLE DB consumer templates
OLE DB consumer templates reference