1、VC++ mfc 连接2010 Access 数据库,查询之后显示效果如下图:
2、步骤如下:
a)头文件中添加;
#include <atlconv.h>
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
如下图:
b)程序信息如下图:
c) 代码如下图:
// TODO: Add your control notification handler code here
try {
m_list.AddString("1");
USES_CONVERSION;
::CoInitialize(NULL);
_ConnectionPtr con(_uuidof(Connection));
_RecordsetPtr rst(_uuidof(Recordset));
_CommandPtr cmd(_uuidof(Command));
m_list.AddString("2");
con->Open(("Provider=Microsoft.Ace.Oledb.12.0;Data Source=D:\\tms.accdb"),"","",adModeUnknown);
m_list.AddString("3");
m_list.AddString("4");
//rst->Open(_variant_t("select * from bt_user"),_variant_t((IDispatch*) con),adOpenDynamic,adLockOptimistic,-1);
rst->Open("select * from tb_user",_variant_t((IDispatch*) con),adOpenDynamic,adLockOptimistic,-1);
m_list.AddString("5");
while(!rst->adoEOF){
m_list.AddString(W2A(rst->GetCollect(_variant_t("user_name")).bstrVal));
rst->MoveNext();
}
} catch(_com_error e){
AfxMessageBox(e.ErrorMessage());//显示错误信息
}