vc 使用ado访问access数据库

来源:互联网 发布:淘宝卖食品需要哪三证 编辑:程序博客网 时间:2024/05/01 09:14

1.定义_ConnectionPtrm_pConnection;

在CFaceDetectApp::InitInstance()中

AfxOleInit();

m_pConnection.CreateInstance(__uuidof(Connection));

 

// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,

// 因为它有时会经常出现一些想不到的错误。jingzhou xu

try                 

{

// 打开本地Access库Demo.mdb

m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb","","",adModeUnknown);

}

catch(_com_error e)

{

AfxMessageBox("数据库连接失败,确认数据库Demo.mdb是否在当前路径下!");

return FALSE;

 

2 在CFaceDetectApp::ExitInstance() 中

 

// TODO: Add your specialized code here and/or call the base class

// 关闭ADO连接状态

if(m_pConnection->State)

m_pConnection->Close();

m_pConnection= NULL;

 

3 在oninitdialog中

 

m_pRecordset.CreateInstance(__uuidof(Recordset));

m_pRecordset1.CreateInstance(__uuidof(Recordset));

    m_pRecordset2.CreateInstance(__uuidof(Recordset));

 

try//打开数据库

{

m_pRecordset->Open("SELECT * FROM db1",                // 查询DemoTable表中所有字段

theApp.m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针

adOpenDynamic,

adLockOptimistic,

adCmdText);

}

catch(_com_error *e)

{

AfxMessageBox(e->ErrorMessage());

}

 

 

原创粉丝点击