Connecting to a data source using OLE DB

The following describes how to use OLE DB interfaces to establish a connection to an Adaptive Server database.

There are two ways to set up a connection using OLE DB, described as follows:

StepsConnecting to a data source using IDBInitialize

  1. Call CoCreateInstance.

  2. Pass the clsid obtained from CLSIDFromProgID("ASEOLEDB").

  3. Set the connection properties using IDBInitialize.

StepsConnecting to a data source using IDataInitialize

  1. Call CoCreateInstance.

  2. Pass the clsid obtained from MSDAINITIALIZE.

  3. Set the connection properties using IDataInitialize.

Code example

A code example for establishing an OLE DB connection follows:

wchar_t* szInitializationString = L"Provider=ASEOLEDB;
   User ID=sa;Password=;Initial Catalog=pubs2;
   Data Source=MANGO:5000;"

IDataInitialize* pIDataInitialize = NULL;
HRESULT hr = CoCreateInstance(
     __uuidof(MSDAINITIALIZE), NULL, CLSCTX_ALL,
     __uuidof(IDataInitialize), (void**)&pIDataInitialize);

IDBInitialize* pIDBInitialize = NULL;
hr = pIDataInitialize->GetDataSource(NULL, CLSCTX_ALL,
     szInitializationString,
     __uuidof(IDBInitialize), (IUnknown**)&pIDBInitialize);
hr = pIDBInitialize->Initialize();

IDBCreateSession* pIDBCreateSession = NULL;
hr = pIDBInitialize->QueryInterface(
     IID_IDBCreateSession, (void**)&pIDBCreateSession);

IDBCreateCommand* pIDBCreateCommand = NULL;
hr = pIDBCreateSession->CreateSession(NULL,
     IID_IDBCreateCommand,
     (IUnknown**)&pIDBCreateCommand);

ICommandText* pICommandText = NULL;
hr = pIDBCreateCommand->CreateCommand(NULL,
     IID_ICommandText, (IUnknown**)&pICommandText);

// use the command object
// ...

pICommandText->Release();

pIDBCreateSession->Release();
pIDBCreateCommand->Release();
pIDBInitialize->Release();
pIDataInitialize->Release();