///////////////////////////////////////////////////////////
// //
// Main Function //
// //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
// //
// CountX Function //
// //
///////////////////////////////////////////////////////////
void CountX()
{
// Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace
_RecordsetPtr pRstEmployee = NULL;
//Define Other Variables
HRESULT hr = S_OK;
_variant_t Index;
Index.vt = VT_I2;
int j = 0;
try
{
// Open recordset with data from Employee table.
TESTHR(pRstEmployee.CreateInstance(__uuidof(Recordset)));
pRstEmployee->Open("Employee", strCnn, adOpenForwardOnly,
adLockReadOnly, adCmdTable);
// Print information about Fields collection.
printf("%d Fields in Employee\n", pRstEmployee->Fields->Count);
for (int intLoop = 0;
intLoop <= (pRstEmployee->Fields->Count-1);
intLoop++)
{
Index.iVal = intLoop;
printf(" %s\n",(LPSTR) pRstEmployee->Fields->
GetItem(Index)->Name);
}
// Print information about Properties collection.
printf("\n%d Properties in Employee\n", pRstEmployee->
Properties->Count);
for (intLoop = 0;
intLoop <= (pRstEmployee->Properties->Count - 1);
intLoop++)
{
j++;
Index.iVal = intLoop;
printf(" %s\n" ,(LPSTR)pRstEmployee->Properties->
GetItem(Index)->Name);
if (((j % 23) == 0) || (intLoop == 11))
{
printf("\nPress any key to continue...");
getch();
//Clear the screen for the next display
system("cls");
j = 0;
}
}
// Clean up objects before exit.
pRstEmployee->Close();
}
catch(_com_error &e)
{
// Notify user of any errors that result from
// executing the query.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstEmployee->GetActiveConnection();
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}
///////////////////////////////////////////////////////////
// //
// 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,
pErr->Description);
}
}
}
///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////