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

📄 vcg02.htm

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

            TRACE0("DONE WAITING for datasource.\n");

#endif

        return;

    }

    if (m_dwWait == 0)

    {

        pThreadState->m_bWaitForDataSource++;

        // 1st call; wait for min amount of time

        m_dwWait = m_dwMinWaitForDataSource;

#ifdef _DEBUG

        if (afxTraceFlags & traceDatabase)

            TRACE0("WAITING for datasource.\n");

#endif

    }

    else

    {

        if (m_dwWait == m_dwMinWaitForDataSource)

        {

            // 2nd call; wait max time; put up wait cursor

            m_dwWait = m_dwMaxWaitForDataSource;

            pApp->DoWaitCursor(1);      // BeginWaitCursor

        }

    }

    CWinThread* pThread = AfxGetThread();

    DWORD clockFirst = GetTickCount();

    while (GetTickCount() - clockFirst < m_dwWait)

    {

        MSG msg;

        if (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))

        {

            TRY

            {

                pThread->PumpMessage();

            }

            CATCH_ALL

            {

                TRACE0("Error: exception in OnWaitForDataSource - continuing.\n");

                DELETE_EXCEPTION;

            }

            END_CATCH_ALL

        }

        else

            pThread->OnIdle(-1);

    }

}</FONT></PRE>

<BR>

<A NAME="E69E39"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B><I>CRecordset</I></B></FONT></CENTER></H4>

<BR>

<P>The CRecordset object is used to manage recordsets. This object is often used with the CDatabase and CRecordView objects. The member functions in the CRecordset object offer a powerful set of database record manipulation tools.

<BR>

<P>The CRecordset object is derived from the CObject base class. Figure 2.3 shows the class hierarchy for the CRecordset class.

<BR>

<P><B><A HREF="02vcg03.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/02vcg03.gif">Figure 2.3. The CRecordset class hierarchy.</A></B>

<BR>

<P>The CRecordset class object has a number of member functions. These functions are divided into the following seven categories:

<BR>

<UL>

<LI>Data members: The data members of the CRecordset class hold information that is used when you're working directly with the database that the CRecordset object has been attached to.

<BR>

<BR>

<LI>Construction: The constructor and a set of database open/close functions form the construction members.

<BR>

<BR>

<LI>Recordset attributes: Thirteen functions are used to obtain information about the recordset that the CRecordset object has been attached to.

<BR>

<BR>

<LI>Recordset update operations: The four CRecordset update operation members allow for transaction processing.

<BR>

<BR>

<LI>Recordset navigation operations: The five CRecordset navigation operation functions allow for moving throughout the records contained within the recordset.

<BR>

<BR>

<LI>Other recordset operations: The eight other CRecordset operation functions provide miscellaneous functionality.

<BR>

<BR>

<LI>Recordset overrides: Five overridable functions are provided to let the programmer customize the functionality of the CRecordset object.

<BR>

<BR>

</UL>

<P>The following sections take a closer look at the members of this class. I don't cover the members of the CObject class (which CRecordset is derived from) in this book. Refer to the Visual C++ documentation (either the manuals or the online help system) for full information about the CObject class.

<BR>

<BR>

<A NAME="E70E8"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Data Members</B></FONT></CENTER></H5>

<BR>

<P>There are a number of data members in the CRecordset object:

<BR>

<UL>

<LI>The m_hstmt member variable contains the ODBC statement handle for the recordset. This variable has a type of HSTMT.

<BR>

<BR>

<LI>The m_nFields member variable contains the number of field data members (the number of columns retrieved from) in the recordset. This variable has a type of UINT.

<BR>

<BR>

<LI>The m_nParams member variable contains the number of parameter data members in the recordset. This variable has a type of UINT.

<BR>

<BR>

<LI>The m_strFilter variable contains a CString that contains an SQL WHERE clause. This CString will be used as a filter to select only records that meet the specified search criteria.

<BR>

<BR>

<LI>The m_strSort variable contains a CString that contains an SQL ORDER BY clause. This CString will be used to control the sorting of the retrieved records.

<BR>

<BR>

</UL>

<BR>

<A NAME="E70E9"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Construction/Destruction</B></FONT></CENTER></H5>

<BR>

<P>Three member functions deal directly with CRecordset construction: CRecordset(), Open(), and Close(). There also is the default destructor, which I won't document here because it's never called by an application. The following paragraphs describe each construction member function and, where applicable, give examples of usage.

