数据库查询以及插入LIST列表函数

类别:VC语言 点击:0 评论:0 推荐:
void CStuDlg::List(CString sql)  //将SQL语句查询的结果显示在列表框中
{
  m_MyList.DeleteAllItems();
  int i=0;
  m_pRecordset.CreateInstance("ADODB.Recordset");
  m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)  theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  if(!m_pRecordset->adoEOF) //如果记录不为空
      {
while(!m_pRecordset->adoEOF)
{m_MyList.InsertItem(i,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Sno"));
m_MyList.SetItemText(i,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Sname"));
          m_MyList.SetItemText(i,2,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Ssex"));
m_MyList.SetItemText(i,3,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Sage"));
m_MyList.SetItemText(i,4,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Sdept"));
            i++;
m_pRecordset->MoveNext();
}
}
m_pRecordset->Close();

}

本文地址:http://com.8s8s.com/it/it17.htm