⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vcg02.htm

📁 Visual C++与数据库的连接经典实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<P>IsFieldNull()

</UL></UL>

<UL>

<UL>

<P>IsFieldNullable()

</UL></UL>

<UL>

<UL>

<P>Requery()

</UL></UL>

<UL>

<UL>

<P>SetFieldDirty()

</UL></UL>

<UL>

<UL>

<P>SetFieldNull()

</UL></UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<P>SetLockingMode()

<BR>

</BLOCKQUOTE></BLOCKQUOTE>

<P>With these member functions, applications can perform miscellaneous operations on recordsets.

<BR>

<P>The Cancel() function is used to cancel a pending asynchronous operation. Cancel() takes no parameters and has no return value. Cancel() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void Cancel();</FONT></PRE>

<P>The default operation, should there be no pending asynchronous operation, is simply to return.

<BR>

<P>The IsFieldDirty() function is used to determine whether a specified field has been changed. IsFieldDirty() takes one parameter&#151;a pointer to a field data member&#151;and returns a nonzero value if the field has, in fact, been modified. IsFieldDirty() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">BOOL IsFieldDirty(void * pField);</FONT></PRE>

<P>If the pField pointer parameter is NULL, all fields in the record are checked.

<BR>

<P>The IsFieldNull() function is used to determine whether a specified field is currently null (contains no value). IsFieldNull() takes one parameter&#151;a pointer to a field data member&#151;and returns a nonzero value if the field is, in fact, null. IsFieldNull() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">BOOL IsFieldNull(void * pField);</FONT></PRE>

<P>If the pField pointer parameter is NULL, all fields in the record are checked. Note that the C/C++ NULL is different from the SQL null.

<BR>

<P>The IsFieldNullable() function is used to determine whether a specified field can be set to null (containing no value). IsFieldNullable() takes one parameter&#151;a pointer to a field data member&#151;and returns a nonzero value if the field can be set to null. IsFieldNullable() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">BOOL IsFieldNullable(void * pField);</FONT></PRE>

<P>If the pField pointer parameter is NULL, all fields in the record are checked. Note that the C/C++ NULL is different from the SQL null.

<BR>

<P>The Requery() function is used to refresh the recordset. A call to the function CanRestart() should be made prior to calling Requery(). Requery() takes no parameters and returns a nonzero value if the refresh was successful. Requery() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">BOOL Requery();</FONT></PRE>

<P>The Requery() function throws a CDBException, CFileException, or CMemoryException if it fails.

<BR>

<P>The SetFieldDirty() function is used to modify the dirty flag for a specified field. SetFieldDirty() takes two parameters&#151;a pointer to a field data member and a Boolean value specifying the new value for the dirty flag. SetFieldDirty() has no return value and has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void SetFieldDirty(void * pField, BOOL bDirty = TRUE);</FONT></PRE>

<P>If the pField pointer parameter is NULL, all fields in the record are marked with the value of the bDirty parameter. Note that the C/C++ NULL is different from the SQL null.

<BR>

<P>The SetFieldNull() function is used to modify the null flag for a specified field. SetFieldNull() takes two parameters&#151;a pointer to a field data member and a Boolean value specifying the new value for the dirty flag. SetFieldNull() has no return value and has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void SetFieldNull(void * pField, BOOL bDirty = TRUE);</FONT></PRE>

<P>If the pField pointer parameter is NULL, all fields in the record are marked with the value of the bDirty parameter. Note that the C/C++ NULL is different from the SQL null.

<BR>

<P>The SetLockingMode() function is used to change the record locking mode. SetLockingMode() takes one parameter&#151;nMode, which must be either optimistic or pessimistic. SetLockingMode() has no return value and has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void SetLockingMode(UINT nMode);</FONT></PRE>

<P>The pessimistic mode is more cautious than optimistic mode. Both pessimistic and optimistic are defined in CRecordset. pessimistic mode locks the record as soon as Edit() is called, and optimistic mode locks the record only while the update is being performed.

