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:
Connecting to a data source using IDBInitialize
Call CoCreateInstance.
Pass the clsid obtained from CLSIDFromProgID("ASEOLEDB").
Set the connection properties using IDBInitialize.
Connecting to a data source using IDataInitialize
Call CoCreateInstance.
Pass the clsid obtained from MSDAINITIALIZE.
Set the connection properties using IDataInitialize.
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();