ConnectionString, ConnectionTimeout, and State Properties Example (VC++)

Microsoft ActiveX Data Objects (ADO)

ConnectionString, ConnectionTimeout, and State Properties Example (VC++)

This example demonstrates different ways of using the ConnectionString property to open a Connection object. It also uses the ConnectionTimeout property to set a connection timeout period, and the State property to check the state of the connections. The GetState function is required for this procedure to run.

#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 ConnectionStringX();
_bstr_t GetState(int intState); 
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

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

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

    ConnectionStringX();

    ::CoUninitialize();
}

///////////////////////////////////////////////////////////
//                                                       //
//      ConnectionStringX Function                       //
//                                                       //
///////////////////////////////////////////////////////////

void ConnectionStringX()
{
    // Define Connection object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace
     _ConnectionPtr pConnection1 = NULL;
     _ConnectionPtr pConnection2 = NULL;
     _ConnectionPtr pConnection3 = NULL;
     _ConnectionPtr pConnection4 = NULL;

    //Define Other Variables
    HRESULT  hr = S_OK;

    try
    {
        // Open a connection without using a Data Source Name (DSN).
        TESTHR(pConnection1.CreateInstance(__uuidof(Connection)));
        pConnection1->ConnectionString = "driver={SQL Server};" 
            "server=dhale1;uid=sa;pwd=;database=Pubs";
        pConnection1->ConnectionTimeout = 30;
        pConnection1->Open("","","",NULL);

        // Open a connection using a DSN and ODBC tags.
        TESTHR(pConnection2.CreateInstance(__uuidof(Connection)));
        pConnection2->ConnectionString = "DSN=Pubs;UID=sa;PWD=;";
        pConnection2->Open("","","",NULL);

        // Open a connection using a DSN and OLE DB tags.
        TESTHR(pConnection3.CreateInstance(__uuidof(Connection)));
        pConnection3->ConnectionString = "Data Source=Pubs;User ID=sa;"
            "Password=;";
        pConnection3->Open("","","",NULL);

        // Open a connection using a DSN and individual
        // arguments instead of a connection string.
        TESTHR(pConnection4.CreateInstance(__uuidof(Connection)));
        pConnection4->Open("Pubs","sa","",NULL);

        // Display the state of the connections.
        printf("cnn1 state: %s\n", 
            (LPCTSTR)GetState(pConnection1->State));
        printf("cnn2 state: %s\n", 
            (LPCTSTR)GetState(pConnection2->State));
        printf("cnn3 state: %s\n", 
            (LPCTSTR)GetState(pConnection3->State));
        printf("cnn4 state: %s\n", 
            (LPCTSTR)GetState(pConnection4->State));

        //Cleanup objects before exit.
        pConnection4->Close();
        pConnection3->Close();
        pConnection2->Close();
        pConnection1->Close();
    }

    catch(_com_error &e)
    {
        // Notify user of any errors.
        // Pass a connection pointer accessed from the Connection.
        PrintProviderError(pConnection1);
        if(pConnection2)
            PrintProviderError(pConnection2);
        if(pConnection3)
            PrintProviderError(pConnection3);
        if(pConnection4)
            PrintProviderError(pConnection4);
        PrintComError(e);
    }
}

///////////////////////////////////////////////////////////
//                                                       //
//      GetState Function                                //
//                                                       //
///////////////////////////////////////////////////////////

_bstr_t GetState(int intState) 
{
    _bstr_t strState; 
    switch(intState) 
    {
        case adStateClosed:
            strState = "adStateClosed";
            break;
        case adStateOpen:
            strState = "adStateOpen";
            break;
        default:
        ;
    }
    return strState;
}

///////////////////////////////////////////////////////////
//                                                       //
//      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, 
                (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);
}