1.建立基于对话框的工程test
2.在stdafx.h中导入msado15.dll
#import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF", "adoEOF")
#endif // _AFX_NO_AFXCMN_SUPPORT
可以换成另一种方式调用:
#include <comdef.h>
#include "msado15.tlh"
要在工程目录下面加上msado15.tlh,msado15.tli
3.初始话COM,在test.cpp文件中
BOOL CTestApp::InitInstance()
{
AfxOleInit();//初始话
AfxEnableControlContainer();
4.进入按钮事件.
void CTestDlg::OnButton4()
{
// TODO: Add your control notification handler code here
_ConnectionPtr connection;
_RecordsetPtr recordset;
connection.CreateInstance( __uuidof(Connection));
recordset.CreateInstance(__uuidof(Recordset));
int ID = 0;
connection->Open("Provider=Microsoft.JET.OLEDB.4.0;Data Source=dictionary.mdb","","",adModeUnknown); //.mdb文件放到当前执行路径下(和DEBUG文件夹同目录)
recordset->Open("SELECT * FROM word",_variant_t((IDispatch *)connection,true),adOpenStatic,adLockOptimistic,adCmdText); //选择WORD表中的所有记录,黑体字的如果有问题可以改这个试试SELECT * FROM [word],具体原因本人才疏学浅,(不知道),请您赐教
while (!(recordset->adoEOF)) {
int id = recordset->GetCollect("ID").intVal;//文件中存在的ID字段
if ( id > ID)
ID = id;
recordset->MoveNext();
}
recordset->Close();
connection->Close();
}