Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Refresh Method Example (VC++)

This example demonstrates using the Refresh method to refresh the Parameters collection for a stored procedure Command object.

// BeginRefreshCpp.cpp
// compile with: /EHsc /c
#import "msado15.dll" no_namespace rename("EOF", "EndOfFile")

#include <ole2.h>
#include <stdio.h>
#include <conio.h>
#include "icrsint.h"

// Class extracts lname,fname from authors table. 
class CAuthorsRs : public CADORecordBinding {

   // Column lname is the 2nd field in the recordset
   ADO_VARIABLE_LENGTH_ENTRY2(2, adVarChar, m_szau_lname, 
                              sizeof(m_szau_lname), lau_lnameStatus, TRUE)

   // Column fname is the 3rd field in the recordset.
   ADO_VARIABLE_LENGTH_ENTRY2(3, adVarChar, m_szau_fname, 
                              sizeof(m_szau_fname), lau_fnameStatus, TRUE)

   CHAR m_szau_fname[21];
   ULONG lau_fnameStatus;    
   CHAR m_szau_lname[41];
   ULONG lau_lnameStatus;

// Function declarations
inline void TESTHR(HRESULT x) { if FAILED(x) _com_issue_error(x); };
void RefreshX();
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

inline char* mygets(char* strDest, int n) {    
   char strExBuff[10];
   char* pstrRet = fgets(strDest, n, stdin);

   if (pstrRet == NULL)
      return NULL;

   if (!strrchr(strDest, '\n'))
      // Exhaust the input buffer.
      do {
         fgets(strExBuff, sizeof(strExBuff), stdin);
      } while (!strrchr(strExBuff, '\n'));
      // Replace '\n' with '\0'
      strDest[strrchr(strDest, '\n') - strDest] = '\0';

   return pstrRet;

int main() {
   if (FAILED(::CoInitialize(NULL)))
      return -1;


void RefreshX() {
   HRESULT  hr = S_OK;

   // Define string variables.
   _bstr_t strCnn("Provider='sqloledb'; Data Source='My_Data_Source'; Initial Catalog='pubs'; Integrated Security='SSPI';");

   // Define ADO object pointers.  Initialize pointers on define.  These are in the ADODB::  namespace.
   _ConnectionPtr pConnection = NULL;
   _CommandPtr pCmdByRoyalty = NULL;
   _RecordsetPtr pRstByRoyalty = NULL;
   _RecordsetPtr pRstAuthors = NULL;
   IADORecordBinding *picRs = NULL;   // Interface Pointer declared. 
   CAuthorsRs authorsrs;   // C++ class object
   char * token1;

   try {
      // Open connection.
      pConnection->Open (strCnn, "", "", adConnectUnspecified);

      // Open a command object for a stored procedure, with one parameter.
      pCmdByRoyalty->ActiveConnection = pConnection;
      pCmdByRoyalty->CommandText = "byroyalty";
      pCmdByRoyalty->CommandType = adCmdStoredProc;

      // Get parameter value and execute the command, storing the results in a recordset.
      char *strRoyalty;
      char strTemp[5];
      do {
         printf("\n\nEnter royalty : ");
         mygets(strTemp, 5);

         strRoyalty = strtok_s(strTemp," \t", &token1);
         if (strRoyalty == NULL) {

         // if the input is not numeric then notify the user.
         if (!atoi(strRoyalty)) {
            printf("\nExpecting numeric value...");
      } while (!atoi(strRoyalty));

      _variant_t vtroyal;
      vtroyal.vt = VT_I2;
      vtroyal.iVal = atoi(strRoyalty);
      _variant_t Index;
      Index.vt = VT_I2;
      Index.iVal = 1;
      pRstByRoyalty = pCmdByRoyalty->Execute(NULL,NULL,adCmdStoredProc);

      // Open the Authors table to get author names for display.
      pRstAuthors->Open ("authors", _variant_t((IDispatch *) pConnection, true),
         adOpenForwardOnly, adLockReadOnly, adCmdTable);

      // Open an IADORecordBinding interface pointer which we'll use for 
      // Binding Recordset to a class.

      // Bind the Recordset to a C++ Class here.

      // Print current data in the recordset,adding 
      // author names from Authors table.
      printf("Authors with %s percent royalty\n\n", strRoyalty);
      while (!(pRstByRoyalty->EndOfFile)) {
         _bstr_t strAuthorID = pRstByRoyalty->Fields->GetItem("au_id")->Value;

         printf("  %s",(LPCSTR) (_bstr_t) pRstByRoyalty->Fields->GetItem("au_id")->Value);

         pRstAuthors->Filter = "au_id ='"+strAuthorID+"'";
         printf(", %s %s\n",authorsrs.lau_fnameStatus == adFldOK ? 
            authorsrs.m_szau_fname : "<NULL>",
            authorsrs.lau_lnameStatus == adFldOK ? 
            authorsrs.m_szau_lname : "<NULL>");
   catch (_com_error &e) {

   if (pRstByRoyalty)
      if (pRstByRoyalty->State == adStateOpen)
   if (pRstAuthors)
      if (pRstAuthors->State == adStateOpen)
   if (pConnection)
      if (pConnection->State == adStateOpen)

void PrintProviderError(_ConnectionPtr pConnection) {
   // Print Provider Errors from Connection object.
   // pErr is a record object in the Connection's Error collection.
   ErrorPtr pErr = NULL;

   if ( (pConnection->Errors->Count) > 0) {
      long nCount = pConnection->Errors->Count;

      // Collection ranges from 0 to nCount -1.
      for (long i = 0 ; i < nCount ; i++) {
         pErr = pConnection->Errors->GetItem(i);
         printf("\t Error number: %x\t%s", pErr->Number, pErr->Description);

void PrintComError(_com_error &e) {
   _bstr_t bstrSource(e.Source());
   _bstr_t bstrDescription(e.Description());

   // Print COM errors. 
   printf("\tCode = %08lx\n", e.Error());
   printf("\tCode meaning = %s\n", e.ErrorMessage());
   printf("\tSource = %s\n", (LPCSTR) bstrSource);
   printf("\tDescription = %s\n", (LPCSTR) bstrDescription);

Authors with 25 percent royalty

  724-80-9391, Stearns MacFeathe
  899-46-2035, Anne Ringer
© 2015 Microsoft