Microsoft KB Archive/172391: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - "&" to "&")
 
(2 intermediate revisions by the same user not shown)
Line 33: Line 33:
</pre>
</pre>
The demonstration code resides in two primary functions; OnExecute and DoGetRows(). Here is the code:
The demonstration code resides in two primary functions; OnExecute and DoGetRows(). Here is the code:
<pre>  #import &quot;c:\program files\Common Files\System\ADO\MSADO10.DLL&quot; \
<pre>  #import "c:\program files\Common Files\System\ADO\MSADO10.DLL" \
       implementation_only
       implementation_only


   const _bstr_t bstrSource(_T(&quot;DRIVER=Microsoft Access Driver&quot;
   const _bstr_t bstrSource(_T("DRIVER=Microsoft Access Driver"
       &quot;(*.mdb);DBQ=nwind.mdb&quot;));
       "(*.mdb);DBQ=nwind.mdb"));
   const _bstr_t bstrUser(_T(&quot;Admin&quot;));
   const _bstr_t bstrUser(_T("Admin"));
   const _bstr_t bstrPassword(_T(&quot;&quot;));
   const _bstr_t bstrPassword(_T(""));
   const _bstr_t bstrSQL(_T(&quot;select EmployeeId, LastName, FirstName from&quot;
   const _bstr_t bstrSQL(_T("select EmployeeId, LastName, FirstName from"
       &quot;Employees&quot;));
       "Employees"));


   void CVC5GetRowsDlg::OnExecute()
   void CVC5GetRowsDlg::OnExecute()
Line 54: Line 54:
             if (FAILED(hr))
             if (FAILED(hr))
             {
             {
               AfxMessageBox(_T(&quot;Failure to Initialize OLE&quot;));
               AfxMessageBox(_T("Failure to Initialize OLE"));
               return;
               return;
             }
             }
Line 60: Line 60:
             m_pConn.CreateInstance(_uuidof(ADODB::Connection));
             m_pConn.CreateInstance(_uuidof(ADODB::Connection));


             m_pConn-&gt;Open( bstrSource, bstrUser, bstrPassword );
             m_pConn->Open( bstrSource, bstrUser, bstrPassword );


             m_pRS.CreateInstance(_uuidof(ADODB::Recordset));
             m_pRS.CreateInstance(_uuidof(ADODB::Recordset));


             m_pRS-&gt;PutSource(bstrSQL);
             m_pRS->PutSource(bstrSQL);


             m_pRS-&gt;ActiveConnection = m_pConn;
             m_pRS->ActiveConnection = m_pConn;


             _variant_t vNull;
             _variant_t vNull;
Line 72: Line 72:
             vNull.scode = DISP_E_PARAMNOTFOUND;
             vNull.scode = DISP_E_PARAMNOTFOUND;


             m_pRS-&gt;Open(vNull, vNull, ADODB::adOpenKeyset,
             m_pRS->Open(vNull, vNull, ADODB::adOpenKeyset,
                         ADODB::adLockOptimistic,  ADODB::adCmdUnknown);
                         ADODB::adLockOptimistic,  ADODB::adCmdUnknown);
         }
         }
Line 80: Line 80:


         // Reset the recordset
         // Reset the recordset
         m_pRS-&gt;MoveFirst();
         m_pRS->MoveFirst();


       }
       }
       catch(_com_error &amp; err)
       catch(_com_error & err)
       {
       {
     ::MessageBox(NULL, (LPCSTR)err.Description(), _T(&quot;ADO Error&quot;), MB_OK);
     ::MessageBox(NULL, (LPCSTR)err.Description(), _T("ADO Error"), MB_OK);
       }
       }
   }
   }
Line 109: Line 109:
       //      arguments in the GetRows call.  If vtMissing is used,
       //      arguments in the GetRows call.  If vtMissing is used,
       //      GetRows starts from the current record and gets all columns
       //      GetRows starts from the current record and gets all columns
       cRows = m_pRS-&gt;GetRows(ADODB::adGetRowsRest);
       cRows = m_pRS->GetRows(ADODB::adGetRowsRest);


       //Find out how many records were actually retrieved
       //Find out how many records were actually retrieved
       //(SafeArrays are 1-based)
       //(SafeArrays are 1-based)
       lNumOfCol = 2;
       lNumOfCol = 2;
       SafeArrayGetUBound(cRows.parray, 2, &amp;lNumRecords);
       SafeArrayGetUBound(cRows.parray, 2, &lNumRecords);


       //Clear the listbox
       //Clear the listbox
       pListBox-&gt;ResetContent();
       pListBox->ResetContent();


       for (lIndex[1] = 0; lIndex[1] <= lNumRecords; lIndex[1]++)
       for (lIndex[1] = 0; lIndex[1] <= lNumRecords; lIndex[1]++)
Line 126: Line 126:
             // get 3 columns
             // get 3 columns
         {
         {
             SafeArrayGetElement(cRows.parray, &amp;lIndex[0], &amp;varField);
             SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);


             hr = VariantChangeType(&amp;varNewField, &amp;varField, 0, VT_BSTR);
             hr = VariantChangeType(&varNewField, &varField, 0, VT_BSTR);


             if(hr == S_OK)
             if(hr == S_OK)
Line 134: Line 134:
               strLBRow += (LPCWSTR)varNewField.bstrVal;
               strLBRow += (LPCWSTR)varNewField.bstrVal;
               if (lIndex[0] != lNumOfCol)
               if (lIndex[0] != lNumOfCol)
               strLBRow += _T(&quot;, &quot;);
               strLBRow += _T(", ");
             }
             }
             varField.Clear();
             varField.Clear();
             varNewField.Clear();
             varNewField.Clear();
         }
         }
       pListBox-&gt;AddString(strLBRow);
       pListBox->AddString(strLBRow);
       }
       }
   }
   }
