void GetRowsX(void) { HRESULT hr = S_OK; // Define string variables. _bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;" "Initial Catalog=pubs;User Id=sa;Password=;"); // Define ADO object pointers. // Initialize pointers on define. // These are in the ADODB:: namespace. _RecordsetPtr pRstEmployees = NULL;
try { // Open recordset with names and hire dates from employee table. TESTHR(pRstEmployees.CreateInstance(__uuidof(Recordset))); pRstEmployees->Open("SELECT fName, lName, hire_date " "FROM Employee ORDER BY lName",strCnn, adOpenStatic, adLockReadOnly,adCmdText); while (true) //continuous loop { int intLines = 0; // Get user input for number of rows. printf("\nEnter number of rows to retrieve (0 to exit): "); int intRows; scanf("%d", &intRows); if (intRows <= 0) break; //Clear the screen for the next display system("cls"); // If GetRowsOK is successful, print the results, // noting if the end of the file was reached. _variant_t avarRecords; if (GetRowsOK(pRstEmployees, intRows, avarRecords)) { long lUbound; HRESULT hr = SafeArrayGetUBound(avarRecords.parray, 2,&lUbound); if (hr == 0) { if (intRows > lUbound + 1) { printf("\n(Not enough records in Recordset to " "retrieve %d rows)\n", intRows); } } printf("%d records found.", lUbound+1); // Print the retrieved data. for (int intRecords = 0;intRecords < lUbound+1; intRecords++) { printf("\n "); long rgIndices[2]; rgIndices[0] = 0; rgIndices[1] = intRecords; _variant_t result; result.vt = VT_BSTR; hr= SafeArrayGetElement(avarRecords.parray, rgIndices, &result); if (hr == 0){printf("%s ",(LPCSTR)(_bstr_t)result);} rgIndices[0] = 1; hr= SafeArrayGetElement(avarRecords.parray, rgIndices, &result); if (hr == 0){printf("%s, ",(LPCSTR)(_bstr_t)result);} rgIndices[0] = 2; hr= SafeArrayGetElement(avarRecords.parray, rgIndices, &result); if (hr == 0){printf("%s",(LPCSTR)(_bstr_t)result);} intLines ++; if (intLines % 10 == 0) { printf("\nPress any key to continue..."); getch(); intLines = 0; //Clear the screen for the next display system("cls"); } } } else { // Assuming the GetRows error was due to data // changes by another user, use Requery to // refresh the Recordset and start over. printf("GetRows failed--retry?\n"); char chKey; do { chKey = getch(); }while(toupper(chKey) != 'Y' && toupper(chKey) != 'N');
if(toupper(chKey) == 'Y') { pRstEmployees->Requery(adOptionUnspecified); } else { printf("GetRows failed!\n"); break; } } // Because using GetRows leaves the current // record pointer at the last record accessed, // move the pointer back to the beginning of the // Recordset before looping back for another search. pRstEmployees->MoveFirst(); } //Clean up objects before exit. pRstEmployees->Close(); } catch(_com_error &e) { // Notify the user of errors if any. // Pass a connection pointer accessed from the Recordset. _variant_t vtConnect = pRstEmployees->GetActiveConnection(); // GetActiveConnection returns connect string if connection // is not open, else returns Connection object. switch(vtConnect.vt) { case VT_BSTR: PrintComError(e); break; case VT_DISPATCH: PrintProviderError(vtConnect); break; default: printf("Errors occured."); break; } } }来源:msdn
{
HRESULT hr = S_OK; // Define string variables.
_bstr_t strCnn("Provider=sqloledb;Data Source=MyServer;"
"Initial Catalog=pubs;User Id=sa;Password=;"); // Define ADO object pointers.
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_RecordsetPtr pRstEmployees = NULL;
try
{
// Open recordset with names and hire dates from employee table.
TESTHR(pRstEmployees.CreateInstance(__uuidof(Recordset))); pRstEmployees->Open("SELECT fName, lName, hire_date "
"FROM Employee ORDER BY lName",strCnn,
adOpenStatic, adLockReadOnly,adCmdText);
while (true) //continuous loop
{
int intLines = 0; // Get user input for number of rows.
printf("\nEnter number of rows to retrieve (0 to exit): ");
int intRows;
scanf("%d", &intRows); if (intRows <= 0)
break; //Clear the screen for the next display
system("cls");
// If GetRowsOK is successful, print the results,
// noting if the end of the file was reached.
_variant_t avarRecords; if (GetRowsOK(pRstEmployees, intRows, avarRecords))
{
long lUbound;
HRESULT hr = SafeArrayGetUBound(avarRecords.parray,
2,&lUbound); if (hr == 0)
{
if (intRows > lUbound + 1)
{
printf("\n(Not enough records in Recordset to "
"retrieve %d rows)\n", intRows);
}
}
printf("%d records found.", lUbound+1); // Print the retrieved data.
for (int intRecords = 0;intRecords < lUbound+1;
intRecords++)
{
printf("\n "); long rgIndices[2];
rgIndices[0] = 0;
rgIndices[1] = intRecords;
_variant_t result;
result.vt = VT_BSTR; hr= SafeArrayGetElement(avarRecords.parray,
rgIndices, &result); if (hr == 0){printf("%s ",(LPCSTR)(_bstr_t)result);} rgIndices[0] = 1; hr= SafeArrayGetElement(avarRecords.parray,
rgIndices, &result); if (hr == 0){printf("%s, ",(LPCSTR)(_bstr_t)result);} rgIndices[0] = 2; hr= SafeArrayGetElement(avarRecords.parray,
rgIndices, &result); if (hr == 0){printf("%s",(LPCSTR)(_bstr_t)result);} intLines ++; if (intLines % 10 == 0)
{
printf("\nPress any key to continue...");
getch(); intLines = 0; //Clear the screen for the next display
system("cls");
}
}
}
else
{
// Assuming the GetRows error was due to data
// changes by another user, use Requery to
// refresh the Recordset and start over.
printf("GetRows failed--retry?\n");
char chKey;
do
{
chKey = getch();
}while(toupper(chKey) != 'Y' && toupper(chKey) != 'N');
if(toupper(chKey) == 'Y')
{
pRstEmployees->Requery(adOptionUnspecified);
}
else
{
printf("GetRows failed!\n");
break;
}
} // Because using GetRows leaves the current
// record pointer at the last record accessed,
// move the pointer back to the beginning of the
// Recordset before looping back for another search.
pRstEmployees->MoveFirst();
}
//Clean up objects before exit.
pRstEmployees->Close();
}
catch(_com_error &e)
{
// Notify the user of errors if any.
// Pass a connection pointer accessed from the Recordset.
_variant_t vtConnect = pRstEmployees->GetActiveConnection(); // GetActiveConnection returns connect string if connection
// is not open, else returns Connection object.
switch(vtConnect.vt)
{
case VT_BSTR:
PrintComError(e);
break;
case VT_DISPATCH:
PrintProviderError(vtConnect);
break;
default:
printf("Errors occured.");
break;
}
}
}来源:msdn