CRecordset::GetFieldValue

void GetFieldValue( LPCTSTR lpszName, CDBVariant& varValue, short nFieldType = DEFAULT_FIELD_TYPE );
throw( CDBException, CMemoryException );

void GetFieldValue( short nIndex, CDBVariant& varValue, short nFieldType = DEFAULT_FIELD_TYPE );
throw( CDBException, CMemoryException );

void GetFieldValue( LPCTSTR lpszName, CString& strValue );
throw( CDBException, CMemoryException );

void GetFieldValue( short nIndex, CString& strValue );
throw( CDBException, CMemoryException );

Parameters

lpszName

The name of a field.

varValue

A reference to a CDBVariant object that will store the field's value.

nFieldType

The ODBC C data type of the field. Using the default value, DEFAULT_FIELD_TYPE, forces GetFieldValue to determine the C data type from the SQL data type, based on the following table. Otherwise, you can specify the data type directly or choose a compatible data type; for example, you can store any data type into SQL_C_CHAR.

C data type SQL data type
SQL_C_BIT SQL_BIT
SQL_C_UTINYINT SQL_TINYINT
SQL_C_SSHORT SQL_SMALLINT
SQL_C_SLONG SQL_INTEGER
SQL_C_FLOAT SQL_REAL
SQL_C_DOUBLE SQL_FLOAT
SQL_DOUBLE
SQL_C_TIMESTAMP SQL_DATE
SQL_TIME
SQL_TIMESTAMP
SQL_C_CHAR SQL_NUMERIC
SQL_DECIMAL
SQL_BIGINT
SQL_CHAR
SQL_VARCHAR
SQL_LONGVARCHAR
SQL_C_BINARY SQL_BINARY
SQL_VARBINARY
SQL_LONGVARBINARY

For more information about ODBC data types, see the topics "SQL Data Types" and "C Data Types" in Appendix D of the ODBC SDK Programmer's Reference.

nIndex

The zero-based index of the field.

strValue

A reference to a CString object that will store the field's value converted to text, regardless of the field's data type.

Remarks

Call this member function to retrieve field data in the current record. 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 ODBC SDK Programmer's Reference.

Example

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 SomeTable" ) );

// 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) in Visual C++ Programmer’s Guide.

CRecordset OverviewClass MembersHierarchy Chart

See Also   CRecordset::DoFieldExchange, CRecordset::DoBulkFieldExchange, CRecordset::GetODBCFieldCount, CRecordset::GetODBCFieldInfo, CRecordset::SetRowsetCursorPosition