Connect to and use the database with the ADO interface code in VC
Source: Internet
Author: User
Title: connect to and use the database with the ADO interface code in VC
I. Other considerations 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 "VC installation directory/include/adoint. H" and "VC installation directory/include/adoid. H". adoint is ActiveX Data Object Interface
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
// ADO. CPP file ////////////////////////////////////// /////
# Include <objbase. h>
# Include <initguid. h>
# Include <adoid. h>
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-author
3. the following code is related to the source file of your database application (not all code)
// 1. Ado. h file ////////////////////////////////////// //
# Ifndef _ adow.hw.lzg
# DEFINE _ ado__h1_lzg
# Include <adoid. h>
# Include <adoint. h>
# Include <icrsint. h>
# Endif
// 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 <afxwin. h> // MFC core and standard components
# Include <afxext. h> // MFC extensions
# Include <afxdisp. h> // MFC automation classes
# Include <afxdtctl. h> // MFC support for Internet Explorer 4 common controls
# Include "ADO. H" // note the following:
# Ifndef _ afx_no_afxcmn_support
# Include <afxcen. h>
// 3. Database Application. h file ////////////////////////////////////// ///////////////////////////////
// The following are the variables referenced by related databases (declared in the header file)
Adofield * PFD;
Adofields * PFDS;
Cstring m_dbfile;
Adorecordset * PRS;
Adoconnection * PDB;
// 4. database application. CPP file ////////////////////////////////////// ///////////////////////////////
# Include "stdafx. H"
# Include "database application. H"
// Add other header files here
// The following are some custom or non-custom methods in the. cpp file, which are related to database connection.
// Each statement has a meaning. Pay attention to the comments.
Bool cbkdlg: initdataenv ()
{
// The following defines a simple connection string, of course, more complex
Cstring S = _ T ("provider = Microsoft. Jet. oledb.3.51; Data Source = D: // 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
// Open the above ADO connection
PDB-> open (unsigned short *) (lpcstr) s, (unsigned short *) "", (unsigned short *) "", 0 );
// Open the preceding ADO dataset, but its connection parameters use the preceding string
// The above connection should exist to replace the string, but I did not find
PRS-> open (colevariant (_ T (""), colevariant (s), adopenkeyset, adlockoptimistic, adcmdtable );
// The dataset's domain does not exist. You only need to reference it directly in the opened dataset. For details, see // cbkdlg: onbeforecolupdatedatagrid.
M_dg.clearfields (); // clear data for the MS DataGrid Control (ActiveX)
M_dg.setrefdatasource (PRS); // 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;
// The following processing may have logical errors, 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)
{
// This event (method) occurs before the unit data of the MS DataGrid Control (ActiveX) is updated.
// 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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service