How to get the form field name in ADO development

xiaoxiao2021-03-06  23

[Original documentation This article is suitable for primary readers have read 5887 times]

ADO developers in how to get the name of the form fields: Yang Guiwen Download This sample source code development process database, common operation is not only to add, modify, delete records, as well as the operation of the field, and get the field name is one of them . Before getting a field name, you must connect to the database, open the corresponding table, then get the field set of the table, which is taken as an example, how to get a field name. • New VC MFCAppWizard (EXE) project, type Dialog Based; • Add a button name "Display Field", add a listBox control; • Create an Access database fti.mdb; • Running is as shown in the figure: 1. Code Description (1) Introducing the following code to introduce the following code to introduce the ADO library definition file, # import "c: / program files / compo files / system / ado / msado15.dll" /

NO_NAMESPACE /

Rename ("EOF", "Adoeof")

(2) Initialize the COM library, add AFXoleinit () in Bool CGetfieldNameapp :: InitInstance (); (3) Add two variables to open database connections and open record sets in class cgetfieldnamedlg; _ConnectionPtr M_PConnection;

_RecordSetPtr m_precordset;

(4) Add a database connection and record set initialization code in the constructor of CGETFIELDNAMEDLG; m_pConnection.createInstance (__ uuidof (connection));

Try

{

m_pConnection-> Open ("provider = microsoft.jet.Oledb.4.0; data source = d: //fti.mdb",

""

""

Admodeunknown

);

}

Catch (_COM_ERROR E)

{

AfxMessageBox ("Database connection failed!");

}

m_precordset.createInstance (__ uuidof (recordset)); (5) Add control variable m_fieldslist; (6) Add a corresponding code in the click event of the button; _BSTR_T MSTRSQL;

CString strcolname;

BSTR BSTRCOLNAME;

Long colcount, i;

Field * Field = NULL;

HRESULT HR;

Fields * Fields = NULL;

LPCTSTSTSTSTSTSTSTR Namefield;

/ / Open the record set, get the field name, and add field name information to listbox

MSTRSQL = "SELECT * FROMAGES";

m_precordset-> Open (MSTRSQL,

m_pconnection.getInterfacePtr (),

AdoPENDYNAMIC,

AdlockOptimistic,

AdcmdText);

hr = m_precordset-> get_fields (& FIELDS); // Get the field set of records and

En (ac))

Fields-> Get_count (& colcount);

/ / Get the total number of fields in the field collection of records

For (i = 0; i

Fields-> Item [i] -> get_name (& bstrcolname); // Get the field name in the recordset //

Strcolname = BSTRCOLNAME;

Namefield = strcolname;

m_fieldslist.addstring (namefield);

}

En (ac))

Fields-> release (); // Release pointer

转载请注明原文地址:https://www.9cbs.com/read-65150.html

New Post(0)