//定义 _RecordsetPtr 型变量;
HRESULT hr;
_RecordsetPtr pRentRecordset;
hr = pRentRecordset.CreateInstance(__uuidof(Recordset));
if (FAILED(hr)) {
MessageBox(_T("创建记录集对象失败."));
return;
}
//调用其 Open 方法,打开一个数据集;
CString strSQL;
strSQL = _T("SELECT * FROM students");
try
{
hr = pRentRecordset->Open(_variant_t(strSQL), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if (SUCCEEDED(hr))
{
_variant_t var;
CString strValue;
int curItem = 0;
while(!pRentRecordset->GetadoEof())
{
var = pRentRecordset->GetCollect((long)0);
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->InsertItem(curItem, strValue);
}
var = pRentRecordset->GetCollect(_T("sname"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,1, strValue);
}
var = pRentRecordset->GetCollect(_T("ssex"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,2, strValue);
}
var = pRentRecordset->GetCollect(_T("sage"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,3, strValue);
}
var = pRentRecordset->GetCollect(_T("sdept"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,4, strValue);
}
pRentRecordset->MoveNext();
curItem++;
}
}else
{
MessageBox(_T("打开结果记录集失败."));
return;
}
}
catch(_com_error *e)
{
MessageBox(e->ErrorMessage());
return;
}
pRentRecordset->Close();
pRentRecordset = NULL;
使用记录集对象的过程步骤:
⑴ 定义 _RecordsetPtr 型变量;
⑵ 调用其 Open 方法,打开一个数据集;
⑶ 可以使用 GetCollect 方法获取记录的字段值;
⑷ 使用 Move 系列方法实现记录集的遍历;
⑸ 关闭记录集。
HRESULT hr;
_RecordsetPtr pRentRecordset;
hr = pRentRecordset.CreateInstance(__uuidof(Recordset));
if (FAILED(hr)) {
MessageBox(_T("创建记录集对象失败."));
return;
}
//调用其 Open 方法,打开一个数据集;
CString strSQL;
strSQL = _T("SELECT * FROM students");
try
{
hr = pRentRecordset->Open(_variant_t(strSQL), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
if (SUCCEEDED(hr))
{
_variant_t var;
CString strValue;
int curItem = 0;
while(!pRentRecordset->GetadoEof())
{
var = pRentRecordset->GetCollect((long)0);
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->InsertItem(curItem, strValue);
}
var = pRentRecordset->GetCollect(_T("sname"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,1, strValue);
}
var = pRentRecordset->GetCollect(_T("ssex"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,2, strValue);
}
var = pRentRecordset->GetCollect(_T("sage"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,3, strValue);
}
var = pRentRecordset->GetCollect(_T("sdept"));
if (var.vt != NULL)
{
strValue = (LPCTSTR)_bstr_t(var);
m_list->SetItemText(curItem,4, strValue);
}
pRentRecordset->MoveNext();
curItem++;
}
}else
{
MessageBox(_T("打开结果记录集失败."));
return;
}
}
catch(_com_error *e)
{
MessageBox(e->ErrorMessage());
return;
}
pRentRecordset->Close();
pRentRecordset = NULL;
使用记录集对象的过程步骤:
⑴ 定义 _RecordsetPtr 型变量;
⑵ 调用其 Open 方法,打开一个数据集;
⑶ 可以使用 GetCollect 方法获取记录的字段值;
⑷ 使用 Move 系列方法实现记录集的遍历;
⑸ 关闭记录集。