<BR>

<P>The CRecordset() function is the constructor for the CRecordset class object. CRecordset()<B> </B>takes one parameter, and, because it's a constructor, it has no specified return value. CRecordset() has the following prototype:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">void CRecordset(CDatabase * pDatabase = NULL);</FONT></PRE>

<P>The default operation is to create and initialize the CRecordset object. If pDatabase is specified, this CDatabase object will be used with the CRecordset object. If the pDatabase pointer is NULL, the constructor will create a default CDatabase member class.

<BR>

<P>If you create a derived class, the derived class must have its own constructor. Your constructor will then call the CRecordset::CRecordset() constructor, passing the appropriate parameter.

<BR>

<P>The Open() function is used to run a query that will return a recordset to the application. Open() takes three parameters and has no return value. Open() has the following prototype:

<BR>

<PRE>

<FONT COLOR="#000080">virtual BOOL Open(

    UINT nOpenType = snapshot,  // Either dynaset, snapshot, or forwardOnly

    LPCSTR lpszSql = NULL,      // NULL, table name, SELECT, or CALL statement

    DWORD dwOptions = none);    // None, appendOnly, or readOnly</FONT></PRE>

<P>The default operation for Open() is to open a datasource. Open() will throw a CDBException, CMemoryException, or CFileException if there are errors.

<BR>

<P>The<B> </B>Close() function is used to close the currently open recordset. If no recordset is open, this function simply returns. After calling Close(), it's possible to then re-call Open() to reopen the recordset, thereby reusing the CRecordset object. The Close() function takes no parameters and has no return value. Close() has the following prototype:

<BR>

<BR>

<PRE>

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

<P>The default operation for Close() is to close the recordset and the ODBC HSTMT that was associated with the recordset.

<BR>

<BR>

<A NAME="E70E10"></A>

<H5 ALIGN=CENTER>

<CENTER>

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

<BR>

<P>Thirteen member functions deal directly with CRecordset attributes. These member functions are listed here:

<BR>

<UL>

<UL>

<P>CanAppend()

</UL></UL>

<UL>

<UL>

<P>CanRestart()

</UL></UL>

<UL>

<UL>

<P>CanScroll()

</UL></UL>

<UL>

<UL>

<P>CanTransact()

</UL></UL>

<UL>

<UL>

<P>CanUpdate()

</UL></UL>

<UL>

<UL>

<P>GetRecordCount()

</UL></UL>

<UL>

<UL>

<P>GetStatus()

</UL></UL>

<UL>

<UL>

<P>GetTableName()

</UL></UL>

<UL>

<UL>

<P>GetSQL()

</UL></UL>

<UL>

<UL>

<P>IsOpen()

</UL></UL>

<UL>

<UL>

<P>IsBOF()

</UL></UL>

<UL>

<UL>

<P>IsEOF()

</UL></UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<P>IsDeleted()

<BR>

</BLOCKQUOTE></BLOCKQUOTE>

<P>With these member functions, applications can obtain information about the recordset.

<BR>

<P>The CanAppend() function is used to determine whether or not new records can be appended to the end of the recordset. Records are added by using the AddNew() function. CanAppend() takes no parameters and returns a nonzero value if the recordset can have records appended. CanAppend() has the following prototype:

<BR>

<BR>

<PRE>

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

<P>Typically, CanAppend() is called to enable or disable the user interface's record append commands and tools.

<BR>

<P>The CanRestart() function is used to determine whether the query can be restarted. CanRestart() takes no parameters and returns a nonzero value if the query can be restarted. CanRestart() has the following prototype:

<BR>

<BR>

<PRE>

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

<P>The CanRestart() function is usually called prior to calling the Requery() member function.

<BR>

<P>The CanScroll() function is used to determine whether the recordset allows scrolling. CanScroll() takes no parameters and returns a nonzero value if the recordset allows scrolling. CanScroll() has the following prototype:

<BR>

<BR>

<PRE>

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

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

<BR>

<BR>Not all recordsets allow scrolling.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>The CanTransact() function is used to determine whether the recordset supports transactions. CanTransact() takes no parameters and returns a nonzero value if transactions are supported. CanTransact() has the following prototype:

<BR>

<BR>

<PRE>

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

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>NOTE</B>

⌨️ 快捷键说明

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