users",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,
adCmdText);
m_bSuccess = TRUE;
while(!m_pRecordset->adoEOF)
{
vID = m_pRecordset->GetCollect("id");
vUsername = m_pRecordset->GetCollect("username");
vAge = m_pRecordset->GetCollect("age");
vBirthday = m_pRecordset->GetCollect("birthday");
nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vID);
m_userlist.SetItem(nItem,1,1,(_bstr_t)vUsername,NULL,0,0,0);
m_userlist.SetItem(nItem,2,1,(_bstr_t)vAge,NULL,0,0,0);
m_userlist.SetItem(nItem,3,1,(_bstr_t)vBirthday,NULL,0,0,0);
m_pRecordset->MoveNext();
}
}
catch(_com_error e)///捕捉异常
{
AfxMessageBox("读取数据库失败!");///显示错误信息
}
……
}
12) 在类 CAdoShowDlg 中添加“添加”和“删除”按钮响应函数的代码,其代码如下:
void CAdoShowDlg::OnAddButton()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(m_strUsername.GetLength()>0)
{
m_pRecordset->AddNew();
m_nCurrentSel = m_userlist.InsertItem(0xffff,"");
SaveData();///保存数据
m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|
LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
m_userlist.SetHotItem(m_nCurrentSel);
m_userlist.SetFocus();
}
else
AfxMessageBox("请输入用户名");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CAdoShowDlg::OnDeleteButton()
{
// TODO: Add your control notification handler code here