home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / ado / getrows / getrdlg.cpp next >
Encoding:
C/C++ Source or Header  |  1997-11-18  |  8.4 KB  |  338 lines

  1. //--------------------------------------------------------------------
  2. // Microsoft ADO Samples
  3. //
  4. // (c) 1996 Microsoft Corporation.  All Rights Reserved.
  5. //
  6. // @doc GetRows Sample
  7. //
  8. // @module    getrdlg.cpp
  9. //
  10. // @devnote None
  11. //--------------------------------------------------------------------
  12.  
  13. #define INITGUID
  14.  
  15. #include "stdafx.h"
  16. #include "GetRows.h"
  17. #include "GetRDlg.h"
  18.  
  19. const LPCTSTR x_lpcszSource = _T("OLE_DB_NWind_Jet");
  20. const LPCTSTR x_lpcszUser = _T("Admin");                    
  21. const LPCTSTR x_lpcszPassword = _T("");
  22. const LPCTSTR x_lpcszSQL = _T("select EmployeeId, LastName, FirstName from Employees");
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26.  
  27. class CAboutDlg : public CDialog
  28. {
  29. public:
  30.     CAboutDlg();
  31.  
  32. // Dialog Data
  33.     //{{AFX_DATA(CAboutDlg)
  34.     enum { IDD = IDD_ABOUTBOX };
  35.     //}}AFX_DATA
  36.  
  37.     // ClassWizard generated virtual function overrides
  38.     //{{AFX_VIRTUAL(CAboutDlg)
  39.     protected:
  40.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  41.     //}}AFX_VIRTUAL
  42.  
  43. // Implementation
  44. protected:
  45.     //{{AFX_MSG(CAboutDlg)
  46.     //}}AFX_MSG
  47.     DECLARE_MESSAGE_MAP()
  48. };
  49.  
  50. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  51. {
  52.     //{{AFX_DATA_INIT(CAboutDlg)
  53.     //}}AFX_DATA_INIT
  54. }
  55.  
  56. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CDialog::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CAboutDlg)
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  64.      //{{AFX_MSG_MAP(CAboutDlg)
  65.     //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CGetRowsDlg dialog
  70.  
  71. CGetRowsDlg::CGetRowsDlg(CWnd* pParent /*=NULL*/)
  72.     : CDialog(CGetRowsDlg::IDD, pParent)
  73. {
  74.     //{{AFX_DATA_INIT(CGetRowsDlg)
  75.         // NOTE: the ClassWizard will add member initialization here
  76.     //}}AFX_DATA_INIT
  77.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  78.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);        
  79.     m_piConnection = NULL;
  80.     m_piEmpRecordSet = NULL;
  81.     m_strSource = x_lpcszSource;
  82.     m_strUser = x_lpcszUser;
  83.     m_strPassword = x_lpcszPassword;
  84.     m_strSQL = x_lpcszSQL;
  85. }
  86.  
  87. CGetRowsDlg::~CGetRowsDlg()
  88. {
  89.     if ( m_piConnection != NULL )
  90.         m_piConnection->Release();                     
  91.     if ( m_piEmpRecordSet != NULL )
  92.         m_piEmpRecordSet->Release();            
  93.     m_piConnection = NULL;
  94.     m_piEmpRecordSet = NULL;
  95. }
  96.  
  97.  
  98. void CGetRowsDlg::DoDataExchange(CDataExchange* pDX)
  99. {
  100.     CDialog::DoDataExchange(pDX);
  101.     //{{AFX_DATA_MAP(CGetRowsDlg)
  102.         // NOTE: the ClassWizard will add DDX and DDV calls here
  103.     //}}AFX_DATA_MAP
  104. }
  105.  
  106. BEGIN_MESSAGE_MAP(CGetRowsDlg, CDialog)
  107.     //{{AFX_MSG_MAP(CGetRowsDlg)
  108.     ON_WM_SYSCOMMAND()
  109.     ON_WM_PAINT()
  110.     ON_WM_QUERYDRAGICON()
  111.     ON_BN_CLICKED(IDD_EXECUTE, OnExecute)
  112.     //}}AFX_MSG_MAP
  113. END_MESSAGE_MAP()
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CGetRowsDlg message handlers
  117.  
  118. BOOL CGetRowsDlg::OnInitDialog()
  119. {
  120.     CDialog::OnInitDialog();
  121.  
  122.     // Add "About..." menu item to system menu.
  123.  
  124.     // IDM_ABOUTBOX must be in the system command range.
  125.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  126.     ASSERT(IDM_ABOUTBOX < 0xF000);
  127.  
  128.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  129.     CString strAboutMenu;
  130.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  131.     if (!strAboutMenu.IsEmpty())
  132.     {
  133.         pSysMenu->AppendMenu(MF_SEPARATOR);
  134.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  135.     }
  136.  
  137.     // Set the icon for this dialog.  The framework does this automatically
  138.     //  when the application's main window is not a dialog
  139.     SetIcon(m_hIcon, TRUE);            // Set big icon
  140.     SetIcon(m_hIcon, FALSE);        // Set small icon
  141.     
  142.     return TRUE;  // return TRUE  unless you set the focus to a control
  143. }
  144.  
  145. void CGetRowsDlg::OnSysCommand(UINT nID, LPARAM lParam)
  146. {
  147.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  148.     {
  149.         CAboutDlg dlgAbout;
  150.         dlgAbout.DoModal();
  151.     }
  152.     else
  153.     {
  154.         CDialog::OnSysCommand(nID, lParam);
  155.     }
  156. }
  157.  
  158. // If you add a minimize button to your dialog, you will need the code below
  159. //  to draw the icon.  For MFC applications using the document/view model,
  160. //  this is automatically done for you by the framework.
  161.  
  162. void CGetRowsDlg::OnPaint() 
  163. {
  164.     if (IsIconic())
  165.     {
  166.         CPaintDC dc(this); // device context for painting
  167.  
  168.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  169.  
  170.         // Center icon in client rectangle
  171.         int cxIcon = GetSystemMetrics(SM_CXICON);
  172.         int cyIcon = GetSystemMetrics(SM_CYICON);
  173.         CRect rect;
  174.         GetClientRect(&rect);
  175.         int x = (rect.Width() - cxIcon + 1) / 2;
  176.         int y = (rect.Height() - cyIcon + 1) / 2;
  177.  
  178.         // Draw the icon
  179.         dc.DrawIcon(x, y, m_hIcon);
  180.     }
  181.     else
  182.     {
  183.         CDialog::OnPaint();
  184.     }
  185. }
  186.  
  187. // The system calls this to obtain the cursor to display while the user drags
  188. //  the minimized window.
  189. HCURSOR CGetRowsDlg::OnQueryDragIcon()
  190. {
  191.     return (HCURSOR) m_hIcon;
  192. }
  193.  
  194.  
  195. void CGetRowsDlg::OnExecute() 
  196. {
  197.     BSTR        bstrSource = NULL;
  198.     BSTR        bstrUser = NULL;
  199.     BSTR        bstrPassword = NULL;
  200.     BSTR        bstrSQL = NULL;
  201.     HRESULT        hr;
  202.     COleVariant    vNull;
  203.                         
  204.     //Open the database and the recordset    
  205.     if ( m_piConnection == NULL || m_piEmpRecordSet == NULL)
  206.     {
  207.         hr = CoInitialize(NULL);
  208.         if (FAILED(hr))        goto ErrorExit;
  209.  
  210.         hr = CoCreateInstance(CLSID_CADOConnection, NULL, CLSCTX_INPROC_SERVER, IID_IADOConnection, (LPVOID *)&m_piConnection);
  211.         if (FAILED(hr))        goto ErrorExit;
  212.  
  213.         bstrSource = m_strSource.AllocSysString();
  214.         bstrUser = m_strUser.AllocSysString();
  215.         bstrPassword = m_strPassword.AllocSysString();
  216.         bstrSQL = m_strSQL.AllocSysString();
  217.         if ( bstrSource == NULL || bstrUser == NULL || bstrSQL == NULL )    
  218.             goto ErrorExit;
  219.  
  220.         hr = m_piConnection->Open( bstrSource, bstrUser, bstrPassword, adOpenUnspecified /* -1 */ ); 
  221.          if (FAILED(hr))        goto ErrorExit;
  222.  
  223.  
  224.           hr = CoCreateInstance(CLSID_CADORecordset, NULL, CLSCTX_INPROC_SERVER, IID_IADORecordset, (LPVOID *)&m_piEmpRecordSet);
  225.         if (FAILED(hr))        goto ErrorExit;
  226.  
  227.         hr = m_piEmpRecordSet->put_Source(bstrSQL);    
  228.         if (FAILED(hr))        goto ErrorExit;
  229.  
  230.         hr = m_piEmpRecordSet->putref_ActiveConnection(m_piConnection);        
  231.         if (FAILED(hr))        goto ErrorExit;    
  232.  
  233.         vNull.vt = VT_ERROR;
  234.         vNull.scode = DISP_E_PARAMNOTFOUND;
  235.         hr = m_piEmpRecordSet->Open(vNull, vNull, adOpenKeyset, adLockOptimistic, adCmdUnknown);        
  236.         if (FAILED(hr))        goto ErrorExit;
  237.     }
  238.                 
  239.     // Perform the two GetRows functions
  240.     hr = DoGetRows();
  241.      if (FAILED(hr))        goto ErrorExit;
  242.  
  243.     // Reset the recordset
  244.     m_piEmpRecordSet->MoveFirst();
  245.  
  246.     SysFreeString(bstrSource);
  247.     SysFreeString(bstrUser);     
  248.     SysFreeString(bstrPassword);
  249.     SysFreeString(bstrSQL);
  250.     
  251.     return ;
  252.  
  253. ErrorExit:
  254.     TCHAR szBuf[256];
  255.     wsprintf(szBuf, _T("Error: %d \n"), hr);
  256.     AfxMessageBox(szBuf);
  257.  
  258.     SysFreeString(bstrSource);
  259.     SysFreeString(bstrUser);
  260.     SysFreeString(bstrPassword);
  261.     SysFreeString(bstrSQL);
  262.     return ;
  263. }
  264.  
  265.  
  266. // Perform standard GetRows against the Employee table
  267. HRESULT CGetRowsDlg::DoGetRows() 
  268. {
  269.     HRESULT            hr;
  270.     COleVariant        vBookmark, rgvFields;
  271.     COleVariant        cRows;
  272.     COleVariant        varField, varNewField;
  273.     CString            strLBRow;
  274.     LONG            lNumOfCol, lNumRecords;
  275.     LONG            lIndex[2];
  276.     CListBox        *pListBox = (CListBox *)GetDlgItem(IDD_GETROWSLIST);
  277.  
  278.     //Perform GetRows on Employee table
  279.  
  280.     //Start from the current place             
  281.     vBookmark.vt = VT_ERROR;
  282.     vBookmark.scode = DISP_E_PARAMNOTFOUND;    
  283.     
  284.     //Get all columns.            
  285.     rgvFields.vt = VT_ERROR;
  286.     rgvFields.scode = DISP_E_PARAMNOTFOUND;    
  287.  
  288.     if (!m_piEmpRecordSet)
  289.         return E_NOINTERFACE;
  290.  
  291.     hr = m_piEmpRecordSet->GetRows(    adGetRowsRest, //MAX_EMP_REC,// non-optional, 
  292.                                     vBookmark,
  293.                                     rgvFields,
  294.                                     &cRows         
  295.                                     );
  296.     if (FAILED(hr)) goto ErrorExit;
  297.  
  298.  
  299.     //Find out how many records were actually retrieved
  300.     //(SafeArrays are 1-based)
  301.     lNumOfCol = 2;
  302.     SafeArrayGetUBound(cRows.parray, 2, &lNumRecords);
  303.  
  304.     //Clear the listbox
  305.     pListBox->ResetContent();
  306.     
  307.     for (lIndex[1] = 0; lIndex[1] <= lNumRecords; lIndex[1]++)
  308.     {
  309.         strLBRow.Empty();//Clear the string
  310.  
  311.         for (lIndex[0] = 0; lIndex[0] <= lNumOfCol; lIndex[0]++)   // get 3 columns
  312.         {
  313.             SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);
  314.             
  315.             hr = VariantChangeType(&varNewField, &varField, 0, VT_BSTR);
  316.  
  317.             if(hr == S_OK)
  318.             {
  319.                 strLBRow += (LPCWSTR)varNewField.bstrVal;
  320.                 strLBRow += _T(", ");
  321.             }
  322.             varField.Clear();
  323.             varNewField.Clear();
  324.         }
  325.         pListBox->AddString(strLBRow);
  326.     }
  327.  
  328.     return hr;
  329.  
  330. ErrorExit:
  331.     TCHAR szBuf[256];
  332.     wsprintf(szBuf, _T("Error: %d \n"), hr);
  333.     AfxMessageBox(szBuf);
  334.  
  335.     return hr;
  336. }
  337.  
  338.