当前位置: 代码迷 >> 综合 >> Cancel 方法范例 (VC++/ADO)
  详细解决方案

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

热度:78   发布时间:2024-01-21 21:42:28.0

本范例使用 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);
}