Source Property Example (VC++)

Microsoft ActiveX Data Objects (ADO)

Source Property Example (VC++)

This example demonstrates the Source property by opening three Recordset objects based on different data sources.

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

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

    SourceX();

    ::CoUninitialize();
}

///////////////////////////////////////////////
//                                           //
//    SourceX Function                       //
//                                           //
///////////////////////////////////////////////
void SourceX(void) 
{
   HRESULT  hr = S_OK;

   // Define string variables.
   _bstr_t strCmdSQL("Select title,type,pubdate "
            "FROM Titles ORDER BY title");  
   _bstr_t strSQL("SELECT title_ID AS TitleID, title AS Title, " 
            "publishers.pub_id AS PubID, pub_name AS PubName "
            "FROM publishers INNER JOIN titles " 
            "ON publishers.pub_id = Titles.pub_id " 
            "ORDER BY Title");
   _bstr_t strCnn("Provider=sqloledb;Data Source=srv;"
            "Initial Catalog=Pubs;User Id=sa;Password=;");

    // Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace.
   _ConnectionPtr  pConnection          = NULL;
   _RecordsetPtr  pRstTitles        = NULL;
   _RecordsetPtr  pRstPublishers      = NULL;
   _RecordsetPtr  pRstPublishersDirect    = NULL;
   _RecordsetPtr  pRstTitlesPublishers    = NULL;
   _CommandPtr     pCmdSQL          = NULL;

     try
    {
        // Open a connection.
        TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
        pConnection->Open (strCnn, "", "", NULL);

        // Open a recordset based on a command object.
        TESTHR(pCmdSQL.CreateInstance(__uuidof(Command)));
        pCmdSQL->ActiveConnection = pConnection;
        pCmdSQL->CommandText = strCmdSQL;
        pRstTitles = pCmdSQL->Execute(NULL,NULL,adCmdText);

        // Open a recordset based on a a table.
        TESTHR(pRstPublishers.CreateInstance(__uuidof(Recordset)));
        pRstPublishers->Open ("publishers", 
            _variant_t((IDispatch *) pConnection, true),
            adOpenForwardOnly, adLockReadOnly, adCmdTable);

        // Open a recordset based on a table.
        TESTHR(pRstPublishersDirect.CreateInstance(
            __uuidof(Recordset)));
        pRstPublishersDirect->Open ("publishers", 
            _variant_t((IDispatch *) pConnection, true),
            adOpenForwardOnly, adLockReadOnly, adCmdTableDirect);

        // Open a recordset based on an SQL string.
        TESTHR(pRstTitlesPublishers.CreateInstance(
            __uuidof(Recordset)));
        pRstTitlesPublishers->Open(strSQL, 
            _variant_t((IDispatch *) pConnection, true),
            adOpenForwardOnly, adLockReadOnly, adCmdText);

        // Use the Source property to display the source of 
        // each recordset.
        printf("rstTitles source: \n%s\n\n",
            (LPCSTR)(_bstr_t) pRstTitles->GetSource().bstrVal);
        printf("rstPublishers source: \n%s\n\n",
            (LPCSTR)(_bstr_t) pRstPublishers->GetSource().bstrVal);
        printf("rstPublishersDirect source: \n%s\n\n",
            (LPCSTR)(_bstr_t) pRstPublishersDirect->GetSource().bstrVal);
        printf("rstTitlesPublishers source: \n%s\n\n",
            (LPCSTR)(_bstr_t) pRstTitlesPublishers->GetSource().bstrVal);

        pRstTitles->Close();
        pRstPublishers->Close();
        pRstPublishersDirect->Close();
        pRstTitlesPublishers->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 Errors 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, 
                (LPCSTR) 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);
   
}