首先,在StdAfx.h文件中,添加库:
#import "c:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF" ,"adoEOF")
其次,创建专属于数据库读取函数:
//.h中
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
void SetDataBase(CString sql);
//.cpp中
void CTextboxListDlg::SetDataBase(CString sql)
{
::CoInitialize(NULL);
m_pConnection=NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->ConnectionString="uid=;pwd=;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=mrdb.mdb;";
m_pConnection->Open(L"",L"",L"",adCmdUnspecified);
CString text;
text.Format("select * from info where xm like '%s%%'",sql);
m_pRecordset=m_pConnection->Execute((_bstr_t)text,NULL,adCmdText);
if(m_pRecordset->GetRecordCount()==0)
{m_tiplist.ShowWindow(SW_HIDE);}
int i=0;
while(!m_pRecordset->adoEOF)
{
xm=(char*)(_bstr_t)m_pRecordset->GetCollect("xm");
xb=(char*)(_bstr_t)m_pRecordset->GetCollect("xb");
csrq=(char*)(_bstr_t)m_pRecordset->GetCollect("csrq");
gzdw=(char*)(_bstr_t)m_pRecordset->GetCollect("gzdw");
yddh=(char*)(_bstr_t)m_pRecordset->GetCollect("yddh");
gddh=(char*)(_bstr_t)m_pRecordset->GetCollect("gddh");
m_tiplist.InsertItem(i,"");
m_tiplist.SetItemText(i,0,xm);
m_tiplist.SetItemText(i,1,xb);
m_tiplist.SetItemText(i,2,csrq);
m_tiplist.SetItemText(i,3,gzdw);
m_tiplist.SetItemText(i,4,yddh);
m_tiplist.SetItemText(i,5,gddh);
i+=1;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pConnection->Close();
m_pRecordset=NULL;
m_pConnection=NULL;
::CoUninitialize();
}
最后,不要忘记用List列表控件把数据导出来!
// TODO: Add extra initialization here
m_tiplist.ModifyStyle(0L,LVS_REPORT);
m_tiplist.ModifyStyle(0L,LVS_SHOWSELALWAYS);
m_tiplist.ModifyStyle(0L,LVS_NOSORTHEADER);
m_tiplist.SetExtendedStyle(LVS_EX_GRIDLINES);
m_tiplist.InsertColumn(0,"姓名");
m_tiplist.InsertColumn(1,"性别");
m_tiplist.InsertColumn(2,"出生日期");
m_tiplist.SetColumnWidth(0,50);
m_tiplist.SetColumnWidth(1,50);
m_tiplist.SetColumnWidth(2,100);
m_edobj.MoveWindow(15,15,150,21);
如法炮制吧!