home *** CD-ROM | disk | FTP | other *** search
- // GetRDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "GetRows.h"
- #include "GetRDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
-
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
-
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
-
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
-
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
-
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
-
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CGetRowsDlg dialog
-
- CGetRowsDlg::CGetRowsDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CGetRowsDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CGetRowsDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CGetRowsDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CGetRowsDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CGetRowsDlg, CDialog)
- //{{AFX_MSG_MAP(CGetRowsDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDD_EXECUTE, OnExecute)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CGetRowsDlg message handlers
-
- BOOL CGetRowsDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Add "About..." menu item to system menu.
-
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
-
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- void CGetRowsDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CGetRowsDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CGetRowsDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
-
- void CGetRowsDlg::OnExecute()
- {
-
- CFileDialog cOpenFile( TRUE,
- _T("MDB"),
- _T("employee.mdb"),
- OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- (_T("Access Files (*.mdb) | *.mdb ||")));
-
-
- //Get the location of database (assume it's the Employee example)
- cOpenFile.DoModal();
-
- try
- {
- // Connect to the Employee database example
- m_cEmpDatabase = m_cDBEngine.OpenDatabase(cOpenFile.m_ofn.lpstrFile);
-
- // Connect the recordset straight to the table.
- m_cEmpRecordSet = m_cEmpDatabase.OpenRecordset(_T("Employees"), dbOpenSnapshot);
-
- // Perform the two GetRows functions
- DoGetRows();
-
- // Reset the recordset
- m_cEmpRecordSet.MoveFirst();
-
- DoGetRowsEx();
-
- }
- catch (CdbException e)
- {
- TCHAR szBuf[256];
-
- wsprintf(szBuf, _T("Error %d : %s\n"), DBERR(e.m_hr), (LPCTSTR) "Couldn't find Employee table.");
- AfxMessageBox(szBuf);
- }
- }
-
-
- // Perform standard GetRows against the Employee table
- void CGetRowsDlg::DoGetRows()
- {
- COleVariant cRows;
- COleVariant varField;
- CString strLBRow;
- TCHAR szId[16];
- LONG lNumRecords;
- LONG lIndex[2];
- HRESULT hResult;
- CListBox *pListBox = (CListBox *)GetDlgItem(IDD_GETROWSLIST);
-
- //Perform GetRows on Employee table
- //This GetRows uses VARIANTS
- cRows = m_cEmpRecordSet.GetRows(MAX_EMP_REC); //arbitrarily get MAX_EMP_REC rows
-
- //Find out how many records were actually retrieved
- //(SafeArrays are 1-based)
- SafeArrayGetUBound(cRows.parray, 2, &lNumRecords);
-
- //Clear the listbox
- pListBox->ResetContent();
-
- for (lIndex[1] = 0; lIndex[1] <= lNumRecords; lIndex[1]++)
- {
- strLBRow.Empty();//Clear the string
-
- lIndex[0] = EMP_ID;
-
- //Use OLE safe array function to access fields
- hResult = SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);
-
- //Watch out for bum variants
- if(FAILED(hResult))
- break;
-
- if(varField.vt == VT_I4) //Gotta be a long
- {
- wsprintf(szId, _T("%d, "), varField.iVal);
- }
- else
- {
- lstrcpy(szId, _T("Unexpected Data Type"));
- }
-
- strLBRow += (LPCTSTR)szId;
-
- //Get last name
- lIndex[0] = EMP_LNAME;
- SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);
- strLBRow += (LPCTSTR)varField.bstrVal;
-
- //Get first name
- strLBRow += _T(", ");
- lIndex[0] = EMP_FNAME;
- SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);
- strLBRow += (LPCTSTR)varField.bstrVal;
-
- pListBox->AddString(strLBRow);
- }
- }
-
-
- // Structure for GetRowsEx
- typedef struct
- {
- LONG lEmpId;
- TCHAR *strLastName;
- TCHAR strFirstName[20];
- } EMP, *LPEMP ;
-
- // Employee table binding
- DAORSETBINDING Bindings[] =
- {
- //Index Type Column Type Offset Size
- {dbBindIndexINT, EMP_ID, dbBindI4, offsetof(EMP,lEmpId), sizeof(LONG)},
- {dbBindIndexINT, EMP_LNAME, dbBindLPSTRING, offsetof(EMP,strLastName), sizeof(TCHAR *)},
- {dbBindIndexINT, EMP_FNAME, dbBindSTRING, offsetof(EMP,strFirstName), sizeof(TCHAR) * 20}
- };
-
- // Perform C++ GetRowsEx against the Employee table
- void CGetRowsDlg::DoGetRowsEx()
- {
- LPEMP pEmpRows = new EMP[MAX_EMP_REC];
- CListBox *pListBox = (CListBox *)GetDlgItem(IDD_GETROWSLISTEX);
- CString strLBRow;
- TCHAR szId[16];
- LONG lNumRecords;
- LONG lCount;
- TCHAR pBuf[MAX_EMP_REC * 15]; // allow average of 15 chars/name
-
- //Perform GetRows on Employee table
- //This GetRows uses a specific C++ structure
- try
- {
- lNumRecords = m_cEmpRecordSet.GetRowsEx(pEmpRows, sizeof(EMP),
- &Bindings[0], sizeof(Bindings) / sizeof(DAORSETBINDING),
- pBuf, sizeof(pBuf),
- MAX_EMP_REC); //arbitrarily get MAX_EMP_REC rows
- }
- catch (CdbException e)
- {
- //Differentiate between GetRowsEx Errors and other CdbExceptions
- // see defines in DAOGETRW.H
- if( e.m_hr == E_ROWTOOSHORT ||
- e.m_hr == E_BADBINDINFO ||
- e.m_hr == E_COLUMNUNAVAILABLE )
- {
- AfxMessageBox(_T("Error in GetRowsEx call."));
- }
- else
- {
- AfxMessageBox(_T("General CdbException"));
- }
- delete [] pEmpRows;
- return;
- }
-
-
- //Step through the returned rows
- for (lCount = 0; lCount < lNumRecords; lCount++)
- {
- strLBRow.Empty();
- wsprintf(szId, _T("%d, "), pEmpRows[lCount].lEmpId);
- strLBRow += szId;
- strLBRow += pEmpRows[lCount].strLastName;
- strLBRow += _T(", ");
- strLBRow += (LPCTSTR) pEmpRows[lCount].strFirstName;
- pListBox->AddString(strLBRow);
- }
-
- delete [] pEmpRows;
- }
-