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

📄 vcg15.htm

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

static char BASED_CODE THIS_FILE[] = __FILE__;

#endif

#define new DEBUG_NEW

/////////////////////////////////////////////////////////////////////////////

// Lines deleted ...

BOOL CRecordView::OnMove(UINT nIDMoveCommand)

{

<B>// First, there are checks to make sure that we are not waiting for</B>

<B>// the datasource:</B>

    if (CDatabase::InWaitForDataSource())

    {

#ifdef _DEBUG

        if (afxTraceFlags &amp; 0x20)

            TRACE0(&quot;Warning: ignored move request\n&quot;);

#endif // _DEBUG

        return TRUE;

    }

<B>// If we're not waiting for the datasource, we get the recordset.</B>

<B>// Then we check to see if we can update the current database. We</B>

<B>// then tell the recordset that we're going to edit (using Edit())</B>

<B>// the current record. We get the user's edits (if any) and then</B>

<B>// update (using Update()) the record in the recordset.</B>

    CRecordset* pSet = OnGetRecordset();

    if (pSet-&gt;CanUpdate())

    {

        pSet-&gt;Edit();

        if (!UpdateData())

            return TRUE;

        pSet-&gt;Update();

    }

<B>// The next step depends on what the move is. There are four</B>

<B>// choices: next record, previous record, first record, and</B>

<B>// last record. The switch() block manages these four choices.</B>

    switch (nIDMoveCommand)

    {

        case ID_RECORD_PREV:

            pSet-&gt;MovePrev();

            if (!pSet-&gt;IsBOF())

                break;

        case ID_RECORD_FIRST:

            pSet-&gt;MoveFirst();

            break;

        case ID_RECORD_NEXT:

            pSet-&gt;MoveNext();

            if (!pSet-&gt;IsEOF())

                break;

            if (!pSet-&gt;CanScroll())

            {

                // Clear out screen since we're sitting on EOF

                pSet-&gt;SetFieldNull(NULL);

                break;

            }

        case ID_RECORD_LAST:

            pSet-&gt;MoveLast();

            break;

        default:

            // Unexpected case value

            ASSERT(FALSE);

    }

<B>// Once the specified move has been made, we simply update</B>

<B>// the view's display of the record (for the user) and return.</B>

    // Show results of move operation

    UpdateData(FALSE);

    return TRUE;

}</FONT></PRE>

<P>Listing 15.2, the CRecordView::OnMove() handler, shows what the default action for OnMove() is. In the original handler, the current record (if there is one) is updated, and then the new record (if there is one) is loaded. At OnMove()'s completion, the view is updated to reflect the changes.

<BR>

<P>Listing 15.3 shows the new OnMove() handler. Much like the default OnMove(), this version updates the current records (all 10 of them) and then loads 10 &quot;new&quot; records. The facility can handle only single moves at a time, so nine of the 10 reads aren't necessary and could be replaced with assignments to move the currently loaded data to the new locations.

<BR>

<P>Listing 15.3 differs from Listing 15.1 in that the OnMove() handler doesn't call other functions that have been written. (In other words, it can run on its own.)

<BR>

<P>

<FONT COLOR="#000080"><B>Listing 15.3. The OnMove() handler.</B></FONT>

<BR>

<PRE>

<FONT COLOR="#000080"><B>BOOL    CContinView::OnMove(UINT nIDMoveCommand)</B>

