Microsoft KB Archive/172391

-

{| The information in this article applies to: ActiveX Data Objects (ADO) included with: - Microsoft Visual C++, 32-bit Editions, version 5.0
 * width="100%"|

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
 * }

-

Last reviewed: July 13, 1998

© 1998 Microsoft Corporation. All rights reserved. Terms of Use.