<BR>

<BR>

<A NAME="E70E14"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Recordset Overridables</B></FONT></CENTER></H5>

<BR>

<P>Applications may override five members to allow control over the recordset:

<BR>

<UL>

<UL>

<P>DoFieldExchange()

</UL></UL>

<UL>

<UL>

<P>GetDefaultConnect()

</UL></UL>

<UL>

<UL>

<P>GetDefaultSQL()

</UL></UL>

<UL>

<UL>

<P>OnSetOptions()

</UL></UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<P>OnWaitForDataSource()

<BR>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The DoFieldExchange() function is used to transfer data to and from the field variables and records in the recordset. If your application is built with AppWizard, a default DoFieldExchange() function will be created. Also, modifications to the AppWizard-created DoFieldExchange() will be done by ClassWizard. DoFieldExchange() takes one parameter and has no return value. DoFieldExchange() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void DoFieldExchange(CFieldExchange * pFX);</FONT></PRE>

<P>The CFieldExchange class object definition is shown in the following code fragment. The actual definition can be found in the AFXDB.H header file.

<BR>

<PRE>

<FONT COLOR="#000080">// CFieldExchange - for field exchange

class CFieldExchange

{

// Attributes

public:

    enum RFX_Operation

    {

        BindParam, // Register user's parameters with ODBC SQLBindParameter

        RebindParam, // Migrate param values to proxy array before requery

        BindFieldToColumn, // Register user's fields with ODBC SQLBindCol

        BindFieldForUpdate, // Temporarily bind columns before

                            // update (via SQLSetPos)

        UnbindFieldForUpdate, // Unbind columns after update (via SQLSetPos)

        Fixup, // Set string lengths and clear status bits

        MarkForAddNew,

        MarkForUpdate,  // Prepare fields and flags for update operation

        Name, // Append dirty field name

        NameValue, // Append dirty name=value

        Value, // Append dirty value or parameter marker

        SetFieldDirty, // Set status bit for changed status

        SetFieldNull,   // Set status bit for null value

        IsFieldDirty, // Return TRUE if field is dirty

        IsFieldNull, // Return TRUE if field is marked NULL

        IsFieldNullable, // Return TRUE if field can hold NULL values

        StoreField, // Archive values of current record

        LoadField,  // Reload archived values into current record

        GetFieldInfoValue,  // General info on a field via pv for field

        GetFieldInfoOrdinal,  // General info on a field via field ordinal

#ifdef _DEBUG

        DumpField,  // Dump bound field name and value

#endif

    };

    UINT m_nOperation;  // Type of exchange operation

    CRecordset* m_prs;  // Recordset handle

// Operations

    enum FieldType

    {

        noFieldType,

        outputColumn,

        param,

    };

// Operations (for implementors of RFX procs)

    BOOL IsFieldType(UINT* pnField);

    // Indicate purpose of subsequent RFX calls

    void SetFieldType(UINT nFieldType);

// Implementation

    CFieldExchange(UINT nOperation, CRecordset* prs, void* pvField = NULL);

    void Default(LPCTSTR szName,

        void* pv, LONG* plLength, int nCType, UINT cbValue, UINT cbPrecision);

    int GetColumnType(int nColumn, UINT* pcbLength = NULL,

        int* pnScale = NULL, int* pnNullable = NULL);

    // Long binary helpers

    long GetLongBinarySize(int nField);

    void GetLongBinaryData(int nField, CLongBinary&amp; lb, long* plSize);

    BYTE* ReallocLongBinary(CLongBinary&amp; lb, long lSizeRequired,

        long lReallocSize);

    // Current type of field

    UINT m_nFieldType;

    // For GetFieldInfo

    CFieldInfo* m_pfi;  // GetFieldInfo return struct

    BOOL m_bFieldFound; // GetFieldInfo search successful

