Title: Connect and use the ADO interface code in VC
First, read other things you need to note this article:
1. When performing method call, the type of transition of the type transmitted (there may be more easy processing methods than this article but I have not found)
2, each line of annotations for each source file indicates its file name
3, please pay attention to the related header file containing the relationship
4, please pay attention to all Chinese comments in the text
5, more content, please refer to "VC installation directory /include/adoint.h" and "VC installation directory /include/adoid.h" file, AdOint, ActiveX Data Object Interface
Second, the following source file is not directly related to your database application, but its target code (generated .Obj file) is required, please refer to
//Ado.cpp file ///
#include
#include
#include
This file uses the newly created an empty MFC project, add this file to the project, build the ADO.Obj file, add this .Obj file to your database application. This source file is applied in your database. No need in the program ---- author
Third, below is the code related to your database application source file (non-all code)
//1 ,ado.h file
#ifndef __ado__h__lzg
#define __ado__h__lzg
#include
#include
#include
#ENDIF
//2 ,stdafx.h file
#iF _MSC_VER> 1000
#pragma overce
#ENDIF / / 100 m _ _ _
#define vc_extralean // Exclude Rarely-usest Stuff from Windows Headers
#include
#include
#include
#include
#include "ado.h" // Please note here
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include
// 3, database application .h file /
// The following is the number of related database reference variables (declared in its header)
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 related header files
// The following is a custom or non-customized method in this .cpp file, related to database connection
// Where each statement has a meaning, please 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 // Data Borrow Management. MDB");
:: Coinitialize (null); // Initialization 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 data set
CoCreateInstance (CLSID_CADOFIELD,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADOFIELDS,
(LPVOID *) & PFDS
); // Initialize an ADO data domain assembly (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 collection
/ / Open the above ADO connection
PDB-> Open ((unsigned short *) s, (unsigned short *) ", (unsigned short *)", 0);
/ / Open the above ADO data set, but its connection parameters are used in the above string
/ / The method of replacing the string with the above connection, but I didn't find it.
PRS-> Open (Colevariant), Colevariant (s), AdopenKeyset, AdlockOptimistic, Adcmdtable;
// The domain of the data set does not open or not, just directly to the open data set, please see // CBKDLG :: OnBeforeColupdatedataGrid method
M_dg.clearfields (); // MS DataGrid Control (ActiveX) data empty
m_dg.setRefDataSource (PRS); // Binds of the above controls and data sets
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 process may have a logical error, but the syntax of the shutdown data set and the data connection is correct.
IF (! failed (PRS-> Get_State)))
IF (state! = adStateclosed)
{
PRS-> close ();
PRS = NULL;
}
IF (! failed (pdb-> get_state (& statE))))
IF (state! = adStateclosed)
{
PDB-> Close ();
PDB = NULL;
}
:: Couninitialize (); // Release COM environment
Return CDialog :: destroyWindow ();
}
Void CBKDLG :: OnBeforeColupdatedataGrid (Short Colindex, Variant Far * OldValue, Short Far * Cancel)
{
// This event (method) occurs before the MS DataGrid control (ActiveX) unit data update
/ / The event you may not need, but you may use it.
Colevariant V ((LPCSTR) m_dg.gettext ()); // Get data of the current cell's current cell
CString FieldName = m_dg.getColumns (). GetItem ()). GetCaption (); DataTyPeenum FieldType; // Describe an enumeration type of the ADO data type
PRS-> Get_fields (& PFDS); // Get the data domain collection from the data set
Pfds-> Get_Item (Colevariant (FieldName), & PFD); // Get a specific name from the data domain collection
PFD-> Get_Type (& FieldType); // Get its data type from the above domain, such as integer or string
Switch (FieldType) {
Case Adsmallint:
Case Adinteger:
Break;
Case address:
Break;
Case Adcurrency: // Data Type Describing for Money, Understand?
Break;
Case AdvarChar: / / corresponds to the string type in VB and CString type in VC
Break;
DEFAULT:
Break;
}
} // This method comes from the event of the MS DataGrid ActiveX control, no specific processing code, Wang Han