VS2015 win10 64位系统
1.引用msado15.dll库文件(将此文件放在生成目录下)
链接:https://pan.baidu.com/s/1xg1jCcxxU9HRx84F7qYR4w 提取码:3wps
2.在头文件中添加
#import "../Debug/msado15.dll" no_namespace rename("EOF", "adoEOF")
3.定义变量
_ConnectionPtr m_pConnection;//连接access数据库的链接对象
_RecordsetPtr m_pRecordset;//结果集对象
4.在初始化函数中
连接数据库
try {
CoInitialize(NULL);
m_pConnection = _ConnectionPtr(__uuidof(Connection));
m_pConnection->Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = ../Debug/DRDB_cn.mdb;Persist Security Info=False;Jet OLEDB:Database Password= Pxray", "", "", adConnectUnspecified);
}
catch (_com_error e) {
AfxMessageBox(_T("数据库连接失败!"));
return;
}
5.查询数据库中某个表格内容
try
{
_variant_t RecordsAffected;
m_pRecordset.CreateInstance(__uuidof(Recordset));
CString search_sql;
search_sql = "SELECT * FROM PartPosition";
m_pRecordset = m_pConnection->Execute(search_sql.AllocSysString(), NULL, adCmdText);
while (!m_pRecordset->adoEOF)
{
CString id;
id = m_pRecordset->GetCollect("ID");
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
}
catch (_com_error e)
{
AfxMessageBox(_T("表格读取失败!"));
return 1;
}