Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Connection Objects

ADO uses Connection objects to represent an individual connection to an OLE DB datasource. Of course, if you are also using MSDASQL, this OLE DB datasource might also correspond to an ODBC datasource.

Any operation performed on a datasource requires a Connection object, although you don't necessarily need to create it yourself. In many cases, you can simply let ADO create a connection used by command or recordset objects. However, you can also perform many operations by using just the Connection object, such as executing SQL that updates database tables. Connection objects are also used to create a single connection that is used by several other objects, which can be useful for optimizing your application. Connection objects are also used to manage transactions in ADO.

To declare a Connection instance, use Visual C++'s smart CComPTR pointer and the CoCreateInstance() function:

//ADO header files Added by Chuck Wood for ADO support
#include <adoid.h>
#include <adoint.h>
#include <comdef.h>
//...
CcomPtr<ADOConnection> m_pConn;
//...
CoCreateInstance(CLSID_CADOConnection, NULL,
    CLSCTX_INPROC_SERVER, IID_IADOConnection,
    (LPVOID *) &m_pConn);

After creating a Connection variable, you can connect to the database using the ADOConnection.Open() method:

m_pConn->Open((CComBSTR) "VCUnleashed",
    (CComBSTR) "",
    (CComBSTR) "",
    adOpenUnspecified));

ADOConnection Properties

All ADO Connection objects have the following properties, although specific implementations offer additional properties:

Connection objects also contain a Properties collection, which is used to work with certain characteristics of the connection, and an ADOErrors collection, which contains information about any errors or warnings generated by an operation on the connection.

ADOConnection Methods

All ADO connections also support the following methods, although additional methods might also be provided:

Share ThisShare This

Informit Network