Example: ADO with Extensions

See Also   

This program shows how values are retrieved from fields and converted to C++ variables. It includes the functionality described in the program fragment, Example: ADO Without Extensions.

#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)
         {
      // Process data in the CCustomRs C++ instance variables.

         printf("\a\tName = %s \t%s", 
            (lau_fnameStatus == adFldOK ? m_szau_fname : "<NULL>"), 
            (lau_lnameStatus == adFldOK ? m_szau_lname): "<NULL>"));

      // Change the current row of the Recordset. 
      // Recordset data for the new current row will automatically be 
      // extracted and placed in the CCustomRs C++ instance variables.
   
         pRs->MoveNext();
         }
      }
   catch (_com_error &e)
      {
      dump_com_error(e);
      }

   if (picRs)
      picRs->Release();

   CoUninitialize();
   };