Attributes and Name Properties Example (VC++)

Microsoft ActiveX Data Objects (ADO)

Attributes and Name Properties Example (VC++)

This example displays the value of the Attributes property for Connection, Field, and Property objects. It uses the Name property to display the name of each Field and Property object.

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

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

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

    AttributesX();

    ::CoUninitialize();
}

///////////////////////////////////////////////////////////
//                                                       //
//      AttributesX Function                             //
//                                                       //
///////////////////////////////////////////////////////////

void AttributesX()
{
    // Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace
    _RecordsetPtr    pRstEmployee  = NULL;
    _ConnectionPtr pConnection = NULL;
    FieldsPtr  fldLoop = NULL;    
    PropertiesPtr proLoop = NULL;

    //Define Other Variables
    HRESULT  hr = S_OK;
    _variant_t Index;
    Index.vt = VT_I2;
    int j=0;      
    //Open a recordset using a Client Cursor
    //For the Employee Table
    _bstr_t strCnn("Provider=sqloledb;Data Source=srv;"
        "Initial Catalog=Pubs;User Id=sa;Password=;");

    try
    {
        // open connection and record set
        TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
        pConnection->Open(strCnn,"","",NULL);
        
        TESTHR(pRstEmployee.CreateInstance(__uuidof(Recordset)));
        pRstEmployee->Open("Employee", 
            _variant_t((IDispatch *)pConnection,true), adOpenForwardOnly,
            adLockReadOnly, adCmdTable);

        // Display the attributes of the Connection.
        printf("Connection attributes: %d \n", pConnection->Attributes);

        // Display the attributes of the employee table's fields
        printf("\nFields attributes:\n");
        fldLoop = pRstEmployee->GetFields();

        for (int i = 0; i < (int)fldLoop->GetCount(); i++)
        {
            Index.iVal=i;
            printf ("   %s = %d \n",
                (LPSTR)fldLoop->GetItem(Index)->GetName(),
                (int)fldLoop->GetItem(Index)->GetAttributes());
        }

        // Display Fields of the Employee table which are NULLBALE
        printf("\nNULLABLE Fields :");
        
        for (int i1 = 0; i1 < (int)fldLoop->GetCount(); i1++)
        {
            Index.iVal = i1;
            
            if (fldLoop->GetItem(Index)->GetAttributes() & 
                    adFldIsNullable)
                {
                    printf ("%s  \n",LPSTR)fldLoop->GetItem(Index)->
                                GetName());
                }
        }

        // Display the attributes of the Employee tables's 
        // properties
        printf("\nProperty attributes:\n");
        proLoop = pRstEmployee->GetProperties();
      
        for (int i2 = 0; i2 < (int)proLoop->GetCount(); i2++)
        {
            j= j+1;
            Index.iVal=i2;
            printf (" %s = %d \n", 
                (LPSTR)(_bstr_t)proLoop->GetItem(Index)->GetName(),
                (int)proLoop->GetItem(Index)->GetAttributes());

            if (((j % 23) == 0) || ( i2==6))
            {
                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();
        pConnection->Close();
    }

    catch(_com_error &e)
    {
        // Notify the user of errors if any.
        
        PrintProviderError(pConnection);
        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;
    long      nCount  = 0;
    long      i    = 0;

    if( (pConnection->Errors->Count) > 0)
    {
        nCount = pConnection->Errors->Count;

        // Collection ranges from 0 to nCount -1.
        for(i = 0; i < nCount; i++)    
        {
            pErr = pConnection->Errors->GetItem(i);
            printf("\t Error number: %x\t%s", (LPCSTR) 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("\nError\n");
    printf("Code = %08lx\n", e.Error());
    printf("Code meaning = %s\n", e.ErrorMessage());
    printf("Source = %s\n", (LPCSTR) bstrSource);
    printf("Description = %s\n", (LPCSTR) bstrDescription);  
}