Deskripsi, HelpContext, HelpFile, NativeError, Number, Source, dan SQLState Properties Example (VC++)

Contoh ini memicu kesalahan, menjebaknya, dan menampilkan properti Description, HelpContext, HelpFile, NativeError, Number, Source, dan SQLState dari objek Kesalahan yang dihasilkan.

// BeginDescriptionCpp.cpp  
// compile with: /EHsc  
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")  
// Function declarations  
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};  
void DescriptionX();  
void PrintProviderError(_ConnectionPtr pConnection);  
void PrintComError(_com_error &e);  
int main() {  
   if (FAILED(::CoInitialize(NULL)))  
      return -1;  
void DescriptionX() {  
   // Define ADO object pointers. Initialize pointers on define. These are in the ADODB::  namespace  
   _ConnectionPtr pConnection = NULL;  
   ErrorPtr errorLoop = NULL;  
   // Define Other Variables  
   HRESULT hr = S_OK;  
   try {  
      // Intentionally trigger an error.  open connection  
      if (FAILED(hr = pConnection->Open("Nothing", "", "", adConnectUnspecified))) {  
      // Cleanup object before exit.  
   catch(_com_error) {  
      // Pass a connection pointer.  
void PrintProviderError(_ConnectionPtr pConnection) {  
   // Define Other Variables  
   HRESULT hr = S_OK;  
   _bstr_t strError;  
   ErrorPtr pErr = NULL;  
   try {  
      // Enumerate Errors collection and display properties of each Error object.  
      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 #%d\n", pErr->Number);  
         printf(" %s\n", (LPCSTR)pErr->Description);  
         printf(" (Source: %s)\n", (LPCSTR)pErr->Source);  
         printf(" (SQL State: %s)\n", (LPCSTR)pErr->SQLState);  
         printf(" (NativeError: %d)\n", (LPCSTR)pErr->NativeError);  
         if ((LPCSTR)pErr->GetHelpFile() == NULL)  
            printf("\tNo Help file available\n");  
         else {  
            printf("\t(HelpFile: %s\n)" ,pErr->HelpFile);  
            printf("\t(HelpContext: %s\n)" , pErr->HelpContext);  
   catch(_com_error &e) {  
      // Notify the user of errors if any.  
void PrintComError(_com_error &e) {  
   // Notify the user of errors if any.  
   _bstr_t bstrSource(e.Source());  
   _bstr_t bstrDescription(e.Description());  
   // Print Com errors.  
   printf("\tCode = %08lx\n", e.Error());  
   printf("\tCode meaning = %s", e.ErrorMessage());  
   printf("\tSource = %s\n", (LPCSTR) bstrSource);  
   printf("\tDescription = %s\n", (LPCSTR) bstrDescription);  

Lihat juga

Properti Deskripsi
Objek Kesalahan
Properti HelpContext, HelpFile
Properti HelpContext, HelpFile
Properti NativeError (ADO)
Properti Angka (ADO)
Properti Sumber (Kesalahan ADO)
Properti SQLState