home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / dbfetch / datadlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  241 lines

  1. // datadlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "dbfetch.h"
  15. #include "datadlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDataDialog dialog
  25.  
  26.  
  27. CDataDialog::CDataDialog(CWnd* pParent /*=NULL*/)
  28.     : CDialog(CDataDialog::IDD, pParent)
  29. {
  30.     //{{AFX_DATA_INIT(CDataDialog)
  31.         // NOTE: the ClassWizard will add member initialization here
  32.     //}}AFX_DATA_INIT
  33.  
  34.     m_prs = NULL;
  35. }
  36.  
  37.  
  38. void CDataDialog::SetRecordset(CDynamicBulkSet* prs)
  39. {
  40.     ASSERT(prs != NULL);
  41.  
  42.     m_prs = prs;
  43. }
  44.  
  45. int CDataDialog::DoModal()
  46. {
  47.     ASSERT(m_prs != NULL);
  48.  
  49.     return CDialog::DoModal();
  50. }
  51.  
  52. BOOL CDataDialog::OnInitDialog()
  53. {
  54.     BOOL bReturn = CDialog::OnInitDialog();
  55.  
  56.     AddColumns();
  57.     FillData();
  58.  
  59.     return bReturn;
  60. }
  61.  
  62. void CDataDialog::AddColumns()
  63. {
  64.     ASSERT(m_prs->IsOpen());
  65.  
  66.     // declare a CODBCFieldInfo structure and get a count
  67.     // of the number of columns in the database
  68.     CODBCFieldInfo info;
  69.     int nColumns = m_prs->GetODBCFieldCount();
  70.  
  71.     // for each column, retrieve the field name and
  72.     // insert the field name in the header
  73.     for (int nNum = 0; nNum < nColumns; nNum++)
  74.     {
  75.         m_prs->GetODBCFieldInfo(nNum, info);
  76.         if (m_listData.InsertColumn(nNum, info.m_strName,
  77.             LVCFMT_LEFT, 80) != nNum)
  78.         {
  79.             ASSERT(FALSE);
  80.             return;
  81.         }
  82.     }
  83. }
  84.  
  85. void CDataDialog::FillData()
  86. {
  87.     ASSERT(m_prs->IsOpen());
  88.  
  89.     // Make sure there are no items
  90.     m_listData.DeleteAllItems();
  91.  
  92.     // Validate that there is data
  93.     if (m_prs->IsEOF() && m_prs->IsBOF())
  94.     {
  95.         // Disable all the controls
  96.         GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
  97.         GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
  98.         GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
  99.         GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
  100.  
  101.         // Put up a warning dialog and return
  102.         CString strError;
  103.         strError.LoadString(IDS_ERROR_NODATA);
  104.         AfxMessageBox(strError);
  105.         return;
  106.     }
  107.     else
  108.     {
  109.         // Enable all the controls
  110.         GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
  111.         GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
  112.         GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
  113.         GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
  114.     }
  115.  
  116.  
  117.     long* rgLength;
  118.     LPSTR rgData;
  119.  
  120.     // Need to use this to convert LPSTR to UNICODE
  121.     CString strData;
  122.  
  123.     int nFields = m_prs->GetODBCFieldCount();
  124.     int nRowsFetched = m_prs->GetRowsFetched();
  125.  
  126.     // Display 1 rowset of data by field
  127.     for (int nField = 0; nField < nFields; nField++)
  128.     {
  129.         // set up the correct data and length arrays
  130.         rgData = (LPSTR)m_prs->m_ppvData[nField];
  131.         rgLength = (long*)m_prs->m_ppvLengths[nField];
  132.  
  133.         for (int nRow = 0; nRow < nRowsFetched; nRow++)
  134.         {
  135.             int nStatus = m_prs->GetRowStatus(nRow + 1);
  136.  
  137.             // Get the string to display
  138.             if (nStatus == SQL_ROW_DELETED)
  139.                 strData = _T("<DELETED>");
  140.             else if (nStatus == SQL_ROW_NOROW)
  141.                 // Shouldn't get this since rows fetched is checked
  142.                 strData = _T("<NO_ROW>");
  143.             else if (rgLength[nRow] == SQL_NULL_DATA)
  144.                 strData = _T("<NULL>");
  145.             else
  146.                 strData = &rgData[nRow * MAX_TEXT_LEN];
  147.  
  148.             // Set the string (if first column must add)
  149.             if (nField == 0)
  150.                 m_listData.InsertItem(nRow, strData);
  151.             else
  152.             {
  153.                 m_listData.SetItem(nRow, nField, LVIF_TEXT,
  154.                     strData, -1, 0, 0, 0);
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. void CDataDialog::DoDataExchange(CDataExchange* pDX)
  161. {
  162.     CDialog::DoDataExchange(pDX);
  163.     //{{AFX_DATA_MAP(CDataDialog)
  164.     DDX_Control(pDX, IDC_DATALIST, m_listData);
  165.     //}}AFX_DATA_MAP
  166. }
  167.  
  168.  
  169. BEGIN_MESSAGE_MAP(CDataDialog, CDialog)
  170.     //{{AFX_MSG_MAP(CDataDialog)
  171.     ON_BN_CLICKED(IDC_FIRST, OnFirst)
  172.     ON_BN_CLICKED(IDC_LAST, OnLast)
  173.     ON_BN_CLICKED(IDC_NEXT, OnNext)
  174.     ON_BN_CLICKED(IDC_PREV, OnPrev)
  175.     //}}AFX_MSG_MAP
  176. END_MESSAGE_MAP()
  177.  
  178. /////////////////////////////////////////////////////////////////////////////
  179. // CDataDialog message handlers
  180.  
  181. void CDataDialog::OnFirst()
  182. {
  183.     ASSERT(m_prs->IsOpen());
  184.  
  185.     m_prs->MoveFirst();
  186.  
  187.     FillData();
  188.  
  189.     // Disable the correct controls
  190.     GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
  191. }
  192.  
  193. void CDataDialog::OnLast()
  194. {
  195.     ASSERT(m_prs->IsOpen());
  196.  
  197.     m_prs->MoveLast();
  198.     FillData();
  199.  
  200.     // Disable the correct controls
  201.     GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
  202. }
  203.  
  204. void CDataDialog::OnNext()
  205. {
  206.     ASSERT(m_prs->IsOpen());
  207.  
  208.     m_prs->MoveNext();
  209.  
  210.     if (m_prs->IsEOF())
  211.     {
  212.         // Disable the correct controls
  213.         GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
  214.  
  215.         CString strError;
  216.         strError.LoadString(IDS_ERROR_EOF);
  217.         AfxMessageBox(strError);
  218.     }
  219.     else
  220.         FillData();
  221. }
  222.  
  223. void CDataDialog::OnPrev()
  224. {
  225.     ASSERT(m_prs->IsOpen());
  226.  
  227.     m_prs->MovePrev();
  228.  
  229.     if (m_prs->IsBOF())
  230.     {
  231.         // Disable the correct controls
  232.         GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
  233.  
  234.         CString strError;
  235.         strError.LoadString(IDS_ERROR_BOF);
  236.         AfxMessageBox(strError);
  237.     }
  238.     else
  239.         FillData();
  240. }
  241.