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

📄 csdn_文档中心_用mfcodbc编写数据库应用程序.htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 2 页
字号:
    </TD>
    <TD align=middle width=620>
      <TABLE bgColor=#eeeeee border=0 cellPadding=0 cellSpacing=0 width=600>
        <TBODY>
        <TR bgColor=#ffffff>
          <TD align=middle height=10 width=50></TD>
          <TD align=right><A href="http://www.csdn.net/">CSDN</A> - <A 
            href="http://www.csdn.net/develop/">文档中心</A> - <FONT 
            color=#003399>Visual C++</FONT>&nbsp;&nbsp;&nbsp;&nbsp; </TD></TR>
        <TR>
          <TD align=middle height=5></TD>
          <TD align=middle width=500></TD></TR>
        <TR>
          <TD align=middle bgColor=#003399 height=10><FONT 
            color=#ffffff>标题</FONT></TD>
          <TD><B>&nbsp;&nbsp;&nbsp;&nbsp;用MFCODBC编写数据库应用程序</B>&nbsp;&nbsp;&nbsp;&nbsp;zzh(原作) 
          </TD></TR>
        <TR>
          <TD align=middle height=5></TD>
          <TD align=middle width=500></TD></TR>
        <TR>
          <TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
          <TD width=500>&nbsp;&nbsp;&nbsp;&nbsp;用MFCODBC编写数据库应用程序</TD></TR>
        <TR>
          <TD align=middle height=5></TD>
          <TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
      <TABLE border=0 width=600>
        <TBODY>
        <TR>
          <TD align=left><BR>1、 
            使用MFC访问ODBC数据源<BR>EMIAL:zzh1415@21cn.com<BR>3.1&nbsp;&nbsp;&nbsp;&nbsp;概述<BR>VisualC++的MFC类库定义了几个数据库类。在利用ODBC编程时,经常要使用到CDatabase(数据库类),CRecordSet(记录集类)和CRecordView(可视记录集类)。 
            其中: 
            <BR>CDatabase类对象提供了对数据源的连接,通过它你可以对数据源进行操作。<BR>CRecordView类对象能以控制的形式 
            显示数据库记录。这个视图是直接连到一个CRecordSet对象的表视图。<BR>CRecordSet类对象提供了从数据源 
            中提取出的记录集。CRecordSet对象通常用于两种形式: 动态行集(dynasets)和快照集(snapshots)。动态行集能保 
            持与其他用户所做的更改保持同步。快照集则是数据的一个静态视图。每一种形式在记录集被打开时都提供一组记录,所不同的是,当你在一个动态行集里滚 
            动到一条记录时,由其他用户或是你应用程序中的其他记录集对该记录所做的更改会相应地显示出来。<BR><BR>Visual 
            C++提供了几种记录集,可以用来定制应用程序的工作方式。查看这些不同选项的最快方式要兼顾速度和特征。你会发现,在很多情况下,如果想添加特征,就必须付出程序执行速度降低的代价。下面告诉你一些可以自由支配的记录集选项。更重要的是,要告诉你从这个选项可以获得更快的速度还是更多的特征。<BR>1、Snapshot(快照) 
            这个选项要Visual 
            C++在一次快照中下载整个查询。换言之,及时快速地给数据库内容拍照,并把它作为未来工作的基础。这种方法有三个缺点。第一,你看不到别人在网络上做的更新,这可能意味着你的决定是建立在老信息的基础上。第二,一次就下载所有这些记录,这意味着在下载期间给网络增加了沉重的负担。第三,记录下载时用户会结束等待,这意味着网络的呼叫性能变得更低。然而这种方法也有两个优点。第一,记录一旦被下载,该工作站所需的网络活动几乎就没有了棗这为其它请求释放了带宽。总之,你会看到网络的吞吐量增大了。第二,因为所有被申请的记录都在用户的机器上,所以用户实际上会得到应用程序更佳的总体性能。你可能想把快照的方法限制在较小的数据库上使用,原因在于快照适用于用户请求信息而不适用于数据编辑会话。<BR>2、Dynaset(动态集) 
            使用这个选项时,Visual 
            C++创建指向所请求的每个记录的实际指针。另外,只有填充屏幕时实际需要的记录是从服务器上下载来的。这种方法的好处很明显。几乎马上就能在屏幕上看到记录。而且还会看到其它用户对数据库所做的更改。最后,其它用户也会看到你做的更改,因为动态集在你更改记录时被上载到服务器上。很明显,这种方法要求对服务器的实时访问,它减小了网络总吞吐量并降低了应用程序的性能。这个选项适合于创建用户要花费很多时间来编辑数据的应用程序。同时,它也是大型数据库的最佳选择,原因在于只需下载用户实际需要的信息。&nbsp; 
            <BR>3.2&nbsp;&nbsp;&nbsp;&nbsp;应用ODBC编程<BR>可以应用AppWizard来建立一个ODBC的应用程序框架,也可以直接使用ODBC来进行数据库编程,这时,应包括头文件afxdb.h。<BR>应用ODBC编程两个最重要的类是CDatabase和CRecordSet,但在应用程序中,不应直接使用CRecordSet类,而必须从CRecordSet类产生一个导出类,并添加相应于数据库表中字段的成员变量。随后,重载CRecordset类的成员函数DoFieldExchange,该函数通过使用RFX函数完成数据库字段与记录集域数据成员变量的数据交换,RFX函数同对话框数据交换(DDX)机制相类似,负责完成数据库与成员变量间的数据交换。<BR><BR>下面举例说明在VisualC++环境中ODBC 
            的编程技巧:<BR>3.21&nbsp;&nbsp;&nbsp;&nbsp;数据库连接<BR>在CRecordSet类中定义了一个成员变 
            量m_pDatabase:<BR>CDatabase 
            *m_pDatabase;<BR>它是指向对象数据库类的指针。如果在CRecordSet类对象调用Open()函数之前,将一个已经打开的CDatabase类对象指针传给m_pDatabase,就能共享相同 
            的CDatabase类对象。如:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;CDatabase 
            m_db;<BR>&nbsp;&nbsp;&nbsp;&nbsp;CRecordSet 
            m_set1,m_set2;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_db.Open(_T("Super_ES"));&nbsp;&nbsp;&nbsp;&nbsp;// 
            建 立ODBC 连 
            接<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_set1.m_pDatabase=&amp;m_db;&nbsp;&nbsp;&nbsp;&nbsp;//m_set1 
            复 用m_db 对 
            象<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_set2.m_pDatabse=&amp;m_db;&nbsp;&nbsp;&nbsp;&nbsp;// 
            m_set2 复 用m_db 对 象<BR>或如下:<BR><BR>Cdatabase 
            db;<BR>db.Open(“Database”);&nbsp; //建立ODBC连接<BR>CrecordSet 
            m_set(&amp;db);&nbsp; 
            //构造记录集对象,使数据库指向db<BR>3.22&nbsp;&nbsp;&nbsp;&nbsp;查询记录<BR>查询记录使用CRecordSet::Open()和 
            CRecordSet::Requery()成员函数。在使用CRecordSet类对象之前,必须使用 
            CRecordSet::Open()函数来获得有效的记录集。一旦已经使用过CRecordSet::Open() 
            函数,再次查询时就可以应用CRecordSet::Requery()函数。在调 
            用CRecordSet::Open()函数时,如果已经将一个已经打开的CDatabase 
            对象指针传给CRecordSet类对象的m_pDatabase成员变量,则使 
            用该数据库对象建立ODBC连接;否则如果m_pDatabase为空指 针,就新建一个CDatabase类对象并使其与缺省的数据源 
            相连,然后进行CRecordSet类对象的初始化。缺省数据源 
            由GetDefaultConnect()函数获得。你也可以提供你所需要的SQL 
            语句,并以它来调用CRecordSet::Open()函数,例如:<BR>m_Set.Open(AFX_DATABASE_USE_DEFAULT,strSQL);<BR>如果没有指定参数,程序则使 
            用缺省的SQL语句,即对在GetDefaultSQL()函数中指定的SQL语 句进行操作:<BR><BR>CString 
            CTestRecordSet::GetDefaultSQL()<BR>{return 
            _T("[BasicData],[MainSize]");}<BR>对于GetDefaultSQL()函数返回的表名, 
            对应的缺省操作是SELECT语句,即:<BR>SELECT * FROM 
            BasicData,MainSize<BR><BR>查询过程中也可以利用CRecordSet的 
            成员变量m_strFilter和m_strSort来执行条件查询和结果排序。m_strFilter 
            为过滤字符串,存放着SQL语句中WHERE后的条件串;m_strSort 为排序字符串,存放着SQL语句中ORDERBY后的字符串。 
            如:<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_Set.m_strFilter="TYPE='电动机'";<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_Set.m_strSort="VOLTAGE";<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_Set.Requery();<BR>&nbsp;&nbsp;&nbsp;&nbsp;对应的SQL语句为:<BR>&nbsp;&nbsp;&nbsp;&nbsp;SELECT 
            * FROM BasicData,MainSize <BR>&nbsp;&nbsp;&nbsp;&nbsp;WHERE 
            TYPE='电动机'<BR>&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY 
            VOLTAGE<BR>除了直接赋值给m_strFilter以外,还 可以使用参数化。利用参数化可以更直观,更方便地 
            完成条件查询任务。使用参数化的步骤如下:<BR><BR>(1).声明参变量:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Cstring 
            p1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Float 
            p2;<BR>(2).在构造函数中初始化参变量<BR>&nbsp;&nbsp;&nbsp;&nbsp;p1=_T("");<BR>&nbsp;&nbsp;&nbsp;&nbsp;p2=0.0f;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_nParams=2;<BR>(3).将参变量与对应列绑定<BR>&nbsp;&nbsp;&nbsp;&nbsp;pFX-&gt;SetFieldType(CFieldExchange::param)<BR>&nbsp;&nbsp;&nbsp;&nbsp;RFX_Text(pFX,_T("P1"),p1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;RFX_Single(pFX,_T("P2"),p2);<BR>完成以上步骤之后就可以利用 
            参变量进行条件查询了:<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;m_strFilter="TYPE=?ANDVOLTAGE=?";<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;p1="电动机";<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;p2=60.0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;Requery();<BR>参变量的值按绑定的顺序替换 
            查询字串中的“?”适配符。<BR>如果查询的结果是多条记录的 
            话,可以用CRecordSet类的函数Move(),MoveNext(),MovePrev(),MoveFirst() 
            和MoveLast()来移动光标。<BR>3.23&nbsp;&nbsp;&nbsp;&nbsp;增加记录<BR>增加记录使用AddNew()函数,要求数据库必须是以允许增加的方式打开:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;AddNew();&nbsp;&nbsp;&nbsp;&nbsp;//在表的末尾增加新记录<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;SetFieldNull(&amp;(m_pSet-&gt;m_type),FALSE);&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp; 
            &nbsp; 
            m_pSet-&gt;m_type="电动机";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//输入新的字段值<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;&nbsp;&nbsp;&nbsp;&nbsp;Update();&nbsp;&nbsp;&nbsp;&nbsp;//将新记录存入数据库<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;Requery();&nbsp;&nbsp;&nbsp;&nbsp;//重建记录集<BR>3.24&nbsp;&nbsp;&nbsp;&nbsp;删除记录<BR>直接使用Delete()函数,并且在调用Delete() 
            函数之后不需调用Update()函数:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;Delete();<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(!m_pSet-&gt;IsEOF())<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;MoveNext();<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;MoveLast();<BR><BR>3.25&nbsp;&nbsp;&nbsp;&nbsp;修改记录<BR>修改记录使用Edit()函数:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;Edit();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//修改当前记录<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;m_type="发电机";&nbsp;&nbsp;&nbsp;&nbsp;//修改当前记录字段值<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;Update();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//将修改结果存入数据库<BR>&nbsp;&nbsp;&nbsp;&nbsp;m_pSet-&gt;Requery();<BR>3.26&nbsp;&nbsp;&nbsp;&nbsp;统计记录<BR>统计记录用来统计记录集的总数。可以先声明一个CRecordset对象m_pSet。再绑定一个变量m_lCount,用来统计记录总数。执行如下语句:<BR>m_pSet-&gt;Open(“Select 
            Count(*) from 表名 where 
            限定条件”);<BR>RecordCount=m_pSet-&gt;m_lCount;<BR>m_pSet-&gt;Close();<BR>RecordCount即为要统计的记录数。<BR>或如下:<BR>CRecordset 
            m_Set(&amp;db);&nbsp; //db 为CDatabase对象<BR>CString 
            strValue;<BR>m_Set.Open(Select count(*) from 表名 where 
            限定条件”);<BR>m_pSet.GetFieldValue((int)0,strValue);<BR>long 
            count=atol(strValue);<BR>m_set.Close();<BR>count为记录总数。<BR>3.27&nbsp;&nbsp;&nbsp;&nbsp;执行SQL语句<BR>虽然通过CRecordSet类,我们可以完成 
            大多数的查询操作,而且在CRecordSet::Open()函数中也可以 提供SQL语句,但是有的时候我们还想进行一些其他操 
            作,例如建立新表,删除表,建立新的字段等等,这 时就需要使用到CDatabase类的直接执行SQL语句的机制。通 
            过调用CDatabase::ExecuteSQL()函数来完成SQL语句的直接执行:<BR>如下代码所示<BR>&nbsp;&nbsp;&nbsp;&nbsp;BOOL 
            CDB::ExecuteSQLAndReportFailure(const CString&amp; 
            strSQL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TRY<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_pdb-&gt;ExecuteSQL(strSQL);&nbsp;&nbsp;&nbsp;&nbsp;//直接执行SQL语句<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CATCH 
            (CDBException,e)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString 
            strMsg;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strMsg.LoadString(IDS_EXECUTE_SQL_FAILED);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strMsg+=strSQL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
            FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_CATCH 
            <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
            TRUE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>应当指出的是,由于不同DBMS提 
            供的数据操作语句不尽相同,直接执行SQL语句可能会破坏软件的DBMS无关性,因此在应用中应当慎用此类操作。<BR>3.28&nbsp;&nbsp;&nbsp;&nbsp;注意<BR>从CRecordSet导出的类中如果包含DataTime类型的数据,在VC中是用CTime类型来替代的,这时,构造函数没有赋于缺省值。这时,我们应当手工赋值。如下所示:<BR>CTime&nbsp; 
            m_time;<BR>m_time=NULL;<BR>3.3&nbsp;&nbsp;&nbsp;&nbsp;总结<BR>VisualC++中的ODBC类库可以帮助程序员完成绝大多数的数据库操作。利用ODBC技术使得程序员从具体的DBMS中解脱出来,从而极大的减少了软件开发的工作量,缩短开发周期,提高了效率和软件的可靠性。<BR><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0 
width=770>
  <TBODY>
  <TR bgColor=#006699>
    <TD align=middle bgColor=#006699 id=white><FONT 
    color=#ffffff>对该文的评论</FONT></TD>
    <TD align=middle>
      <SCRIPT src="CSDN_文档中心_用MFCODBC编写数据库应用程序.files/readnum.htm"></SCRIPT>
    </TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1 
width=770>
  <TBODY>
  <TR>
    <TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16 
      hspace=1 src="CSDN_文档中心_用MFCODBC编写数据库应用程序.files/ico_pencil.gif" width=16> 
      </SPAN>&nbsp;&nbsp;&nbsp;&nbsp; yilong2001 <I>(2001-5-8 16:46:38)</I> 
  </TD></TR>
  <TR>
    <TD bgColor=#ffffff colSpan=3 
      width=532><BR>hi:大哥(姐)你的水平很高啊,李某幸运能看到这编文章,收益蜚浅,不知是否可以单独一e-mail联络,让鄙人能多请教一些问题,如蒙不弃,就与yilong2001@sina.com回个话,无论你愿意否,这里都先谢过了! 
      <BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1 
width=770>
  <TBODY>
  <TR>
    <TH bgColor=#006699 id=white><FONT 
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
  <TBODY>
  <TR>
    <TD>你没有登陆,无法发表评论。 请先<A 
      href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=330">登陆</A> 
      <A 
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>

<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
  <TBODY>
  <TR align=middle>
    <TD height=10 vAlign=bottom><A 
      href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A 
      href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A 
      href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A 
      href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A 
      href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A 
      href="http://www.csdn.net/english">English</A> </TD>
    <TD align=middle rowSpan=3><A 
      href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG 
      border=0 height=48 src="CSDN_文档中心_用MFCODBC编写数据库应用程序.files/biaoshi.gif" 
      width=40></A></TD></TR>
  <TR align=middle>
    <TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
  <TR align=middle>
    <TD vAlign=top><FONT face=Verdana>Copyright &copy; CSDN.net, Inc. All rights 
      reserved</FONT></TD></TR>
  <TR>
    <TD height=15></TD>
    <TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>

⌨️ 快捷键说明

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