Adding the Run Button Handler

You must add a handler function for the Run button you created in the dialog resource file.

To create a Run button handler

To create the handler, you must use the following OLE DB Consumer Template classes:

Each of these classes corresponds to a COM object in the provider.

Add the code shown below. In this handler code, the CCommand object takes a CProvider class as a template parameter. This parameter represents the bindings used by the provider to access the data.

///////////////////////////////////////////////////////////////////////
// TestProvDlg.cpp

void CTestProvDlg::OnRun()
{
   CCommand<CAccessor<CProvider>> table;
   CDataSource source;
   CSession   session;

   if (source.Open(“MyProvider.MyProvider.1”, NULL) != S_OK)
      return;

   if (session.Open(source) != S_OK)
      return;

   if (table.Open(session, _T("c:\\public\\testprf2\\gloo.txt")) != S_OK)
      return;

   while (table.MoveNext() == S_OK)
   {
      m_ctlString1.AddString(table.szCommand);
      m_ctlString2.AddString(table.szText);
   }
}

The handler uses the three Open calls, shown below, which correspond to three COM objects in the provider. It locates the provider by using its ProgID. You can obtain the ProgID from the system registry or by looking in the provider rgs file (MyProvider.rgs for the simple provider shown in Implementing a Simple Read-Only Provider). The provider opens a text file named gloo.txt, which contains an even number of strings separated by carriage returns. Open passes the file pathname to the provider as a string to the provider.

   if (source.Open(“MyProvider.MyProvider.1”, NULL) != S_OK)
      return;

   if (session.Open(source) != S_OK)
      return;

   if (table.Open(session, _T("c:\\public\\testprf2\\gloo.txt")) != S_OK)
      return;

To fetch the data, the handler calls table.MoveNext, as shown below. When there are no more rows, the provider returns DB_S_ENDOFROWSET and the loop terminates. The loop control statement compares the return value to S_OK. If the code used the SUCCEEDED macro, the loop would not terminate because DB_S_ENDOFROWSET is considered a successful return code.

   while (table.MoveNext() == S_OK)
   {
      m_ctlString1.AddString(table.szCommand);
      m_ctlString2.AddString(table.szText);
   }

The consumer is now ready to test with the simple provider (constructed in Implementing a Simple Read-Only Provider).

The next topic shows how to modify the consumer for use with an enhanced provider that implements the IRowsetLocate interface.

Back to Implementing an OLE DB Template Consumer