MFC Library Reference CRecordset::GetFieldValue 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 ); 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. 一个例子: 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( );
GetFieldValue(num,"varValue"); num 为数据表内对应的项的列号,varValue 为所获得的内容。注意顺序,如:
比如 创建了字段 field1 field 2 field3
读的时候就要
GetFieldValue(field1 ,...);
GetFieldValue(field2 ,...);
GetFieldValue(field3 ,...);
如果改为
GetFieldValue(field3,...);
GetFieldValue(field1 ,...); //这时就会报错
GetFieldValue(field2...);2 关联了一个记录集就直接用
m_recordset.field ///field就是字段
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
);
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. 一个例子:
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( );