將字串讀入 OLE DB 提供者內
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;
}
}