范例: 无 Extensions 的 ADO

目录

该程序段说明了如何从字段检索数值并将数值转换为 C++ 变量。

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" 
   no_namespace rename("EOF", "EndOfFile")
#include <stdio.h>Class CEmployee
{
public:
   FetchEmployeeData();   char m_szFirstName[30];
   char m_szLastName[30];
   int nAge;
};CEmployee::FetchEmployeeData()
{
_ConnectionPtr   pCon();
_RecordsetPtr    pRs();
FieldPtr         pfldFirstName, pfldLastName, pfldAge;
_variant_t      vFirstName, vLastName, vAge;pCon.CreateInstance(__uuidof(Connection));
pCon->Open("pubs", "sa", "");pRs.CreateInstance(__uuidof(Recordset));
pRs->Open("select FirstName, LastName, Age from Employees", pCon, 
   adOpenForwardOnly, adLockReadOnly, adCmdUnknown);pfldFirstName = pRs->Fields->GetItem(0);
pfldLastName = pRs->Fields->GetItem(1);
pfldAge = pRs->Fields->GetItem(2);while (VARIANT_FALSE == pRs->EndOfFile)
   {
   vFirstName.Clear();
   vLastName.Clear();
   vAge.Clear();   vFirstName = pfldFirstName->Value;
   WideCharToMultiByte(CP_ACP, 0, vFirstName.bstrVal, -1, 
      m_szFirstName, sizeof(m_szFirstName), NULL, NULL);   vLastName = pfldLastName->Value;
   WideCharToMultiByte(CP_ACP, 0, vLastName.bstrVal, -1, 
      m_szLastName, sizeof(m_szLastName), NULL, NULL);   nAge = vAge.iVal;   pRs->MoveNext();
   }
}
www.2mysite.net