<B>{</B>

<B>int        i;</B>

<B>int        nStepBack;</B>

<B>//---------START OF DBVIEW.CPP OnMove()...</B>

<B>// First, there are checks to make sure that we aren't waiting for</B>

<B>// the datasource:</B>

<B>    if (CDatabase::InWaitForDataSource())</B>

<B>    {</B>

<B>#ifdef _DEBUG</B>

<B>        if (afxTraceFlags &amp; 0x20)</B>

<B>            TRACE0(&quot;Warning: ignored move request\n&quot;);</B>

<B>#endif // _DEBUG</B>

<B>        return TRUE;</B>

<B>    }</B>

<B>// If we aren't waiting for the datasource, we get the recordset.</B>

<B>// Then we check to see if we can update the current database. We</B>

<B>// then tell the recordset that we are going to edit (using Edit())</B>

<B>// the current record. We get the user's edits (if any) and then</B>

<B>// update (using Update()) the record in the recordset.</B>

<B>    if (m_pSet-&gt;CanUpdate())</B>

<B>    {</B>

<B>        if (!UpdateData())</B>

<B>            return TRUE;</B>

<B>        nStepBack = 0;</B>

<B>        for (i = 0; i &lt; 10; i++)</B>

<B>        {// Save the current record and then get next one!</B>

<B>            m_pSet-&gt;Edit();</B>

<B>            m_pSet-&gt;m_English_Name = m_EnglishName[i];</B>

<B>            m_pSet-&gt;m_Product_Name = m_ProductName[i];</B>

<B>            m_pSet-&gt;m_Unit_Price = m_UnitPrice[i];</B>

<B>            m_pSet-&gt;m_Units_In_Stock = m_UnitsInStock[i];</B>

<B>            m_pSet-&gt;m_Units_On_Order = m_UnitsOnOrder[i];</B>

<B>            m_pSet-&gt;Update();</B>

<B>            if (!m_pSet-&gt;IsEOF())</B>

<B>            {</B>

<B>                TRY</B>

<B>                {// Use old-style exceptions for Visual C++ 1.5x</B>

<B>                    m_pSet-&gt;MoveNext();</B>

<B>                    --nStepBack;</B>

<B>                }</B>

<B>                CATCH(CDBException, e)</B>

<B>                {// Died. Should use message box to user!</B>

<B>                       TRACE(&quot;MoveNext() fail Ret = %d Error '%s', cause '%s'\n&quot;,</B>

<B>                           e-&gt;m_nRetCode,</B>

<B>                           (const char *)e-&gt;m_strError,</B>

<B>                           (const char *)e-&gt;m_strStateNativeOrigin);</B>

<B>                }</B>

<B>                END_CATCH</B>

<B>            }</B>

<B>            else</B>

<B>            {</B>

<B>                break;</B>

<B>            }</B>

<B>        }</B>

<B>//        Restore the record pointer! Take nStepBack giant steps back!</B>

<B>        TRY</B>

<B>        {// Use old-style exceptions for Visual C++ 1.5x</B>

<B>            m_pSet-&gt;Move(nStepBack);  // Back to original record...</B>

<B>        }</B>

<B>        CATCH(CDBException, e)</B>

<B>        {// Died. Should use message box to user!</B>

<B>               TRACE(&quot;Move(nStepBack) failed Ret = %d Error '%s', cause '%s'\n&quot;,</B>

<B>                   e-&gt;m_nRetCode,</B>

<B>                   (const char *)e-&gt;m_strError,</B>

<B>                   (const char *)e-&gt;m_strStateNativeOrigin);</B>

<B>        }</B>

<B>        END_CATCH</B>

<B>    }</B>

<B>// The next step depends on what the move is. There are four</B>

<B>// choices: next record, previous record, first record, and</B>

<B>// last record. The switch() block manages these four choices.</B>

<B>    switch (nIDMoveCommand)</B>

<B>    {</B>

<B>        case ID_RECORD_PREV:</B>

<B>            m_pSet-&gt;MovePrev();</B>

<B>            if (!m_pSet-&gt;IsBOF())</B>

<B>                break;</B>

<B>        case ID_RECORD_FIRST:</B>

<B>            m_pSet-&gt;MoveFirst();</B>

<B>            break;</B>

<B>        case ID_RECORD_NEXT:</B>

<B>            m_pSet-&gt;MoveNext();</B>

<B>            if (!m_pSet-&gt;IsEOF())</B>

<B>                break;</B>

<B>            if (!m_pSet-&gt;CanScroll())</B>

<B>            {</B>

<B>                // Clear out screen since we're sitting on EOF</B>

<B>                m_pSet-&gt;SetFieldNull(NULL);</B>

<B>                break;</B>

<B>            }</B>

<B>        case ID_RECORD_LAST:</B>

<B>            m_pSet-&gt;MoveLast();</B>

<B>            break;</B>

<B>        default:</B>

<B>            // Unexpected case value</B>

<B>            ASSERT(FALSE);</B>

<B>    }</B>

<B>//---------END OF DBVIEW.CPP OnMove()...</B>

<B>nStepBack = 0;</B>

<B>    for (i = 0; i &lt; 10; i++)</B>

<B>    {// Save the current record and then get next one!</B>

<B>        m_EnglishName[i] = m_pSet-&gt;m_English_Name;</B>

<B>        m_ProductName[i] = m_pSet-&gt;m_Product_Name;</B>

<B>        m_UnitPrice[i] = m_pSet-&gt;m_Unit_Price;</B>

<B>        m_UnitsInStock[i] = m_pSet-&gt;m_Units_In_Stock;</B>

<B>        m_UnitsOnOrder[i] = m_pSet-&gt;m_Units_On_Order;</B>

<B>        if (!m_pSet-&gt;IsEOF())</B>

<B>        {</B>

<B>            TRY</B>

<B>            {// Use old-style exceptions for Visual C++ 1.5x</B>

<B>                m_pSet-&gt;MoveNext();</B>

<B>                --nStepBack;</B>

<B>            }</B>

<B>            CATCH(CDBException, e)</B>

<B>            {// Died. Should use message box to user!</B>

<B>                   TRACE(&quot;MoveNext() failed Ret = %d Error '%s', cause '%s'\n&quot;,</B>

<B>                       e-&gt;m_nRetCode,</B>

<B>                       (const char *)e-&gt;m_strError,</B>

<B>                       (const char *)e-&gt;m_strStateNativeOrigin);</B>

<B>            }</B>

<B>            END_CATCH</B>

<B>        }</B>

<B>        else</B>

<B>        {</B>

<B>            m_pSet-&gt;SetFieldNull(NULL);</B>

<B>        }</B>

<B>    }</B>

<B>//    Restore the record pointer! Take nStepBack giant steps back!</B>

<B>    TRY</B>

<B>    {// Use old-style exceptions for Visual C++ 1.5x</B>

<B>        m_pSet-&gt;Move(nStepBack);</B>

<B>    }</B>

<B>    CATCH(CDBException, e)</B>

<B>    {// Died. Should use message box to user!</B>

⌨️ 快捷键说明

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