⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap10_8.htm

📁 非常好的应用vc++6.0进行相关网络程序开发的教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<title>10.8 DAO</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>

<body link="#3973DE" alink="#3973DE">
<font SIZE="5"><b><div align="center"><center>

<table border="0" width="615" cellspacing="0" cellpadding="0">
  <tr>
    </b><td><div align="center"><center><table border="0" width="615" cellpadding="0" cellspacing="0" height="20">
      <tr>
        <td width="377" bgcolor="#15397D" height="20"></td>
        <td width="238" bgcolor="#000000" height="20"><p align="right"></font><span style="text-decoration: none"><a href="../../index.htm"><font color="#FFFFFF">电脑报Visual 
        C++网络教程</font></a></span></td>
      </tr>
    </table>
    </center></div><font FACE="Times New Roman" SIZE="3"><b><p ALIGN="CENTER"></b></font><font FACE="Times New Roman" color="#3973DE" size="5">10.8 DAO</font><font FACE="Times New Roman" size="5"></p>
    </font><font FACE="Times New Roman" SIZE="3"><b><p ALIGN="JUSTIFY"></b></font><font color="#3973DE" FACE="Times New Roman" SIZE="3">10.8.1 </font><font SIZE="3" color="#3973DE">什么是</font><font color="#3973DE" FACE="Times New Roman" SIZE="3">DAO</font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY">DAO(Database Access Object)</font><font SIZE="3">使用</font><font FACE="Times New Roman" SIZE="3">Microsoft Jet</font><font SIZE="3">数据库引擎来访问数据库。</font><font FACE="Times New Roman" SIZE="3">Microsoft Jet</font><font SIZE="3">为象</font><font FACE="Times New Roman" SIZE="3">Access</font><font SIZE="3">和</font><font FACE="Times New Roman" SIZE="3">Visual Basic</font><font SIZE="3">这样的产品提供了数据引擎。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY"></font><font SIZE="3">与</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">一样,</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">提供了一组</font><font FACE="Times New Roman" SIZE="3">API</font><font SIZE="3">供编程使用。</font><font FACE="Times New Roman" SIZE="3">MFC</font><font SIZE="3">也提供了一组</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">类,封装了底层的</font><font FACE="Times New Roman" SIZE="3">API</font><font SIZE="3">,从而大大简化了程序的开发。利用</font><font FACE="Times New Roman" SIZE="3">MFC</font><font SIZE="3">的</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">类,用户可以编写独立于</font><font FACE="Times New Roman" SIZE="3">DBMS</font><font SIZE="3">的应用程序。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY">DAO</font><font SIZE="3">是从</font><font FACE="Times New Roman" SIZE="3">Visual C++4.0</font><font SIZE="3">版开始引入的。一般地讲,</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">类提供了比</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">类更广泛的支持。一方面,只要有</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">驱动程序,使用</font><font FACE="Times New Roman" SIZE="3">Microsoft Jet</font><font SIZE="3">的</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">就可以访问</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">数据源。另一方面,由于</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">是基于</font><font FACE="Times New Roman" SIZE="3">Microsoft Jet</font><font SIZE="3">引擎的,因而在访问</font><font FACE="Times New Roman" SIZE="3">Access</font><font SIZE="3">数据库</font><font FACE="Times New Roman" SIZE="3">(</font><font SIZE="3">即</font><font FACE="Times New Roman" SIZE="3">*.MDB</font><font SIZE="3">文件</font><font FACE="Times New Roman" SIZE="3">)</font><font SIZE="3">时具有很好的性能。</font><font FACE="Times New Roman" SIZE="3"></p>
    <b><p ALIGN="JUSTIFY"></b></font><font color="#3973DE" FACE="Times New Roman" SIZE="3">10.8.2 
    DAO</font><font color="#3973DE"><font SIZE="3">和</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">的相似之处</font></font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY">DAO</font><font SIZE="3">类与</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">类相比具有很多相似之处,这主要有下面几点:</p>
    <ul>
      <li><p ALIGN="JUSTIFY">二者都支持对各种<font FACE="Times New Roman" SIZE="3">ODBC</font>数据源的访问。虽然二者使用的数据引擎不同,但都可以满足用户编写独立于<font FACE="Times New Roman" SIZE="3">DBMS</font>的应用程序的要求。</p>
      </li>
      <li><font FACE="Times New Roman" SIZE="3"><p ALIGN="JUSTIFY">DAO</font>提供了与<font FACE="Times New Roman" SIZE="3">ODBC</font>功能相似的<font FACE="Times New Roman" SIZE="3">MFC</font>类。例如,<font FACE="Times New Roman" SIZE="3">DAO</font>的<font FACE="Times New Roman" SIZE="3">CDaoDatabase</font>类对应<font FACE="Times New Roman" SIZE="3">ODBC</font>的<font FACE="Times New Roman" SIZE="3">CDatabase</font>类,<font FACE="Times New Roman" SIZE="3">CDaoRecordset</font>对应<font FACE="Times New Roman" SIZE="3">CRecordset</font>,<font FACE="Times New Roman" SIZE="3">CDaoRecordView</font>对应<font FACE="Times New Roman" SIZE="3">CRecordView</font>,<font FACE="Times New Roman" SIZE="3">CDaoException</font>对应<font FACE="Times New Roman" SIZE="3">CDBException</font>。这些对应的类功能相似,它们的大部分成员函数都是相同的。</p>
      </li>
      <li><font FACE="Times New Roman" SIZE="3"><p ALIGN="JUSTIFY">AppWizard</font>和<font FACE="Times New Roman" SIZE="3">ClassWizard</font>对使用<font FACE="Times New Roman" SIZE="3">DAO</font>和<font FACE="Times New Roman" SIZE="3">ODBC</font>对象的应用程序提供了类似的支持。</font></p>
      </li>
    </ul>
    <p><font SIZE="3"> </font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY"></font><font SIZE="3">由于</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">和</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">类的许多方面都比较相似,因此只要用户掌握了</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">,就很容易学会使用</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">。实际上,用户可以很轻松地把数据库应用程序从</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">移植到</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">。</font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY">Visual C++</font><font SIZE="3">随盘提供了一个名为</font><font FACE="Times New Roman" SIZE="3">DaoEnrol</font><font SIZE="3">的例子,该例实际上是</font><font FACE="Times New Roman" SIZE="3">Enroll</font><font SIZE="3">的一个</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">版本。读者可以打开</font><font FACE="Times New Roman" SIZE="3">DaoEnrol</font><font SIZE="3">工程看一看,它的源代码与</font><font FACE="Times New Roman" SIZE="3">Enroll</font><font SIZE="3">的极为相似。读者可以按照建立</font><font FACE="Times New Roman" SIZE="3">Enroll</font><font SIZE="3">的步骤来建立</font><font FACE="Times New Roman" SIZE="3">DaoEnrol</font><font SIZE="3">,其中只有若干个地方有差别,这主要有以下几点:</p>
    <blockquote>
      <p ALIGN="JUSTIFY">选取的数据源不同。在用<font FACE="Times New Roman" SIZE="3">AppWizard</font>创建<font FACE="Times New Roman" SIZE="3">DaoEnrol</font>时,以及在用<font FACE="Times New Roman" SIZE="3">ClassWizard</font>创建<font FACE="Times New Roman" SIZE="3">CDaoRecordset</font>类的派生类时,在<font FACE="Times New Roman" SIZE="3">Database 
      Options</font>对话框中应该选择<font FACE="Times New Roman" SIZE="3">DAO</font>而不是<font FACE="Times New Roman" SIZE="3">ODBC</font>。而且<font FACE="Times New Roman" SIZE="3">DAO</font>的数据源是通过选择一个<font FACE="Times New Roman" SIZE="3">.MDB</font>文件来指定的,即点击“<font FACE="Times New Roman" SIZE="3">...</font><font SIZE="3">”</font>按钮后在文件对话框中选择要访问的<font FACE="Times New Roman" SIZE="3">.MDB</font>文件。</p>
      <p ALIGN="JUSTIFY">记录集的缺省类型不同。<font FACE="Times New Roman" SIZE="3">ODBC</font>记录集的缺省类型是快照<font FACE="Times New Roman" SIZE="3">(Snapshot)</font>,而<font FACE="Times New Roman" SIZE="3">DAO</font>则是动态集<font FACE="Times New Roman" SIZE="3">(Dynaset)</font>。</p>
      <p ALIGN="JUSTIFY">参数化的方式不同。<font FACE="Times New Roman" SIZE="3">DAO</font>记录集的<font FACE="Times New Roman" SIZE="3">m_strFilter</font>和<font FACE="Times New Roman" SIZE="3">m_strSort</font>中的参数不是“<font FACE="Times New Roman" SIZE="3">?</font>”号,而是一个有意义的参数名。例如,在下面的过滤器中有一个名为<font FACE="Times New Roman" SIZE="3">CourseIDParam</font>的参数。<font SIZE="3"><br>
      </font><font FACE="Times New Roman" SIZE="3">m_pSet-&gt;m_strFilter =&quot;CourseID = 
      CourseIDParam&quot;;<br>
      </font>在<font FACE="Times New Roman" SIZE="3">DoFieldExchange</font>函数中,有下面两行:<font SIZE="3"><br>
      </font><font FACE="Times New Roman" SIZE="3">pFX-&gt;SetFieldType(CDaoFieldExchange::param);<br>
      DFX_Text(pFX, _T(&quot;CourseIDParam&quot;), m_strCourseIDParam);<br>
      DFX</font>函数的第二个参数也是<font FACE="Times New Roman" SIZE="3">CourseIDParam</font>。</p>
      <p ALIGN="JUSTIFY">处理异常的方式不同。例如,在删除记录时,对异常的处理如下所示:</p>
      </font><font FACE="Times New Roman" SIZE="3"><p ALIGN="JUSTIFY">try</p>
      <p ALIGN="JUSTIFY">{</p>
      <p ALIGN="JUSTIFY">m_pSet-&gt;Delete();</p>
      <p ALIGN="JUSTIFY">}</p>
      <p ALIGN="JUSTIFY">catch(CDaoException* e)</p>
      <p ALIGN="JUSTIFY">{</p>
      <p ALIGN="JUSTIFY">AfxMessageBox(e-&gt;</p>
      <p ALIGN="JUSTIFY">m_pErrorInfo-&gt;m_strDescription);</p>
      <p ALIGN="JUSTIFY">e-&gt;Delete();</p>
      <p ALIGN="JUSTIFY">}</p>
    </blockquote>
    <p ALIGN="JUSTIFY"></font><font SIZE="3">除了上述差别外,</font><font FACE="Times New Roman" SIZE="3">AppWizard</font><font SIZE="3">和</font><font FACE="Times New Roman" SIZE="3">ClassWizard</font><font SIZE="3">也隐藏了一些细微的不同之处,例如,</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">记录集是使用是</font><font FACE="Times New Roman" SIZE="3">DFX</font><font SIZE="3">数据交换机制</font><font FACE="Times New Roman" SIZE="3">(DAO record field exchange)</font><font SIZE="3">而不是</font><font FACE="Times New Roman" SIZE="3">RFX</font><font SIZE="3">,在</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">记录集的</font><font FACE="Times New Roman" SIZE="3">DoFieldExchange</font><font SIZE="3">中使用的是</font><font FACE="Times New Roman" SIZE="3">DFX</font><font SIZE="3">函数而不是</font><font FACE="Times New Roman" SIZE="3">RFX</font><font SIZE="3">函数。</font><font FACE="Times New Roman" SIZE="3"></p>
    <b><p ALIGN="JUSTIFY"></b></font><font color="#3973DE" FACE="Times New Roman" SIZE="3">10.8.3 
    DAO</font><font SIZE="3" color="#3973DE">的特色</font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY">DAO</font><font SIZE="3">可以通过</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">驱动程序访问</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">数据源。但</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">是基于</font><font FACE="Times New Roman" SIZE="3">Microsoft Jet</font><font SIZE="3">引擎的,通过该引擎,</font><font FACE="Times New Roman" SIZE="3">DAO</font><font SIZE="3">可以直接访问</font><font FACE="Times New Roman" SIZE="3">Access</font><font SIZE="3">、</font><font FACE="Times New Roman" SIZE="3">FoxPro</font><font SIZE="3">、</font><font FACE="Times New Roman" SIZE="3">dBASE</font><font SIZE="3">、</font><font FACE="Times New Roman" SIZE="3">Paradox</font><font SIZE="3">、</font><font FACE="Times New Roman" SIZE="3">Excel</font><font SIZE="3">和</font><font FACE="Times New Roman" SIZE="3">Lotus WK</font><font SIZE="3">等数据库。</font><font FACE="Times New Roman" SIZE="3">CDaoDatabase</font><font SIZE="3">类可以直接与这些数据库进行连接,而不必在</font><font FACE="Times New Roman" SIZE="3">ODBC</font><font SIZE="3">管理器中注册</font><font FACE="Times New Roman" SIZE="3">DSN</font><font SIZE="3">。例如,下面的代码用来打开一个</font><font FACE="Times New Roman" SIZE="3">FoxPro</font><font SIZE="3">数据库:</font><font FACE="Times New Roman" SIZE="3"></p>
    <p ALIGN="JUSTIFY">CDaoDatabase daoDb;</p>
    <p ALIGN="JUSTIFY">daoDb.Open( </font><font SIZE="3">“”</font><font FACE="Times New Roman" SIZE="3">,FALSE,FALSE,&quot;FoxPro 2.5;DATABASE=c:\\zyf&quot;);</p>
    <p ALIGN="JUSTIFY">CDaoDatabase::Open</font><font SIZE="3">函数用来连接某个数据库,该函数的声明为:</p>
    <blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -