理解 Visual C++ Extensions for ADO(4)
file://开始程序执行
void main(void)
{
::CoInitialize(NULL); file://初始化COM对象
try
{
file://typedef _com_ptr_t<_Recordset, __uuidof(_Recordset)> _RecordsetPtr:指向一个_Recordset的智能指针
_RecordsetPtr pRs("ADODB.Recordset");
file://定义一个CCustomRS: public CADORecordBinding类的实例
CCustomRs rs;
file://把IADORecordBindingPtr接口类型对象picRs指定到pRs对象,从而实现接口和对象之间的关联
IADORecordBindingPtr picRs(pRs);
file://调用_Recordset的Open方法取得Recordset对象
pRs->Open("SELECT * FROM Employee ORDER BY lname",
"dsn=pubs;uid=sa;pwd=;",
adOpenStatic, adLockOptimistic, adCmdText);
file://利用CCustomRS类中的宏定义,实现数据之间的绑定
file://如果绑定成功,我们就可以使用rs对象的属性了
TESTHR(picRs->BindToRecordset(&rs));
file://大家注意,这里的EndOfFile对应上文import语句中的rename("EOF", "EndOfFile"),其实就是EOF。rename的作用为了防止命名冲突。
while (!pRs->EndOfFile)
{
// Process data in the CCustomRs C++ instance variables.
printf("Name = %s %s
",
(rs.m_ul_fnameStatus == adFldOK ? rs.m_ch_fname: ""),
(rs.m_ul_lnameStatus == adFldOK ? rs.m_ch_lname: ""));
// Move to the next row of the Recordset.
// Fields in the new row will automatically be
// placed in the CCustomRs C++ instance variables.
pRs->MoveNext();
}
}
catch (_com_error &e )
{
printf("Error:
");
printf("Code = %08lx
", e.Error());
printf("Meaning = %s
", e.ErrorMessage());
printf("Source = %s
", (LPCSTR) e.Source());
printf("Description = %s
", (LPCSTR) e.Description());
}
file://清除COM对象实例
::CoUninitialize();
}

