VC USES the Ado interface to connect and use the database and considerations

  • 2020-05-19 04:35:01
  • OfStack

1. Other things to note when reading this article
1. Conversion of the type of parameters passed when making method calls (there may be a simpler method than this one, but I haven't found it)
2. Each line comment for each source file indicates its file name
3. Please pay attention to the relevant header file inclusion relationship
4. Please pay attention to all the Chinese comments in this article
5. For more information, please refer to the file "VC installation directory \Include\ adoint.h ",adoint = ActiveX Data Object
InterFace(don't panic, it's just a name)

2. The source file below is not directly related to your database application, but its object code (generated.obj file) is required for your reference
file: / / Ado cpp file / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# include
# include
# include
How to use the file: create an empty MFC project, add the file to the project, compile to generate the Ado.obj file, and then add the.obj file to your database application. The source file is not required in your database application.

3. Here is the code (not all of it) associated with your database application source files
 
  file://1 , ado.h file //////////////////////////////////////// 
  #ifndef __ADO__H__LZG 
  #define __ADO__H__LZG 
  #include 
  #include 
  #include 
  #endif 
  file://2 , stdafx.h file //////////////////////////////////////// 
  #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:// Notice here  
  #ifndef _AFX_NO_AFXCMN_SUPPORT 
  #include 
  file://3 , database applications .h file /////////////////////////////////////////////// 
  file:// The following are several related database reference variables used ( The declaration is in its header file ) 
  ADOField* pfd; 
  ADOFields* pfds; 
  CString m_dbfile; 
  ADORecordset* prs; 
  ADOConnection* pdb; 
  file://4 , database applications .cpp file ///////////////////////////////////////////// 
  #include "stdafx.h" 
  #include " Database application .h" 
  file:// Add other related header files here  
  file:// The following is the .cpp Several custom or non-custom methods in the file , Related to database connection  
  file:// each 1 Each statement has a meaning , Pay attention to the comments  
  BOOL CBKDlg::InitDataEnv() 
  { 
   file:// The following is defined 1 A simple connection string , There are more complicated ones, of course  
   CString s=_T("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=d:\\data\\ Information lending management .mdb"); 
   ::CoInitialize (NULL);// Initialize the COM The environment  
   CoCreateInstance(CLSID_CADOConnection, 
   NULL, 
   CLSCTX_INPROC_SERVER, 
   IID_IADOConnection15, 
   (LPVOID*)&pdb 
   );// Initialize the 1 a ADO The connection  
  CoCreateInstance(CLSID_CADORecordset, 
   NULL, 
   CLSCTX_INPROC_SERVER, 
   IID_IADORecordset, 
   (LPVOID*)&prs 
   );// Initialize the 1 a ADO The data set  
  CoCreateInstance(CLSID_CADOField, 
   NULL, 
   CLSCTX_INPROC_SERVER, 
   IID_IADOFields, 
   (LPVOID*)&pfds 
   );// Initialize the 1 a ADO Data domain collection ( The domain is Fox In the field , The same below ) 
  CoCreateInstance (CLSID_CADOField, 
   NULL, 
   CLSCTX_INPROC_SERVER, 
   IID_IADOField, 
   (LPVOID*)&pfd 
   );// Initialize the 1 a ADO In a data domain collection 1 A domain  
  file:// Open the ADO The connection  
  pdb->Open((unsigned short*)(LPCSTR)s,(unsigned short*)"",(unsigned short*)"",0); 
   file:// Open the ADO The data set , But its connection parameter is the string above used  
  file:// There should be a way to replace the string with the connection above , But I couldn't find it  
  prs->Open(COleVariant(_T(" information ")),COleVariant(s),adOpenKeyset,adLockOptimistic,adCmdTable); 
  file:// The domain of the data set does not exist whether to open or not , Simply go directly to the open data set for a reference , For details, please see  
   
  file://CBKDlg::OnBeforeColUpdateDatagrid methods  
  m_dg.ClearFields ();//MS DATAGRID  controls (Activex) Data clearing  
  m_dg.SetRefDataSource(prs); file:// The binding of the above control to the data set  
  return TRUE; 
  } 
  BOOL CBKDlg::DestroyWindow() 
   { 
    // TODO: Add your specialized code here and/or call the base class 
    m_dg.SetRefDataSource(NULL); 
    long state; 
    file:// There may be a logical error in the following processing , However, the syntax to close the data set and data connection is correct  
    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 ();// The release of COM The environment  
     return CDialog::DestroyWindow(); 
    } 
    void CBKDlg::OnBeforeColUpdateDatagrid(short ColIndex, VARIANT FAR* OldValue, short FAR* Cancel) 
   { 
    file:// The event ( methods ) in MS DATAGRID  controls (ActiveX) Occurs before the cell data is updated  
    file:// You may not need the event, but you may need the code  
    COleVariant v((LPCSTR)m_dg.GetText ());// Gets the data for the current cell of the above control  
    CString fieldname=m_dg.GetColumns().GetItem(COleVariant(ColIndex)).GetCaption(); 
    DataTypeEnum fieldtype;// describe ADO An enumerated type for a data type  
    prs->get_Fields(&pfds);// Get a collection of data domains from a data set  
    pfds->get_Item (COleVariant(fieldname),&pfd);// Gets a domain with a specific name from the data domain collection  
    pfd->get_Type (&fieldtype);// Gets its data type, such as integer or string, from the above field  
    switch (fieldtype){ 
     case adSmallInt: 
     case adInteger: 
          break; 
     case adDate: 
          break; 
     case adCurrency://Data type describing for Money ,Understand? 
          break; 
     case adVarChar:// Corresponding to the VB In the String The type and VC In the CString type  
          break; 
     default: 
          break; 
     } 
    }// This method is derived from MS DataGrid ActiveX Control events without specific handling code , Hope is burke  

Related articles: