The member variable m_pDatabase is defined in the CRecordSet class:
CDatabase * m _ pDatabase
It is a pointer to the object database class. If you pass an open pointer to the CDatabase class object to m_pDatabase before the CRecordSet class object calls the open () function, you can * * * enjoy the same CDatabase class object. For example:
CDatabase m _ db
CRecordSet m_set 1,m _ set2
m_db。 open(_ T(" Super _ ES ")); //establish ODBC connection
m _ set 1 . m _ p database = & amp; M _ db//m_set 1 reuse m _ db object
m _ set 2 . m _ pdata BSE = & amp; M _ db// m_set2 reuses m _ db objects.
Or as follows:
Cdatabase db
db。 Open ("database"); //establish ODBC connection
CrecordSet m _ set(& amp; db); //Construct a recordset object so that the database points to db.
3.22 Query records
Querying records uses CRecordSet::Open () and CRecordSet::Requery () member functions. Before using the CRecordSet class object, you must use the CRecordSet::Open () function to obtain a valid recordset. Once the CRecordSet::Open () function is used, you can apply the CRecordSet::Requery () function when querying again. When calling the CRecordSet::Open () function, if an opened pointer to the CDatabase object has been passed to the m_pDatabase member variable of the CRecordSet class object, an ODBC connection will be established with the database object; Otherwise, if m_pDatabase is empty, create a new CDatabase class object and connect it to the default data source, and then initialize the CRecordSet class object. The default data source is obtained through the GetDefaultConnect () function. You can also provide the required SQL statement and use it to call the CRecordSet::Open () function, for example:
m_Set。 Open(AFX_DATABASE_USE_DEFAULT,strSQL);
If no parameters are specified, the program will use the default SQL statement, that is, run the SQL statement specified in the GetDefaultSQL () function:
CString CTestRecordSet::GetDefaultSQL()
{return _T("[BasicData],[MainSize]"); }
For the table name returned by the GetDefaultSQL () function, the corresponding default operation is the SELECT statement, namely:
SELECT * FROM BasicData,MainSize
In the process of query, you can also use the member variables m_strFilter and m _ strort of CRecordSet for conditional query and result sorting. M_strFilter is a filter string, which stores the conditional string after WHERE in the SQL statement; M _ strjob is a sort string, which stores the string after ORDERBY in the SQL statement. For example:
m _ set . m _ str filter = " TYPE = ' motor ' ";
M _ set.m _ strort = "voltage";
m_Set。 requery();
The corresponding SQL statement is:
SELECT * FROM BasicData,MainSize
Where type = "Motor"
Sort by voltage
In addition to assigning directly to m_strFilter, you can also use parameterization. Using parameterization can complete the task of conditional query more intuitively and conveniently. The steps to use parameterization are as follows:
(1). Declare parameter variables:
cstring p 1;
Floating p2;
(2) Initialize the parameter variables in the constructor.
p 1 = _ T(" ");
p2 = 0.0f
m _ nParams = 2;
(3) Bind the parameter variable with the corresponding column.
pFX->; setfield type(CFieldExchange::param)
RFX_Text(pFX,_T("P 1 "),p 1);
RFX _ single (pFX, _ t ("p2"), p2);
After completing the above steps, you can use parameter variables for conditional query:
m _ pSet-& gt; m_strFilter="TYPE=? ANDVOLTAGE=? ”;
m _ pSet-& gt; P 1= "motor";
m _ pSet-& gt; p2 = 60.0
m _ pSet-& gt; requery();
The value of parameter variable replaces "?" Binding order in the query string. Adapter.
If the query result is multiple records, you can use the functions of CRecordSet class Move (), MoveNext (), MovePrev (), MoveFirst () and MoveLast () to move the cursor.
3.23 Add records
Adding records uses the AddNew () function, which requires that the database must be opened in such a way as to allow the following contents to be added:
m _ pSet-& gt; add new(); //Add a new record at the end of the table
m _ pSet-& gt; setfield null(& amp; (m _ pSet-& gt; m_type),FALSE);
m _ pSet-& gt; M_type= "motor";
...//Enter a new field value.
m _ pSet-& gt; update(); //Store the new record in the database.
m _ pSet-& gt; requery(); //Regenerate the recordset
3.24 Delete records
After calling the Delete () function, instead of calling the Update () function, use the Delete () function directly:
m _ pSet-& gt; delete();
If (! m _ pSet-& gt; IsEOF())
m _ pSet-& gt; MoveNext();
other
m _ pSet-& gt; MoveLast();
3.25 modify the record
Use the Edit () function to modify the record:
m _ pSet-& gt; edit(); //Modify the current record
m _ pSet-& gt; M _ type = "generator// Modify the field value of the current record.
...
m _ pSet-& gt; update(); //Store the modification results in the database.
m _ pSet-& gt; requery();
3.26 Statistical records
Statistical records are used to calculate the total number of record sets. You can declare a CRecordset object m_pSet first. Then bind a variable m_lCount to count the total number of records. Execute the following statement:
m _ pSet-& gt; open(" select count(*)from table name where qualification ");
record count = m _ pSet-& gt; m _ lCount
m _ pSet-& gt; close();
RecordCount is the number of records to count.
Or as follows:
CRecordset m _ Set(& amp; db); //db is a CDatabase object.
CString strValue
M_Set。 open(Select count(*)from table name where qualification ");
m_pSet。 GetFieldValue((int)0,strValue);
long count = atol(strValue);
m_set。 close();
Count is the total number of records.