Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Using Enumerators

OLE DB enumerators are special rowsets that list all available OLE DB providers. It used to be quite difficult to write enumerators, but Visual C++ provides a CEnumerator class that enables your application to view a list of all of the available data sources, as well as other available enumerators. This class provides a mechanism to search through all available data sources.

To use the CEnumerator class, simply instantiate it. The CEnumerator class is inherited from CRowset and therefore contains all the navigation capabilities (MoveFirst, MoveNext, and so forth) of traditional rowsets. Instead of accessing database data, the CEnumerator class accesses OLE DB provider data. The following OnEnumerate() function shows how to display all the OLE DB providers using the CEnumerator class:

LRESULT OnEnumerate(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&
bHandled) {
    char message[4096];
    CEnumerator dbEnum;        //Declare Enumerator
    dbEnum.Open();
    HRESULT hr = dbEnum.MoveFirst();
    strcpy (message, "");
    while (hr == S_OK) {
        sprintf(message, "%s%-60S \t%S\n", message, dbEnum.m_szName, dbEnum.m_szDescription);
        hr = dbEnum.MoveNext();
    }
    MessageBox(message, "Enumerated OLE DB Providers");
    dbEnum.Close();
    return 0;
}

The output from the OnEnumerate() function can be viewed in Figure 22.9.

22fig09.gif

Figure 22.9 It's easy to retrieve a list of all OLE DB enumerators using the CEnumerator class.

Share ThisShare This

Informit Network