当前位置: 移动技术网 > IT编程>开发语言>.net > VC用Ado接口连接和使用数据库及注意事项

VC用Ado接口连接和使用数据库及注意事项

2017年12月12日  | 移动技术网IT编程  | 我要评论

商陵君养龙,上流社会130518,衣柜字幕

一、阅读本文所需注意的其它事项
  1、进行方法调用时,所传递的参数的类型的转换(可能存在比本文更简便的处理方法但我未发现)
  2、每个源文件的每行注释说明了其文件名
  3、请关注相关头文件包含关系
  4、请关注文中所有中文注释
  5、更多的内容请参阅 "vc安装目录\include\adoint.h"文件,adoint即activex data object
  interface(菜 鸟请勿惊慌,这仅仅只是个名称)

二、下面的源文件与您的数据库应用程序不直接相关,但其目标代码(生成的.obj文件)是您必需的,请参考
  file://ado.cpp文件///////////////////////////////////////////
  #include
  #include
  #include
  该文件使用方法:新建一个空的mfc工程,将此文件添加到该工程中,编译生成ado.obj文件,再将此.obj文件添加到您的数据库应用程序.该源文件在您的数据库应用程序中是不需要的。

三、下面是与您的数据库应用程序源文件相关代码(非所有代码)
复制代码 代码如下:

  file://1、ado.h文件////////////////////////////////////////
  #ifndef __ado__h__lzg
  #define __ado__h__lzg
  #include
  #include
  #include
  #endif
  file://2、stdafx.h文件////////////////////////////////////////
  #if _msc_ver > 1000
  #pragma once
  #endif // _msc_ver > 1000
  #define vc_extralean // exclude rarely-used stuff from windows headers
  #include // mfc core and standard components
  #include // mfc extensions
  #include // mfc automation classes
  #include // mfc support for internet explorer 4 common controls
  #include "ado.h" file://请注意这里
  #ifndef _afx_no_afxcmn_support
  #include
  file://3、数据库应用程序.h文件///////////////////////////////////////////////
  file://以下为用到的若干相关数据库引用变量(声明在其头文件中)
  adofield* pfd;
  adofields* pfds;
  cstring m_dbfile;
  adorecordset* prs;
  adoconnection* pdb;
  file://4、数据库应用程序.cpp文件/////////////////////////////////////////////
  #include "stdafx.h"
  #include "数据库应用程序.h"
  file://这里添加其它相关头文件
  file://以下为该.cpp文件中若干自定义或非自定义的方法,与数据库连接相关
  file://其中每一条语句都有含义,请关注其中的注释
  bool cbkdlg::initdataenv()
  {
   file://下面定义了一个简单的连接字符串,当然还有更复杂的
   cstring s=_t("provider=microsoft.jet.oledb.3.51;data source=d:\\data\\资料借阅管理.mdb");
   ::coinitialize (null);//初始化com环境
   cocreateinstance(clsid_cadoconnection,
   null,
   clsctx_inproc_server,
   iid_iadoconnection15,
   (lpvoid*)&pdb
   );//初始化一个ado连接
  cocreateinstance(clsid_cadorecordset,
   null,
   clsctx_inproc_server,
   iid_iadorecordset,
   (lpvoid*)&prs
   );//初始化一个ado数据集
  cocreateinstance(clsid_cadofield,
   null,
   clsctx_inproc_server,
   iid_iadofields,
   (lpvoid*)&pfds
   );//初始化一个ado数据域集合(域即是fox中的字段,下同)
  cocreateinstance (clsid_cadofield,
   null,
   clsctx_inproc_server,
   iid_iadofield,
   (lpvoid*)&pfd
   );//初始化一个ado数据域集合中的一个域
  file://打开上述ado连接
  pdb->open((unsigned short*)(lpcstr)s,(unsigned short*)"",(unsigned short*)"",0);
   file://打开上述ado数据集,但它的连接参数是使用的上述字符串
  file://应当存在以上面的连接替换该字符串的方法,但我没找到
  prs->open(colevariant(_t("资料信息")),colevariant(s),adopenkeyset,adlockoptimistic,adcmdtable);
  file://数据集的域不存在打开与否,只需直接到已打开的数据集中引用即可,详情请见
  
  file://cbkdlg::onbeforecolupdatedatagrid方法
  m_dg.clearfields ();//ms datagrid 控件(activex)的数据清空
  m_dg.setrefdatasource(prs); file://上述控件与数据集的绑定
  return true;
  }
  bool cbkdlg::destroywindow()
   {
    // todo: add your specialized code here and/or call the base class
    m_dg.setrefdatasource(null);
    long state;
    file://下面的处理可能存在逻辑上的错误,不过关闭数据集与数据连接的语法是正确的
    if(!failed(prs->get_state(&state)))
     if(state!=adstateclosed)
      {
       prs->close();
       prs=null;
      }
     if(!failed(pdb->get_state(&state)))
      if(state!=adstateclosed)
      {
       pdb->close();
       pdb=null;
      }
     ::couninitialize ();//释放com环境
     return cdialog::destroywindow();
    }
    void cbkdlg::onbeforecolupdatedatagrid(short colindex, variant far* oldvalue, short far* cancel)
   {
    file://该事件(方法)在ms datagrid 控件(activex)的单元数据更新前发生
    file://该事件您可能用不着,但其中的代码您可能用得着
    colevariant v((lpcstr)m_dg.gettext ());//获取上述控件当前单元格的数据
    cstring fieldname=m_dg.getcolumns().getitem(colevariant(colindex)).getcaption();
    datatypeenum fieldtype;//描述ado数据类型的枚举类型
    prs->get_fields(&pfds);//从数据集中获得数据域集合
    pfds->get_item (colevariant(fieldname),&pfd);//从数据域集合中获得特定名称的域
    pfd->get_type (&fieldtype);//从上述域中获取其数据类型,如整型或字符串型
    switch (fieldtype){
     case adsmallint:
     case adinteger:
          break;
     case addate:
          break;
     case adcurrency://data type describing for money ,understand?
          break;
     case advarchar://对应于vb中的string类型和vc中的cstring类型
          break;
     default:
          break;
     }
    }//该方法来源于ms datagrid activex控件的事件,无具体的处理代码,望海涵

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网