Catalog ActiveConnection Property Example (VC++)
Setting the ActiveConnection property to a valid, open connection "opens" the catalog. From an open catalog, you can access the schema objects contained within that catalog.
#import "c:\Program Files\Common Files\system\ado\msado15.dll"
#import "c:\Program Files\Common Files\system\ado\msadox.dll" \
no_namespace
#include "iostream.h"
#include "stdio.h"
#include "conio.h"
// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void OpenConnectionX(void);
void OpenConnectionWithStringX(void);
//////////////////////////////////////////////////////////
// //
// Main Function //
// //
//////////////////////////////////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
OpenConnectionX();
OpenConnectionWithStringX();
::CoUninitialize();
}
//////////////////////////////////////////////////////////
// //
// OpenConnectionX Function //
// //
//////////////////////////////////////////////////////////
void OpenConnectionX(void)
{
HRESULT hr = S_OK;
// Define ADOX object pointers.
// Initialize pointers on define.
// These are in the ADOX:: namespace.
_CatalogPtr m_pCatalog = NULL;
//Define ADODB object pointers
ADODB::_ConnectionPtr m_pCnn = NULL;
// Define string variables.
_bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;"
"Data source = c:\\Program Files\\Microsoft Office\\"
"Office\\Samples\\Northwind.mdb;");
try
{
TESTHR(hr = m_pCnn.CreateInstance(__uuidof(ADODB::Connection)));
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
m_pCnn->Open(strcnn,"","",NULL);
m_pCatalog->PutActiveConnection(
_variant_t((IDispatch *) m_pCnn));
_variant_t vIndex = (short) 0;
cout<<m_pCatalog->Tables->GetItem(vIndex)->Type<<endl;
m_pCnn->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\n\tSource : %s \n\tdescription : %s \n ",
(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}
catch(...)
{
cout << "Error occured in include files...."<< endl;
}
}
//////////////////////////////////////////////////////////
// //
// OpenConnectionWithStringX Function //
// //
//////////////////////////////////////////////////////////
void OpenConnectionWithStringX(void)
{
HRESULT hr = S_OK;
// Define ADOX object pointers.
// Initialize pointers on define.
// These are in the ADOX:: namespace.
_CatalogPtr m_pCatalog = NULL;
// Define string variables.
_bstr_t strcnn("Provider=Microsoft.JET.OLEDB.4.0;"
"Data source = c:\\Program Files\\Microsoft Office\\"
"Office\\Samples\\Northwind.mdb;");
try
{
TESTHR(hr = m_pCatalog.CreateInstance(__uuidof (Catalog)));
m_pCatalog->PutActiveConnection(strcnn);
_variant_t vIndex = (short) 0;
cout<<m_pCatalog->Tables->GetItem(vIndex)->Type<<endl;
}
catch(_com_error &e)
{
// Notify the user of errors if any.
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\n\tSource : %s \n\tdescription : %s \n ",
(LPCSTR)bstrSource,(LPCSTR)bstrDescription);
}
catch(...)
{
cout << "Error occured in include files...."<< endl;
}
}