Cancel Method Example (VC++)

Microsoft ActiveX Data Objects (ADO)

Cancel Method Example (VC++)

This example uses the Cancel method to cancel a command executing on a Connection object if the connection is busy.

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

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

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

    CancelX();

    ::CoUninitialize();
    }
}

///////////////////////////////////////////////////////////
//                                                       //
//      CancelX Function                                 //
//                                                       //
///////////////////////////////////////////////////////////

void CancelX()
{
    // Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace
     _ConnectionPtr pConnection = NULL;

     //Define Other Variables
    HRESULT  hr = S_OK;
    _bstr_t strCmdChange;
    _bstr_t strCmdRestore;
    BOOL booChanged = FALSE;

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

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

        // Define command strings.
        strCmdChange = "UPDATE Titles SET type = 'self_help' "
        "WHERE type = 'psychology'";

        strCmdRestore = "UPDATE Titles SET type = 'psychology' "
        "WHERE type = 'self_help'";

        // Begin a transaction, then execute a command asynchronously.
        pConnection->BeginTrans();
        pConnection->Execute(strCmdChange,NULL,adAsyncExecute);

        // do something else for a little while - this could be changed
        for (int i = 1; i<=10 ;i++) 
        {
            i = i + i;
            printf("%d\n", i);
        }

        // If the command has NOT completed, cancel the execute
        // and roll back the transaction. Otherwise, commit the
        // transaction.
        if ((pConnection->GetState()) && (adStateExecuting))
        {
            pConnection->Cancel();
            pConnection->RollbackTrans();
            booChanged = FALSE;
            printf("Update canceled.\n");
        }
        else
        {
            pConnection->CommitTrans();
            booChanged = TRUE;
            printf("Update complete.\n");
        }

        // If the change was made, restore the data
        // because this is a demonstration.
        if (booChanged)
        {
            pConnection->Execute(strCmdRestore,NULL,0);
            printf("Data restored.\n");
        }
        // Cleanup object before exit    
        pConnection->Close ();
    }

    catch(_com_error &e)
    {
        // Notify user of any errors that result from
        // executing the query.
        // Pass a connection pointer accessed from the Connection.
        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;

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