⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 descriptionx.cpp

📁 visual c++ 很全的ado的sample
💻 CPP
字号:
#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 DescriptionX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

///////////////////////////////////////////////////////////
//                                                       //
//      Main Function                                    //
//                                                       //
///////////////////////////////////////////////////////////

void main()
{
    if(FAILED(::CoInitialize(NULL)))
        return;

    DescriptionX();

    //Wait here for user to see the output..
    printf("\nPress any key to continue...");
    getch();
    ::CoUninitialize();
}

///////////////////////////////////////////////////////////
//                                                       //
//      DescriptionX Function                            //
//                                                       //
///////////////////////////////////////////////////////////

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
        TESTHR(pConnection.CreateInstance(__uuidof(Connection)));

        if (FAILED(hr = pConnection->Open("nothing","","",NULL)))
        {
            _com_issue_error(hr);
            exit(1);
        }

        // Cleanup object before exit.
        pConnection->Close();
    }
    
    catch(_com_error)
    {
        // Pass a connection pointer.
        PrintProviderError(pConnection);
    }
}

///////////////////////////////////////////////////////////
//                                                       //
//      PrintProviderError Function                      //
//                                                       //
///////////////////////////////////////////////////////////

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.
        PrintComError(e);
    }
}

///////////////////////////////////////////////////////////
//                                                       //
//      PrintComError Function                           //
//                                                       //
///////////////////////////////////////////////////////////

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("Error\n");
   printf("\tCode = %08lx\n", e.Error());
   printf("\tCode meaning = %s", e.ErrorMessage());
   printf("\tSource = %s\n", (LPCSTR) bstrSource);
   printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -