Condividi tramite


Lettura di stringhe in un provider OLE DB

La funzione RMyProviderRowset::Execute apre un file e legge stringhe. Il consumer passa il nome file al provider chiamando ICommandText::SetCommandText. Il provider riceve il nome file e lo archivia nella variabile membro m_szCommandText. Execute legge il nome file da m_szCommandText. Se il nome file non è valido o se il file non è disponibile, Execute restituirà un errore. In caso contrario, aprirà il file e chiamerà fgets per recuperare le stringhe. Per ciascun insieme di stringhe letto, Execute creerà un'istanza del record utente (CAgentMan) e la inserirà in una matrice.

Se il file non può essere aperto, Execute dovrà restituire DB_E_NOTABLE. Se viene restituito E_FAIL, il provider non potrà essere utilizzato con molti consumer e non supererà i test di conformità OLE DB.

Esempio

Descrizione

Nel seguente esempio è riportata la funzione Execute modificata:

Codice

/////////////////////////////////////////////////////////////////////////
// MyProviderRS.h
class RMyProviderRowset : public CRowsetImpl< RMyProviderRowset, CAgentMan, CRMyProviderCommand>
{
public:
    HRESULT Execute(DBPARAMS * pParams, LONG* pcRowsAffected)
    {
        enum {
            sizeOfBuffer = 256,
            sizeOfFile = MAX_PATH
        };
        USES_CONVERSION;
        FILE* pFile = NULL;
        TCHAR szString[sizeOfBuffer];
        TCHAR szFile[sizeOfFile];
        size_t nLength;        errcodeerr;

        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_s(szFile, sizeOfFile, m_szCommandText);
        if (szFile[0] == _T('\0') || 
            ((err = fopen_s(&pFile, &szFile[0], "r")) == 0))
        {
            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, sizeOfBuffer, pFile) != NULL)
        {
            nLength = strnlen(szString, sizeOfBuffer);
            szString[nLength-1] = '\0';   // Strip off trailing CR/LF
            CAgentMan am;
            _tcscpy_s(am.szCommand, am.sizeOfCommand, szString);
            _tcscpy_s(am.szCommand2, am.sizeOfCommand2, szString);

            if (fgets(szString, sizeOfBuffer, pFile) != NULL)
            {
                nLength = strnlen(szString, sizeOfBuffer);
                szString[nLength-1] = '\0'; // Strip off trailing CR/LF
                _tcscpy_s(am.szText, am.sizeOfText, szString);
                _tcscpy_s(am.szText2, am.sizeOfText2, 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;
    }
}

Vedere anche

Riferimenti

Implementazione di un provider semplice in sola lettura