Reference resources: http://blog.csdn.net/augusdi/article/details/7005597 Some additions and deletions were made.
Dead work:
(1) Introducing ADO classes
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
(2) Initialization of COM
AfxOleInit() can be used in MFC.
Use CoInitialize (NULL) in non-MFC environments; / CoUnInitialize ();
(3) Import contains three smart pointers: _ConnectionPtr, _RecordsetPtr and_CommandPtr
Use steps:
1. Connection and closure data base
(1) connection
Example: Connect to Access database
(2) shut down
(3) Setting connection time
2. Open a result set
(1) Open, first create an instance of _RecordsetPtr, and then call Open() to get the execution result of an SQL statement.
(2) Close the result set
m_pRecordset->Close();
3. Operating on a result set
(1) Traverse (read)
(a) Use pRecordset - > adoEOF to determine whether the database pointer has moved to the end of the result set;
M_pRecordset - > BOF determines whether or not the first record precedes:
b) There are two ways to get the value of a field
The first is:
// Represents getting the value of field 0
rdset->GetCollect("Name");
/ / or
rdset->GetCollect(_variant_t(long(0));
Second kinds
pRecordset->get_Collect("COLUMN_NAME");
/ / or
pRecordset->get_Collect(long(index));
(2), add
Call m_pRecordset - > AddNew ();
Call m_pRecordset - > PutCollect (); assign values to each field
Call m_pRecordset - > Update (); confirm
(3), modification
Call m_pRecordset - > PutCollect (); assign values to each field
Call m_pRecordset - > Update (); confirm
(4), delete
Move the record pointer to the record to be deleted, and then call Delete(adAffectCurrent)
4. Execute SQL statements directly
(1) Cooperate with _CommandPtr and _RecordsetPtr
(2) Executing SQL statements directly with _ConnectionPtr
_RecordsetPtr Connection::Execute ( _bstr_t CommandText, VARIANT * RecordsAffected, long Options )
Parameters:
Records Affected is the number of rows affected by the completion of the operation.
Options denotes the type of content in CommandText, and Options can take one of the following values:
adCmdText: Indicates that CommandText is a text command, usually an SQL statement;
adCmdTable: Indicates that CommandText is a table name;
adCmdProc: Indicates that CommandText is a stored procedure
adCmdUnknown: Unknown, or Default
5. Calling stored procedures
(1) Utilizing _CommandPtr
(2) Call directly with _ConnectionPtr
Special applications:
Display database and table information
The member function openSchema() of _ConnectionPtr is used to obtain the schema information of the database.
Function prototype:
Parameters:_RecordsetPtr recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)
1. QueryType
SchemaEnum enumerates the value of the type, indicating the type of the acquisition pattern (Schema);
2. Criteria
Options, corresponding to SchemaEnum's Query options
3. SchemaID
Optionally, it is only useful if QueryType is set to adSchemaProvider Specific, otherwise this parameter is not used;
The commonly used values for SchemeEnum enumeration types are as follows:
adSchemaColumns: Returns the column names of the tables in the database directory accessible to the user (including View views);
Returns the column name (field name) in the result set:
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
adSchemaTables: Returns the name of the table in the database directory accessible to the user;
Returns the column name (field name) in the result set:
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
Example 1: Traverse all table names in the database;
Example 2: Traversing through all fields in a table
- HRESULT hr;
- Fields * fields = NULL;
- // Get the field set of the recordset and store it in fields.
- hr=m_pRecordset->get_Fields(&fields);
- if(SUCCEEDED(hr))
- {
- // Get the total number of fields in the field set of the Recordset
- fields->get_Count(&ColCount);
- for(i=0;i ...
- {
- Item [i] - > get_Name (& bstrColName); // Get the field name in recordset //
- strColName=bstrColName;
- nameField = strColName;
- m_FieldsList.AddString(nameField);
- }
- if(SUCCEEDED(hr))
- {
- Fields - > Release (); // Release Pointer
- }
- }