VC用Ado介面串連和使用資料庫及注意事項_實用技巧

來源:互聯網
上載者:User
一、閱讀本文所需注意的其它事項
  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控制項的事件,無具體的處理代碼,望海涵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.