다음을 통해 공유


State 속성 예제(VC++)

이 예제에서는 비동기 연결이 열리고 비동기 명령이 실행되는 동안 State 속성을 사용하여 메시지를 표시합니다.

// BeginStateCpp.cpp  
// compile with: /EHsc  
#import "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 StateX();  
void PrintProviderError(_ConnectionPtr pConnection);  
void PrintComError(_com_error &e);  
  
int main() {  
   if (FAILED(::CoInitialize(NULL)))  
      return -1;  
  
   StateX();  
   ::CoUninitialize();  
}  
  
void StateX() {  
   HRESULT  hr = S_OK;  
  
   // Define string variables.  
   _bstr_t strSQLChange("UPDATE Titles SET Type = 'self_help' WHERE Type = 'psychology'");  
   _bstr_t strSQLRestore("UPDATE Titles SET Type = 'psychology' WHERE Type = 'self_help'");  
   _bstr_t strCnn("Provider='sqloledb'; Data Source='My_Data_Source'; Initial Catalog='pubs'; Integrated Security='SSPI';");  
  
   // Define ADO object pointers.  Initialize pointers on define.  
   // These are in the ADODB::  namespace.  
   _ConnectionPtr pConnection = NULL;  
   _ConnectionPtr pConnection2 = NULL;  
   _CommandPtr pCmdChange = NULL;  
   _CommandPtr pCmdRestore = NULL;  
  
   try {  
      // Open two asynchronous connections,displaying a message while connecting.  
      TESTHR(pConnection.CreateInstance(__uuidof(Connection)));  
      TESTHR(pConnection2.CreateInstance(__uuidof(Connection)));  
  
      pConnection->Open (strCnn, "", "", adAsyncConnect);  
      while(pConnection->State == adStateConnecting)  
         printf("Opening first connection....\n\n");  
  
      pConnection2->Open (strCnn, "", "", adAsyncConnect);  
      while(pConnection2->State == adStateConnecting)  
         printf("Opening second connection....\n\n");  
  
      // Create two command objects.  
      TESTHR(pCmdChange.CreateInstance(__uuidof(Command)));  
      pCmdChange->ActiveConnection = pConnection;  
      pCmdChange->CommandText = strSQLChange;  
  
      TESTHR(pCmdRestore.CreateInstance(__uuidof(Command)));  
      pCmdRestore->ActiveConnection = pConnection2;  
      pCmdRestore->CommandText = strSQLRestore;  
  
      // Executing the commands,displaying a message while they are executing.  
      pCmdChange->Execute(NULL,NULL,adAsyncExecute);  
      while(pCmdChange->State == adStateExecuting)  
         printf("Change command executing...\n\n");  
  
      pCmdRestore->Execute(NULL,NULL,adAsyncExecute);  
      while(pCmdRestore->State == adStateExecuting)  
         printf("Restore command executing...\n\n");  
   }  
   catch (_com_error &e) {  
      // Notify the user of errors if any.  
      PrintProviderError(pConnection);  
      PrintComError(e);  
   }  
  
   if (pConnection)  
      if (pConnection->State == adStateOpen)  
         pConnection->Close();  
   if (pConnection2)  
      if (pConnection2->State == adStateOpen)  
         pConnection2->Close();  
}  
  
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);  
      }  
   }  
}  
  
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);  
}  

참고 항목

State 속성(ADO)