Reading Strings into the OLE DB Provider

The RMyProviderRowset::Execute function opens a file and reads strings. The consumer passes the filename to the provider by calling ICommandText::SetCommandText. The provider receives the filename and stores it in the member variable m_szCommandText. Execute reads the filename from m_szCommandText. If the filename is invalid or the file is unavailable, Execute returns an error. Otherwise, it opens the file and calls fgets to retrieve the strings. For each set of strings it reads, Execute creates an instance of the User Record (CAgentMan) and places it into an array.

If the file can’t be opened, Execute must return DB_E_NOTABLE. If it returns E_FAIL instead, the provider will not work with many consumers and will not pass the OLE DB conformance tests.

The edited Execute function looks like this:

/////////////////////////////////////////////////////////////////////////
// MyProviderRS.h
class RMyProviderRowset : public CRowsetImpl< RMyProviderRowset, CAgentMan, CRMyProviderCommand>
{
public:
   
HRESULT Execute(DBPARAMS * pParams, LONG* pcRowsAffected)
{
 USES_CONVERSION;
 FILE* pFile;
 TCHAR szString[256];
 TCHAR szFile[MAX_PATH];
 size_t nLength;
 ObjectLock lock(this);

 // From a filename, passed in as a command text, scan the file
 // placing data in the data array.
 if (!m_szCommandText)
 {
    ATLTRACE("No filename specified");
    return E_FAIL;
 }

 // Open the file
 _tcscpy(szFile, m_szCommandText);
 if (szFile[0] == _T('\0') || ((pFile = fopen(&szFile[0], "r")) == NULL))
 {
    ATLTRACE("Could not open file");
    return DB_E_NOTABLE;
 }

 // scan and parse the file.  The file should contain two strings per record
 LONG cFiles = 0;
 while (fgets(szString, 256, pFile) != NULL)
 {
    nLength = strlen(szString);
    szString[nLength-1] = '\0';   // Strip off trailing CR/LF
    CAgentMan am;
    _tcscpy(am.szCommand, szString);
    _tcscpy(am.szCommand2, szString);

    if (fgets(szString, 256, pFile) != NULL)
    {
       nLength = strlen(szString);
       szString[nLength-1] = '\0';   // Strip off trailing CR/LF
       _tcscpy(am.szText, szString);
       _tcscpy(am.szText2, szString);
    }

    am.dwBookmark = ++cFiles;
    if (!m_rgRowData.Add(am))
    {
       ATLTRACE("Couldn't add data to array");
       fclose(pFile);
       return E_FAIL;
    }
 }

 if (pcRowsAffected != NULL)
    *pcRowsAffected = cFiles;
 return S_OK;
}