VC++6.0通过odbc连接数据库,将网络接收到的数据存到数据库表中相应的字段中应该怎么做?
基于对话框的工程。已经在工程中添加了继承了CRecordset的类,和继承了CRecordview 的类,然后应该如何操作呢?
[解决办法]
CRecordset::Edit
virtual void Edit( );
throw( CDBException, CMemoryException );
Remarks
Call this member function to allow changes to the current record. After you call Edit, you can change the field data members by directly resetting their values. The operation is completed when you subsequently call the Update member function to save your changes on the data source.
Note If you have implemented bulk row fetching, you cannot call Edit. This will result in a failed assertion. Although class CRecordset does not provide a mechanism for updating bulk rows of data, you can write your own functions by using the ODBC API function SQLSetPos. For an example of how to do this, see the sampleDBFETCH. For more information about bulk row fetching, see the articleRecordset: Fetching Records in Bulk (ODBC) in Visual C++ Programmer’s Guide.
Edit saves the values of the recordset’s data members. If you call Edit, make changes, then call Edit again, the record’s values are restored to what they were before the first Edit call.
In some cases, you may want to update a column by making it Null (containing no data). To do so, call SetFieldNull with a parameter of TRUE to mark the field Null; this also causes the column to be updated. If you want a field to be written to the data source even though its value has not changed, call SetFieldDirty with a parameter of TRUE. This works even if the field had the value Null.
If the data source supports transactions, you can make the Edit call part of a transaction. Note that you should call CDatabase::BeginTrans before calling Edit and after the recordset has been opened. Also note that calling CDatabase::CommitTrans is not a substitute for calling Update to complete the Edit operation. For more information about transactions, see class CDatabase.
Depending on the current locking mode, the record being updated may be locked by Edit until you call Update or scroll to another record, or it may be locked only during the Edit call. You can change the locking mode with SetLockingMode.
The previous value of the current record is restored if you scroll to a new record before calling Update. A CDBException is thrown if you call Edit for a recordset that cannot be updated or if there is no current record.
For more information, see the articlesTransaction (ODBC) andRecordset: Locking Records (ODBC) in Visual C++ Programmer’s Guide.
Example
// Example for CRecordset::Edit
// To edit a record,
// First set up the edit buffer
rsCustSet.Edit( );
// Then edit field data members for the record
rsCustSet.m_dwCustID = 2795;
rsCustSet.m_strCustomer = "Jones Mfg";
// Finally, complete the operation
if( !rsCustSet.Update( ) )
// Handle the failure to update
CRecordset Overview
[解决办法]
Class Members
[解决办法]
Hierarchy Chart
See Also CRecordset::Update, CRecordset::AddNew, CRecordset::Delete, CRecordset::SetFieldDirty, CRecordset::SetFieldNull, CRecordset::CanUpdate, CRecordset::CanTransact, CRecordset::SetLockingMode
[解决办法]
CRecordset::Update
virtual BOOL Update( );
throw( CDBException );
Return Value
Nonzero if one record was successfully updated; otherwise 0 if no columns have changed. If no records were updated, or if more than one record was updated, an exception is thrown. An exception is also thrown for any other failure on the data source.
Remarks
Call this member function after a call to the AddNew or Edit member function. This call is required to complete the AddNew or Edit operation.
Note If you have implemented bulk row fetching, you cannot call Update. This will result in a failed assertion. Although class CRecordset does not provide a mechanism for updating bulk rows of data, you can write your own functions by using the ODBC API function SQLSetPos. For an example of how to do this, see the sampleDBFETCH. For more information about bulk row fetching, see the articleRecordset: Fetching Records in Bulk (ODBC) in Visual C++ Programmer’s Guide.
Both AddNew and Edit prepare an edit buffer in which the added or edited data is placed for saving to the data source. Update saves the data. Only those fields marked or detected as changed are updated.
If the data source supports transactions, you can make the Update call (and its corresponding AddNew or Edit call) part of a transaction. For more information about transactions, see the articleTransaction (ODBC) in Visual C++ Programmer’s Guide.
Caution If you call Update without first calling either AddNew or Edit, Update throws a CDBException. If you call AddNew or Edit, you must call Update before you call a Move operation or before you close either the recordset or the data source connection. Otherwise, your changes are lost without notification.
For details on handling Update failures, see the articleRecordset: How Recordsets Update Records (ODBC) in Visual C++ Programmer’s Guide.
Example
See the articleTransaction: Performing a Transaction in a Recordset (ODBC) in Visual C++ Programmer’s Guide.
CRecordset Overview
[解决办法]
Class Members
[解决办法]
Hierarchy Chart
See Also CRecordset::Edit, CRecordset::AddNew, CRecordset::SetFieldDirty, CDBException
[解决办法]
数据得解析, 解析后存到表的对应字段.
了解 SQL语言 INSERT
[解决办法]
参考
MSDN98\SAMPLES\VC98\MFC\DATABASE\ODBCINFO\*.*
?
[解决办法]