Line 171: Line 171:
</span><br />
</span><br />
<br />
<br />
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED &quot;AS IS&quot; WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.</blockquote>
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.</blockquote>
|}
|}


Last reviewed: July 13, 1998<br />
Last reviewed: July 13, 1998<br />
© 1998 Microsoft Corporation. All rights reserved. [[../cpyright|Terms of Use.]]<br />
© 1998 Microsoft Corporation. All rights reserved. [[../cpyright|Terms of Use.]]<br />

Latest revision as of 11:29, 21 July 2020



SAMPLE: GetRows.exe Demonstrates #import with ADO and GetRows()

Last reviewed: July 13, 1998
Article ID: Q172391



The information in this article applies to: ActiveX Data Objects (ADO) included with:

    - Microsoft Visual C++, 32-bit Editions, version 5.0

SUMMARY

GetRows.exe is a sample that is identical to the ADO GetRows sample that comes with the OLEDBSDK, but GetRows.exe is re-written to use the #import compiler directive, which was introduced in Visual C++ version 5.0. The code is much cleaner using #import and is less error-prone.

MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

 ~ GetRows.exe (size: 599958 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

The demonstration code resides in two primary functions; OnExecute and DoGetRows(). Here is the code:

   #import "c:\program files\Common Files\System\ADO\MSADO10.DLL" \
      implementation_only

   const _bstr_t bstrSource(_T("DRIVER=Microsoft Access Driver"
      "(*.mdb);DBQ=nwind.mdb"));
   const _bstr_t bstrUser(_T("Admin"));
   const _bstr_t bstrPassword(_T(""));
   const _bstr_t bstrSQL(_T("select EmployeeId, LastName, FirstName from"
      "Employees"));

   void CVC5GetRowsDlg::OnExecute()
   {
      HRESULT hr;
      try
      {

         if ( m_pConn == NULL || m_pRS == NULL)
         {
            hr = CoInitialize(NULL);
            if (FAILED(hr))
            {
               AfxMessageBox(_T("Failure to Initialize OLE"));
               return;
            }

            m_pConn.CreateInstance(_uuidof(ADODB::Connection));

            m_pConn->Open( bstrSource, bstrUser, bstrPassword );

            m_pRS.CreateInstance(_uuidof(ADODB::Recordset));

            m_pRS->PutSource(bstrSQL);

            m_pRS->ActiveConnection = m_pConn;

            _variant_t vNull;
            vNull.vt = VT_ERROR;
            vNull.scode = DISP_E_PARAMNOTFOUND;

            m_pRS->Open(vNull, vNull, ADODB::adOpenKeyset,
                        ADODB::adLockOptimistic,  ADODB::adCmdUnknown);
        }

        // Perform the two GetRows functions
        DoGetRows();

        // Reset the recordset
        m_pRS->MoveFirst();

      }
      catch(_com_error & err)
      {
     ::MessageBox(NULL, (LPCSTR)err.Description(), _T("ADO Error"), MB_OK);
      }
   }

   void CVC5GetRowsDlg::DoGetRows()
   {
      ASSERT(m_pRS!=NULL);

      HRESULT hr;
      _variant_t cRows;
      _variant_t varField, varNewField;
      CString strLBRow;
      LONG lNumOfCol, lNumRecords;
      LONG   lIndex[2];
      CListBox *pListBox = (CListBox *)GetDlgItem(IDD_GETROWSLIST);

      //Perform GetRows on Employee table

      // NOTE: #import generates vtMissing for default arguments which
      //       evaluates to a variant with vt=VT_ERROR and
      //       scode = DISP_E_PARAMNOTFOUND.
      //       vtMissing is used for the optional second and third
      //       arguments in the GetRows call.  If vtMissing is used,
      //       GetRows starts from the current record and gets all columns
      cRows = m_pRS->GetRows(ADODB::adGetRowsRest);

      //Find out how many records were actually retrieved
      //(SafeArrays are 1-based)
      lNumOfCol = 2;
      SafeArrayGetUBound(cRows.parray, 2, &lNumRecords);

      //Clear the listbox
      pListBox->ResetContent();

      for (lIndex[1] = 0; lIndex[1] <= lNumRecords; lIndex[1]++)
      {
         strLBRow.Empty();//Clear the string

         for (lIndex[0] = 0; lIndex[0] <= lNumOfCol; lIndex[0]++)
             // get 3 columns
         {
            SafeArrayGetElement(cRows.parray, &lIndex[0], &varField);

            hr = VariantChangeType(&varNewField, &varField, 0, VT_BSTR);

            if(hr == S_OK)
            {
               strLBRow += (LPCWSTR)varNewField.bstrVal;
               if (lIndex[0] != lNumOfCol)
               strLBRow += _T(", ");
            }
            varField.Clear();
            varNewField.Clear();
         }
      pListBox->AddString(strLBRow);
      }
   }

For additional information, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q169496
   TITLE     : INFO: Using ActiveX Data Objects (ADO) via #import in VC++

   ARTICLE-ID: Q166112
   TITLE     : PRB: Conflict with EOF When Using #import with ADO

   ARTICLE-ID: Q169498
   TITLE     : INFO: Extracting Error Information from ADO in VC++ with
               #import

   ARTICLE-ID: Q172394
   TITLE     : FILE: ADOReg.exe License Key Registration DLL and ADO Use
               Tips



Additional query words:

Keywords : kbcode kbfile kbsample adovc
Technology : odbc
Version : WINDOWS:
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.