Count プロパティの例 (VC++)

この例では、Employee データベース内の 2 つのコレクションで Count プロパティを使用します。このプロパティで、各コレクション内のオブジェクト数を取得し、これらのコレクションを列挙するループに上限を設定します。

#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 CountX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

///////////////////////////////////////////////////////////
//                                                       //
//      Main Function                                    //
//                                                       //
///////////////////////////////////////////////////////////

void main()
{
    if(FAILED(::CoInitialize(NULL)))
        return;

    CountX();

    ::CoUninitialize();
}

///////////////////////////////////////////////////////////
//                                                       //
//      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;

    _bstr_t strCnn("Provider=SQLOLEDB;Data Source=srv;"
        "Initial Catalog=Pubs;User Id=sa;Password=;");

    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                           //
//                                                       //
///////////////////////////////////////////////////////////

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);
}