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

📄 handlerx.cpp

📁 visual c++ 很全的ado的sample
💻 CPP
字号:
TESTHR( "c:\Program Files\Common Files\System\ADO\msado15.dll" \
    no_namespace rename("EOF", "EndOfFile")
TESTHR( "C:\Program Files\Common Files\System\MSADC\msadco.dll"

#include <ole2.h>
#include <stdio.h>
#include <conio.h>

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void HandlerX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

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

void main()
{
    HRESULT  hr = S_OK;
    hr = ::CoInitialize(NULL);
    if (SUCCEEDED(hr))
    {
        HandlerX();
        printf("Press any key to continue...");
        getch();
        ::CoUninitialize();
    }
}

//////////////////////////////////////////////////////////
//                                                      //
//          HandlerX Function                           //
//                                                      //
//////////////////////////////////////////////////////////

void HandlerX(void) 
{
   HRESULT  hr = S_OK;

    // Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB::  namespace.   
   _RecordsetPtr  pRst  = NULL;
   
   //Define RDS object pointers.
   RDS::IBindMgrPtr dc;

     try
    {
        TESTHR(hr = dc.CreateInstance(__uuidof(RDS::DataControl)));
        dc->Handler = "MSDFMAP.Handler";
        dc->Server = "http://tcsp636";
        dc->Connect = "Data Source=AuthorDatabase";
        dc->SQL = "AuthorById(267-41-2394)";

        // Retrieve the record.
        dc->Refresh();

        // Use another Recordset as a convenience.
        pRst = dc->GetRecordset();
        printf("Author is %s %s",(LPSTR) (_bstr_t) pRst->Fields->GetItem("au_fname")->Value,\
            (LPSTR) (_bstr_t) pRst->Fields->GetItem("au_lname")->Value);
        pRst->Close();
        
    }  // End Try statement.
    catch (_com_error &e)
    {
        PrintProviderError(pRst->GetActiveConnection());
        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;
    long      nCount  = 0;     
    long      i  = 0;

    if( (pConnection->Errors->Count) > 0)
    {
        nCount = pConnection->Errors->Count;
        // Collection ranges from 0 to nCount -1.
        for(i = 0; i < nCount; i++)
        {
            pErr = pConnection->Errors->GetItem(i);
            printf("\t Error number: %x\t%s", 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);
   
}

⌨️ 快捷键说明

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