次の方法で共有


OLE DB プロバイダへの文字列の読み込み

更新 : 2007 年 11 月

RMyProviderRowset::Execute 関数は、ファイルを開いて文字列を読み込みます。コンシューマは、ICommandText::SetCommandText を呼び出して、プロバイダにファイル名を渡します。プロバイダはファイル名を受け取り、m_szCommandText メンバ変数に格納します。Execute は、m_szCommandText からファイル名を読み取ります。ファイル名が無効な場合またはファイルが使用できない場合、Execute はエラーを返します。それ以外の場合は、ファイルを開いて fgets を呼び出し、文字列を取得します。読み込む文字列の各セットに対して、Execute はユーザー レコードのインスタンス (CAgentMan) を作成し、配列に入れます。

ファイルを開くことができない場合、Execute は DB_E_NOTABLE を返す必要があります。代わりに E_FAIL を返した場合、プロバイダは多くのコンシューマで動作しなくなり、OLE DB 準拠合致テストに合格しません。

説明

編集された Execute 関数は、次のようになります。

コード

/////////////////////////////////////////////////////////////////////////
// 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;
    }
}

参照

参照

単純な読み取り専用プロバイダの実装