This example demonstrates the Provider property by opening three Connection objects using different providers. It also uses the DefaultDatabase property to set the default database for the Microsoft ODBC Provider.
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include <ole2.h>
#include <stdio.h>
#include <conio.h>
// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void ProviderX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
///////////////////////////////
// //
// Main Function //
// //
///////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
ProviderX();
::CoUninitialize();
}
/////////////////////////////////
// //
// ProviderX Function //
// //
/////////////////////////////////
void ProviderX(void)
{
HRESULT hr = S_OK;
// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection1 = NULL;
_ConnectionPtr pConnection2 = NULL;
_ConnectionPtr pConnection3 = NULL;
try
{
// Open a Connection using the Microsoft ODBC provider.
TESTHR(pConnection1.CreateInstance(__uuidof(Connection)));
pConnection1->ConnectionString = "driver={SQL Server};"
"server=dhale1;uid=sa;pwd=;";
pConnection1->Open("","","",NULL);
pConnection1->DefaultDatabase = "Pubs";
// Display the provider
printf("\n\nConnection1 provider: %s \n\n",
(LPCSTR)pConnection1->Provider);
// Open a connection using the OLE DB Provider for Microsoft Jet.
TESTHR(pConnection2.CreateInstance(__uuidof(Connection)));
pConnection2->Provider = "Microsoft.Jet.OLEDB.3.51";
char *sConn = "c:\\Program Files\\Microsoft Office\\Office\\"
"Samples\\Northwind.mdb";
pConnection2->Open(sConn,"admin","",NULL);
// Display the provider
printf("Connection2 provider: %s \n\n",(LPCSTR)pConnection2->
Provider);
// Open a Connection using the Microsoft SQL Server provider.
TESTHR(pConnection3.CreateInstance(__uuidof(Connection)));
pConnection3->Provider = "sqloledb";
pConnection3->Open("Data Source=dhale1;Initial Catalog=Pubs;",
"sa","",NULL);
// Display the provider.
printf("Connection3 provider: %s\n\n",(LPCSTR)pConnection3->
Provider);
pConnection1->Close();
pConnection2->Close();
pConnection3->Close();
}
catch (_com_error &e)
{
// Notify the user of errors if any.
PrintProviderError(pConnection1);
if(pConnection2) PrintProviderError(pConnection2);
if(pConnection3) PrintProviderError(pConnection3);
PrintComError(e);
}
}
///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Error collection.
ErrorPtr pErr = NULL;
if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0;i < nCount;i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number,
(LPCSTR) pErr->Description);
}
}
}
///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print COM errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}