void ReadRecordToList(CListCtrl m_Dlist,CString &szDb, char *sztab, _ConnectionPtr m_pConnect , _RecordsetPtr m_pRecordset)//
{
m_Dlist.DeleteAllItems();
// char *szDb;
// szDb="history";
FieldPtr ptr;
CString fieldname;
LPSTR fieldText;
long m_fieldscount,m_listcolumnscount;
int numline=0;
BOOL bn=OpenDatabase(m_pConnect,szDb,szServer,szUser,szPwd);
if (bn )
{
char szSQL[512];
sprintf(szSQL, sztab);
m_pRecordset.CreateInstance("ADODB.Recordset");
HRESULT hr = m_pRecordset->Open(szSQL,_variant_t(m_pConnect,true),adOpenDynamic,adLockPessimistic,adCmdText);
if(m_pRecordset->adoEOF)
{
AfxMessageBox("数据库表中没有记录信息!");
}
else
{
m_fieldscount=m_pRecordset->GetFields()->GetCount(); file://获得记录总数
m_listcolumnscount=m_fieldscount;
LV_ITEM lvitem;
lvitem.mask=LVIF_TEXT ;
lvitem.pszText="";
lvitem.iSubItem=0;
while(!m_pRecordset->adoEOF)
{
lvitem.iItem=numline;
m_Dlist.InsertItem(&lvitem);
file://读出数据写入到list中
for(long i=0;i<m_listcolumnscount;i++){
ptr=m_pRecordset->GetFields()->GetItem(i);
fieldname= (LPCTSTR)(_bstr_t)ptr->Name;
fieldText=fieldname.GetBuffer(fieldname.GetLength());
m_Dlist.SetItemText(numline,i,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect(fieldText));
}
numline++;
m_pRecordset->MoveNext();
}
}
if(m_pRecordset->State)
m_pRecordset->Close();
m_pRecordset.Release();
CloseDatabase(m_pConnect);
}
}
联系客服