ODBC是不是一定要DoFieldExchange进行数据邦定的?要是程序作好后数据库表的结构变了怎么办?不就要修改整个程序了吗?哪有这么不方便的技术的?

解决方案 »

  1.   

    那这个ODBC就太没用了,学来干嘛?一个程序只能对应一种结构的数据表。
      

  2.   

    不用绑定,但只能用CRecordset::forwardOnly 打开。这种方式还是很灵活的。
      

  3.   

    可以不帮定,,只是要自己重载CRecordset
      

  4.   

    Crecordset rs(&db);
    rs.Open(CRecordset::forwardOnly,,"SQL语句");
    CString szValues;
    rs.GetFieldValue("fieldname", szValues);

    rs.GetFieldValue(short(0), szValues); 
    MSDN:
    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. 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 Platform SDK. 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. 
    Res
    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 Platform SDK.Exceptions
    This method can throw exceptions of type CDBException* and CMemoryException*.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).See Also
    CRecordset Overview | Class Members | Hierarchy Chart | CRecordset::DoFieldExchange | CRecordset::DoBulkFieldExchange | CRecordset::GetODBCFieldCount | CRecordset::GetODBCFieldInfo | CRecordset::SetRowsetCursorPosition--------------------------------------------------------------------------------Send feedback to Microsoft&copy; 2001 Microsoft Corporation. All rights reserved.