Retrieves field data in the current record.
void GetFieldValue( LPCTSTR lpszName, CDBVariant& varValue, short nFieldType = DEFAULT_FIELD_TYPE ); void GetFieldValue( short nIndex, CDBVariant& varValue, short nFieldType = DEFAULT_FIELD_TYPE ); void GetFieldValue( short nIndex, CStringA& strValue ); void GetFieldValue( short nIndex, CStringW& strValue );
You can look up a field either by name or by index. You can store the field value in either a CDBVariant object or a CString object.
If you have implemented bulk row fetching, the current record is always positioned on the first record in a rowset. To use GetFieldValue on a record within a given rowset, you must first call the SetRowsetCursorPosition member function to move the cursor to the desired row within that rowset. Then call GetFieldValue for that row. To implement bulk row fetching, you must specify the CRecordset::useMultiRowFetch option of the dwOptions parameter in the Open member function.
You can use GetFieldValue to dynamically fetch fields at run time rather than statically binding them at design time. For example, if you have declared a recordset object directly from CRecordset, you must use GetFieldValue to retrieve the field data; record field exchange (RFX), or bulk record field exchange (Bulk RFX), is not implemented.
Note
|
|---|
|
If you declare a recordset object without deriving from CRecordset, do not have the ODBC Cursor Library loaded. The cursor library requires that the recordset have at least one bound column; however, when you use CRecordset directly, none of the columns are bound. The member functions CDatabase::OpenEx and CDatabase::Open control whether the cursor library will be loaded. |
GetFieldValue calls the ODBC API function SQLGetData. If your driver outputs the value SQL_NO_TOTAL for the actual length of the field value, GetFieldValue throws an exception. For more information about SQLGetData, see the Windows SDK.
This method can throw exceptions of type CDBException* and CMemoryException*.
The following sample code illustrates calls to GetFieldValue for a recordset object declared directly from CRecordset.
// Create and open a database object; // do not load the cursor library CDatabase db; db.OpenEx(NULL, CDatabase::forceOdbcDialog); // Create and open a recordset object // directly from CRecordset. Note that a // table must exist in a connected database. // Use forwardOnly type recordset for best // performance, since only MoveNext is required CRecordset rs(&db); rs.Open(CRecordset::forwardOnly, _T("SELECT * FROM Customer")); // Create a CDBVariant object to // store field data CDBVariant varValue; // Loop through the recordset, // using GetFieldValue and // GetODBCFieldCount to retrieve // data in all columns short nFields = rs.GetODBCFieldCount(); while(!rs.IsEOF()) { for(short index = 0; index < nFields; index++) { rs.GetFieldValue(index, varValue); // do something with varValue } rs.MoveNext(); } rs.Close(); db.Close();
Note
|
|---|
|
Unlike the DAO class CDaoRecordset, CRecordset does not have a SetFieldValue member function. If you create an object directly from CRecordset, it is effectively read-only. |
For more information about bulk row fetching, see the article Recordset: Fetching Records in Bulk (ODBC).
Header: afxdb.h
Note