I. Other notes for reading this article
1. During method calling, the type of the passed parameter is converted (there may be a simpler solution than this article, but I did not find it)
2. Each line comment of each source file specifies the file name.
3. Check the header file inclusion relationship.
4. Please pay attention to all Chinese comments in this article
5. For more information, see the "VC installation directory \ Include \ adoint. h" file. adoint is ActiveX Data Object.
InterFace (this is just a name)
2. The following source files are not directly related to your database application, but the target code (generated. obj file) is required. For more information, see
File: // Ado. cpp file ////////////////////////////////////// /////
# Include
# Include
# Include
How to use this file: Create an empty MFC project, add this file to this project, compile and generate Ado. obj file. add the obj file to your database application. this source file is not required in your database application.
3. the following code is related to the source file of your database application (not all code)
Copy codeThe Code is as follows:
File: // 1. ado. h file ////////////////////////////////////// //
# Ifndef _ adow.hw.lzg
# Define _ ado__h1_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: // note the following:
# Ifndef _ AFX_NO_AFXCMN_SUPPORT
# Include
File: // 3. database application. h file ////////////////////////////////////// /////////
File: // The following are the variables referenced by the relevant database (declared in the header file)
ADOField * pfd;
ADOFields * pfds;
CString m_dbfile;
ADORecordset * prs;
ADOConnection * pdb;
File: // 4. database application. cpp file ////////////////////////////////////// ///////
# Include "stdafx. h"
# Include "database application. h"
File: // Add other header files.
File: // The following are some custom or non-custom methods in the. cpp file, which are related to database connection.
File: // each statement has a meaning. Pay attention to the comments.
BOOL CBKDlg: InitDataEnv ()
{
File: // The following defines a simple connection string, of course, more complex
CString s = _ T ("Provider = Microsoft. Jet. OLEDB.3.51; Data Source = d: \ data \ Data borrow management. mdb ");
: CoInitialize (NULL); // initialize the COM Environment
CoCreateInstance (CLSID_CADOConnection,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOConnection15,
(LPVOID *) & pdb
); // Initialize An ADO connection
CoCreateInstance (CLSID_CADORecordset,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADORecordset,
(LPVOID *) & prs
); // Initialize An ADO Dataset
CoCreateInstance (CLSID_CADOField,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOFields,
(LPVOID *) & pfds
); // Initialize An ADO data domain set (the domain is the field in Fox, the same below)
CoCreateInstance (CLSID_CADOField,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOField,
(LPVOID *) & pfd
); // Initialize a domain in An ADO data domain set
File: // open the above ADO connection
Pdb-> Open (unsigned short *) (LPCSTR) s, (unsigned short *) "", (unsigned short *) "", 0 );
File: // open the preceding ADO dataset, but its connection parameters use the preceding string
File: // The above connection should replace the string, but I did not find
Prs-> Open (COleVariant (_ T (""), COleVariant (s), adOpenKeyset, adLockOptimistic, adCmdTable );
File: // whether the dataset's domain does not exist. You only need to reference it directly in the opened dataset. For details, see
File: // CBKDlg: OnBeforeColUpdateDatagrid Method
M_dg.ClearFields (); // clear data for the ms datagrid Control (Activex)
M_dg.SetRefDataSource (prs); file: // bind the preceding control to the dataset
Return TRUE;
}
BOOL CBKDlg: DestroyWindow ()
{
// TODO: Add your specialized code here and/or call the base class
M_dg.SetRefDataSource (NULL );
Long state;
File: // The following processing may have a logical error, but the syntax for closing the dataset 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 (); // release the COM Environment
Return CDialog: DestroyWindow ();
}
Void CBKDlg: OnBeforeColUpdateDatagrid (short ColIndex, variant far * OldValue, short FAR * Cancel)
{
File: // This event (method) occurs before the unit data of the ms datagrid Control (ActiveX) is updated.
File: // you may not need this event, but the code in it may be used.
COleVariant v (LPCSTR) m_dg.GetText (); // obtain the data of the current cell of the preceding Control
CString fieldname = m_dg.GetColumns (). GetItem (COleVariant (ColIndex). GetCaption ();
DataTypeEnum fieldtype; // specifies the enumerated type of the ADO data type.
Prs-> get_Fields (& pfds); // obtain the data domain set from the dataset
Pfds-> get_Item (COleVariant (fieldname), & pfd); // obtain the domain with a specific name from the data domain collection
Pfd-> get_Type (& fieldtype); // obtain the data type from the above domain, such as integer or string
Switch (fieldtype ){
Case adSmallInt:
Case adInteger:
Break;
Case adDate:
Break;
Case adCurrency: // Data type describing for Money, Understand?
Break;
Case adVarChar: // corresponds to the String type in VB and the CString type in VC
Break;
Default:
Break;
}
} // This method is derived from the event of the MS DataGrid ActiveX control and has no specific processing code.