范例: 带 Extensions 的 ADO

目录

该程序说明了如何从字段检索数值并将数值转换为 C++ 变量。它包括了在程序段(范例:无 Extensions 的 ADO)中所描述的功能。

#define INITGUID
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" 
   no_namespace rename("EOF", "EndOfFile")
#include <stdio.h>
#include "icrsint.h"void dump_com_error(_com_error &e)
   {
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
   }class CCustomRs : 
   public CADORecordBinding
{
BEGIN_ADO_BINDING(CCustomRs)
   ADO_VARIABLE_LENGTH_BINDING_ENTRY(1, adVarChar, m_szau_lname, 
         sizeof(m_szau_lname), lau_lnameStatus, FALSE)
   ADO_VARIABLE_LENGTH_BINDING_ENTRY(2, adVarChar, m_szau_fname, 
         sizeof(m_szau_fname), lau_fnameStatus, TRUE)
END_ADO_BINDING()public:
   CHAR   m_szau_lname[41];
   ULONG   lau_lnameStatus;
   CHAR   m_szau_fname[41];
   ULONG   lau_fnameStatus;
};VOID   main()
   {
   HRESULT hr;
   IADORecordBinding   *picRs = NULL;
   
   ::CoInitialize(NULL);   try 
      {
      _RecordsetPtr pRs.CreateInstance(__uuidof(Recordset));      CCustomRs rs;
      
      pRs->Open("select FirstName, LastName, Age from Employees", 
         "dsn=pubs;uid=sa;pwd=;", 
         adOpenStatic, adLockOptimistic, adCmdUnknown);
      
      if (FAILED(hr = pRs->QueryInterface(__uuidof(IADORecordBinding), 
            (LPVOID*)&picRs)))
         _com_issue_error(hr);
      
      if (FAILED(hr = picRs->BindToRecordset(&rs)))
         _com_issue_error(hr);      while (VARIANT_FALSE == pRs->EndOfFile)
         {
      // 处理 CCustomRs C++ 实例变量中的数据。         printf("\a\tName = %s \t%s", 
            (lau_fnameStatus == adFldOK ? m_szau_fname : "<NULL>"), 
            (lau_lnameStatus == adFldOK ? m_szau_lname): "<NULL>"));      // 更改 Recordset 的当前行。 
      // 新当前行的 Recordset 数据将被
      // 自动取出并防止在 CCustomRs C++ 实例变量中。
   
         pRs->MoveNext();
         }
      }
   catch (_com_error &e)
      {
      dump_com_error(e);
      }   if (picRs)
      picRs->Release();   CoUninitialize();
   };
www.51windows.Net