📄 vcg02.htm
字号:
<BR>
<BR>Many, but not all, ODBC drivers support transactions.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The CanUpdate() function is used to determine whether the recordset supports updating. Updating would typically fail if the underlying database were opened in read-only mode. CanUpdate() takes no parameters and returns a nonzero value if the recordset supports updating. CanUpdate() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">BOOL CanUpdate();</FONT></PRE>
<P>The most common reason that a recordset can't be updated when the ODBC driver supports updating is that it has been opened in read-only mode. Read-only mode offers faster access (there is no need to perform record locking) at the expense of being able to update the recordset.
<BR>
<P>The GetRecordCount() function is used to determine the number of records in the current recordset. GetRecordCount() takes no parameters and returns the number of records in the recordset—a –1 value if the number of records can't be determined and a zero value if there are no records in the recordset. GetRecordCount() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">long GetRecordCount();</FONT></PRE>
<P>The number of records in a recordset can be determined only if the application scrolls through the entire recordset. The count of records is maintained as a counter that is incremented with each forward read. The true total number of records is known only after the application has scrolled past the last record. Using MoveLast() won't affect the record counter.
<BR>
<P>The GetStatus() function is used to obtain status information about the current recordset. GetStatus() takes one parameter, a reference to the CRecordsetStatus structure, and has no return value. GetStatus() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void GetStatus(CRecordsetStatus & rsStatus);</FONT></PRE>
<P>The members of the CRecordsetStatus class are shown in the following code fragment:
<BR>
<PRE>
<FONT COLOR="#000080">struct CRecordsetStatus
{
long m_lCurrentRecord; // Zero-based index of current record
// if the current record is known, or
// AFX_CURRENT_RECORD_UNDEFINED if the
// current record is undefined.
BOOL m_bRecordCountFinal; // Nonzero if the total number of records
// in the recordset has been determined.
};</FONT></PRE>
<P>The GetTableName() function is used to fetch the name of the recordset table. GetTableName() takes no parameters and returns a CString reference. GetTableName() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">CString & GetTableName();</FONT></PRE>
<P>The CString returned won't contain a name if the recordset was based on a join or if the recordset was created by a call to a stored procedure.
<BR>
<P>The GetSQL() function is used to return a CString reference that contains the current SQL statement. The SQL statement is the SELECT statement used to generate the recordset. GetSQL() takes no parameters and returns a CString reference. GetSQL() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">CString & GetSQL();</FONT></PRE>
<P>The returned SQL string usually will have been modified by the system to include any filtering (a WHERE clause) and sorting (an ORDER BY clause).
<BR>
<P>The IsOpen() function is used to determine whether the CRecordset Open() or Requery() functions have been called and whether the recordset has been closed. IsOpen() takes no parameters and returns a nonzero value if there has been a call to Open() or Requery() without an intervening call to Close(). IsOpen() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">BOOL IsOpen();</FONT></PRE>
<P>Your application should check the IsOpen() function prior to calling Open().
<BR>
<P>The IsBOF() function is used to check whether the current record is the first record in the dataset. IsBOF() takes no parameters and returns a nonzero value if the recordset is empty or if the application has scrolled to before the first record in the recordset. IsBOF() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">BOOL IsBOF();</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>CAUTION</B>
<BR>
<BR>The IsBOF() function should be called prior to scrolling backward in a recordset. Scrolling backward when there are no records in the recordset or when the current record pointer is before the first record in the recordset causes an error.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The IsEOF() function is used to determine whether the current record is the last record in the dataset. IsEOF() takes no parameters and returns a nonzero value if the recordset is empty or if the application has scrolled to after the last record in the recordset. IsEOF() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">BOOL IsEOF();</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>CAUTION</B>
<BR>
<BR>The IsEOF() function should be called prior to scrolling forward in a recordset. Scrolling forward when there are no records in the recordset or when the current record pointer is after the last record in the recordset causes an error.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>The IsDeleted() function is used to determine whether the current record in the recordset has been deleted. IsDeleted() takes no parameters and returns a nonzero value if the current record has been marked as deleted. IsDeleted() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">BOOL IsDeleted();</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>CAUTION</B>
<BR>
<BR>It's considered an error to update or delete a record that has been marked as deleted.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E70E11"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Recordset Update Operations</B></FONT></CENTER></H5>
<BR>
<P>Four member functions deal directly with CRecordset updating:
<BR>
<UL>
<UL>
<P>AddNew()
</UL></UL>
<UL>
<UL>
<P>Delete()
</UL></UL>
<UL>
<UL>
<P>Edit()
</UL></UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>Update()
<BR>
</BLOCKQUOTE></BLOCKQUOTE>
<P>With these member functions, applications can add, delete, and edit records in the recordset.
<BR>
<P>The AddNew() function is used to prepare a new record to be added to the recordset. This record's contents must then be filled in by the application. After the new record's contents are filled in, Update() should be called to write the record. AddNew() takes no parameters and has no return value. AddNew() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void AddNew();</FONT></PRE>
<P>The AddNew() function throws a CDBException or a CFileException if an error occurs (such as trying to add records to a dataset that is read-only). AddNew() can be used as part of a transaction if the dataset supports transactions.
<BR>
<P>The Delete() function is used to delete the current record from the recordset. After calling Delete(), you must explicitly scroll to another record. Delete() takes no parameters and has no return value. Delete() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void Delete();</FONT></PRE>
<P>The Delete() function will throw a CDBException if an error occurs (such as trying to delete records in a dataset that is read-only). Delete() can be used as part of a transaction if the dataset supports transactions.
<BR>
<P>The Edit() function is used to prepare the current record for editing. The Edit() function will save the current record's current values. If you call Edit(), make changes, and then call Edit() a second time (without a call to Update()), the changes will be lost, and the record will be restored to the original values. Edit() takes no parameters and has no return value. Edit() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void Edit();</FONT></PRE>
<P>The Edit() function throws a CDBException if an error occurs. Edit() can be used as part of a transaction if the dataset supports transactions.
<BR>
<P>The Update() function is used to write the record that has been added to or edited by other recordset update operations. Update() takes no parameters and returns a nonzero value if a record was actually updated or a zero if no records were updated. Update() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">BOOL Update();</FONT></PRE>
<P>The Update() function throws a CDBException if an error occurs. Update() can be used as part of a transaction if the dataset supports transactions.
<BR>
<BR>
<A NAME="E70E12"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Recordset Navigation Operations</B></FONT></CENTER></H5>
<BR>
<P>Five member functions deal directly with CRecordset record navigation:
<BR>
<UL>
<UL>
<P>Move()
</UL></UL>
<UL>
<UL>
<P>MoveFirst()
</UL></UL>
<UL>
<UL>
<P>MoveLast()
</UL></UL>
<UL>
<UL>
<P>MoveNext()
</UL></UL>
<BLOCKQUOTE>
<BLOCKQUOTE>
<P>MovePrev()
<BR>
</BLOCKQUOTE></BLOCKQUOTE>
<P>You should also refer to the IsBOF() and IsEOF() functions, described in the section "Recordset Attributes." With these member functions, applications can move forward, backward, to a specific record, to the beginning of a recordset, and to the end of a recordset.
<BR>
<P>The Move() function is used to move to a specific record in the recordset, relative to the current record. This function allows random movement in the recordset. Move() takes one parameter and has no return value. Move() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void Move(long lRows);</FONT></PRE>
<P>Use a negative parameter value to move backward from the current record. The Move() function throws a CDBException, CFileException, or CMemoryException if it fails.
<BR>
<P><B>WARNING</B>
<BR>
<BR>Don't call any move function for a recordset that doesn't have any records (if both IsEOF() and IsBOF() return nonzero, the recordset is empty).
<P>The MoveFirst() function is used to move to the first record in the recordset. MoveFirst() takes no parameters and has no return value. MoveFirst() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void MoveFirst();</FONT></PRE>
<P>The MoveFirst() function throws a CDBException, CFileException, or CMemoryException if it fails.
<BR>
<P>The MoveLast() function is used to move to the last record in the recordset. MoveLast() takes no parameters and has no return value. MoveLast() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void MoveLast();</FONT></PRE>
<P>The MoveLast() function throws a CDBException, CFileException, or CMemoryException if it fails.
<BR>
<P>The MoveNext() function is used to move to the next record in the recordset. If you're positioned after the last record in the recordset, don't call MoveNext(). MoveNext() takes no parameters and has no return value. MoveNext() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void MoveNext();</FONT></PRE>
<P>The MoveNext() function throws a CDBException, CFileException, or CMemoryException if it fails.
<BR>
<P>The MovePrev() function is used to move to the previous record in the recordset. If you're positioned before the first record in the recordset, don't call MovePrev(). MovePrev() takes no parameters and has no return value. MovePrev() has the following prototype:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">void MovePrev();</FONT></PRE>
<P>The MovePrev() function throws a CDBException, CFileException, or CMemoryException if it fails.
<BR>
<BR>
<A NAME="E70E13"></A>
<H5 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Other Recordset Operations</B></FONT></CENTER></H5>
<BR>
<P>Eight member functions deal directly with CRecordset operations:
<BR>
<UL>
<UL>
<P>Cancel()
</UL></UL>
<UL>
<UL>
<P>IsFieldDirty()
</UL></UL>
<UL>
<UL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -