Cancel 方法范例 (VC++/ADO)
来源:互联网 发布:java 生成淘宝客链接 编辑:程序博客网 时间:2024/05/21 07:12
本范例使用 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 declarationsinline 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);}
- Cancel 方法范例 (VC++/ADO)
- ADO Cancel 方法
- ADO 事件模型范例 (VC++)
- ADO 事件模型范例 (VC++)
- VC中调用ADO对象访问数据库的范例
- VC操作ADO的方法
- vc++中调用ADO的常用方法
- vc++中调用ADO的常用方法
- VC用ADO连接oracle的方法
- VC用ADO连接oracle的方法 .
- VC + ADO + Access数据文件简单加密方法
- VC++ ADO调用存储过程方法
- VC++ ADO调用存储过程方法
- ADO + VC
- VC ADO
- VC+ADO
- VC ADO
- vc ado
- 中国最厉害的CEO简历曝光-百度搜狐携程网新浪当当网SOHO中国网通等
- C和C++中的static
- Axis2 User's Guide
- 关于Apache的404错误重定向的问题
- 如何切分同视图窗口
- Cancel 方法范例 (VC++/ADO)
- E文积累_20071009
- 测试order by 是在group by之前执行的还是之后执行的
- Err 对象
- 读Axis2用户帮助文档 (axis2中文文档)
- Dll原理和使用
- Zend framework版blog的demo实例教程
- C++/GDI+ 学习笔记(二)——几个例子
- 简简单单避免程序重复启动