Cancel 方法范例 (VC++/ADO)

 

本范例使用 Cancel 方法,在连接繁忙时取消在 Connection 对象上执行的命令。

#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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值