结合ADO、ADOX和MFC的文档/视图/框架架构创建和打开Access数据库[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 mynote 的 blog

本文描述了如何在mfc的文档/视图/框架架构中使用ado和adox来创建和打开数据库。

预备阅读
在阅读本文之前,建议先对com,数据库和mfc的文档/视图/框架有一个基本的了解。推荐阅读下列文章

mfc技术文章

tn025: document, view, and frame creation
微软知识库文章

q183606 activex data objects (ado) frequently asked questions
q169496 info: using activex data objects (ado) via #import in vc++
q317881 how to: create an access database using adox and visual c# .net
q252908 howto: create a table with primary key through adox
q201826 prb: error 3265 when you access properties collection
office vba参考

creating and modifying access tables
步骤
在计算机上安装mdac2.5以上版本
打开vc。首先,我们使用mfc应用程序向导创建一个标准的mdi程序,这里我为这个工程起名为passport,然后在stdafx.h中导入adox
#include <shlwapi.h>
#import "c:\program files\common files\system\ado\msado15.dll"  rename("eof","adoeof") rename("datatypeenum","adodatatypeenum")
#import "c:\program files\common files\system\ado\msadox.dll"  rename("eof", "adoxeof") rename("datatypeenum","adoxdatatypeenum")
#import "c:\program files\common files\system\ado\msjro.dll"

根据你的计算机上ado的安装路径,这里的路径可能有所不同。
在文档类中声明数据库连接 adodb::_connectionptr m_pconn;和记录集 adodb::_recordsetptr m_pset;,并且重载文档类的deletecontents() 、onnewdocument()和onopendocument()函数,用于断开数据库连接,创建数据库和表,以及打开现有的数据库。
(作者的抱怨:csdn文章中心该改改了,代码排版这么麻烦)
void cpassportdoc::deletecontents()
{
 try
 {
  if(m_pset){
   esrecordsetclose(m_pset);
  }
  if(m_pconn)
   if(m_pconn->state&adodb::adstateopen)
    m_pconn->close();
  m_pconn=null;
 }
 catch(_com_error &e){
  eserrprintprovidererror(m_pconn);
  eserrprintcomerror(e);
 }
 cdocument::deletecontents();
}bool cpassportdoc::onnewdocument()
{
 if (!cdocument::onnewdocument())
  return false;
 cfiledialog dlgfile(false, _t(".mdb"), null, ofn_hidereadonly | ofn_pathmustexist, _t("access 数据库 (*.mdb)|*.mdb|全部文件(*.*)|*.*||"));
 if (dlgfile.domodal() != idok)
  return false;
 cstring strdbpath=dlgfile.getpathname();
 if(!createdb(strdbpath))return false;
 //create
 cstring strconnect;
 
 strconnect.format(_t("provider=microsoft.jet.oledb.4.0;data source=%s"),strdbpath);
 colevariant connect(strconnect);
 // todo: add reinitialization code here
 // (sdi documents will reuse this document)
 try{
  m_pconn.createinstance(_t("adodb.connection"));
  m_pset.createinstance(_t("adodb.recordset"));
  m_pconn->putcommandtimeout(30);
  m_pconn->putconnectiontimeout(30);
  m_pconn->put_cursorlocation(adodb::aduseclient);
  m_pconn->open(_bstr_t(strconnect),_bstr_t(),_bstr_t(),adodb::adconnectunspecified);
  ::esrecordsetopen(_t("passport"),m_pconn,m_pset);
  setpathname(strdbpath);
  return true;
 }
 catch(_com_error &e){
  eserrprintprovidererror(m_pconn);
  eserrprintcomerror(e);
 }
 catch(...){
 }
 m_pconn=null;
 return false;
}
bool cpassportdoc::onopendocument(lpctstr lpszpathname)
{
 if (!cdocument::onopendocument(lpszpathname))
  return false;
 adodb::_connectionptr tempconnn;
 cstring strconnect;
 cstring strdbpath=lpszpathname;
 strconnect.format(_t("provider=microsoft.jet.oledb.4.0;data source=%s"),strdbpath);
 colevariant connect(strconnect);
 // todo: add reinitialization code here
 // (sdi documents will reuse this document)
 try{
  tempconnn.createinstance(_t("adodb.connection"));
  tempconnn->putcommandtimeout(30);
  tempconnn->putconnectiontimeout(30);
  tempconnn->put_cursorlocation(adodb::aduseclient);
  tempconnn->open(_bstr_t(strconnect),_bstr_t(),_bstr_t(),adodb::adconnectunspecified);
  setpathname(strdbpath);
  m_pconn=tempconnn;
  m_pset=null;
  m_pset.createinstance(_t("adodb.recordset"));
  ::esrecordsetopen(_t("passport"),m_pconn,m_pset);
  updateallviews(null,updatehintrefresh);
  return true;
 }
 catch(_com_error &e){
  eserrprintprovidererror(tempconnn);
  eserrprintcomerror(e);
 }
 catch(...){
 }
 return false;
}

本文关键:结合ADO、ADOX和MFC的文档/视图/框架架构创建和打开Access数据库
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top