    // For returning status info for a field

    BOOL m_bNull;       // Return result of IsFieldNull(able)/Dirty operation

    BOOL m_bDirty;      // Return result of IsFieldNull(able)/Dirty operation

    CString* m_pstr;    // Field name or destination for building various SQL

                        // clauses

    BOOL m_bField;      // Value to set for SetField operation

    void* m_pvField;    // For indicating an operation on a specific field

    CArchive* m_par;    // For storing/loading copy buffer

    LPCTSTR m_lpszSeparator; // Append after field names

    UINT m_nFields;     // Count of fields for various operations

    UINT m_nParams;     // Count of fields for various operations

    UINT m_nParamFields;    // Count of fields for various operations

    HSTMT m_hstmt;      // For SQLBindParameter on update statement

    long m_lDefaultLBFetchSize;     // For fetching CLongBinary data of

                                    // unknown length

    long m_lDefaultLBReallocSize;   // For fetching CLongBinary data of

                                    // unknown length

#ifdef _DEBUG

    CDumpContext* m_pdcDump;

#endif //_DEBUG

};</FONT></PRE>

<P>A typical AppWizard-created DoFieldExchange() function is shown in the following code fragment. This example is from the sample program shown in Figure 2.1.

<BR>

<PRE>

<FONT COLOR="#000080">void CRecordViewSet::DoFieldExchange(CFieldExchange* pFX)

{

    //{{AFX_FIELD_MAP(CRecordViewSet)

    pFX-&gt;SetFieldType(CFieldExchange::outputColumn);

    RFX_Text(pFX, _T(&quot;[Customer ID]&quot;), m_Customer_ID);

    RFX_Text(pFX, _T(&quot;[Company Name]&quot;), m_Company_Name);

    RFX_Text(pFX, _T(&quot;[Contact Name]&quot;), m_Contact_Name);

    RFX_Text(pFX, _T(&quot;[Contact Title]&quot;), m_Contact_Title);

    RFX_Text(pFX, _T(&quot;[Address]&quot;), m_Address);

    RFX_Text(pFX, _T(&quot;[City]&quot;), m_City);

    RFX_Text(pFX, _T(&quot;[Region]&quot;), m_Region);

    RFX_Text(pFX, _T(&quot;[Postal Code]&quot;), m_Postal_Code);

    RFX_Text(pFX, _T(&quot;[Country]&quot;), m_Country);

    RFX_Text(pFX, _T(&quot;[Phone]&quot;), m_Phone);

    RFX_Text(pFX, _T(&quot;[Fax]&quot;), m_Fax);

    //}}AFX_FIELD_MAP

}</FONT></PRE>

<P>The GetDefaultConnect() function is used to return the default SQL connect string. GetDefaultConnect() takes no parameters and returns a CString reference. GetDefaultConnect() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">CString &amp; GetDefaultConnect();</FONT></PRE>

<P>The default GetDefaultConnect() function created by AppWizard is shown in the following code fragment. This example is from the sample program shown later in this chapter (see the section called &quot;An AppWizard-Generated Program&quot;). It causes ODBC to display an open database dialog box.

<BR>

<PRE>

<FONT COLOR="#000080">CString CRecordViewSet::GetDefaultConnect()

{

    return _T(&quot;ODBC;DSN=MS Access 7.0 Database&quot;);

}</FONT></PRE>

<P>The GetDefaultSQL() function is used to return the default SQL string used to select records from the datasource to be placed in the recordset. GetDefaultSQL() takes no parameters and returns a CString reference. GetDefaultSQL() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">CString &amp; GetDefaultSQL();</FONT></PRE>

<P>The default GetDefaultSQL() function created by AppWizard is shown in the following code fragment. This example is from the sample program shown later in this chapter.

<BR>

<PRE>

<FONT COLOR="#000080">CString CRecordViewSet::GetDefaultSQL()

{

  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -