Ani*_*amy 10 c++ database mfc bulk fetch
在程序开始时,我需要将MS Access数据库(.mdb)中的数据读入下拉控件.这样做是为了无论何时用户键入该控件,应用程序都可以自动完成.
无论如何,从数据库读取永远是如此,所以我认为我将实现批量行提取.
这是我的代码:
CString sDsn;
CString sField;
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database
database.Open(NULL,false,false,sDsn);
// Allocate the rowset
CMultiRowset recset( &database );
// Build the SQL statement
SqlString = "SELECT NAME "
"FROM INFOTABLE";
// Set the rowset size. These many rows will be fetched in one bulk operation
recset.SetRowsetSize(25);
// Open the rowset
recset.Open(CRecordset::forwardOnly, SqlString, CRecordset::readOnly | CRecordset::useMultiRowFetch);
// Loop through each rowset
while( !recset.IsEOF() )
{
int rowsFetched = (int)recset.GetRowsFetched(); // This value is always 1 somehow
for( int rowCount = 1; rowCount <= rowsFetched; rowCount++ )
{
recset.SetRowsetCursorPosition(rowCount);
recset.GetFieldValue("NAME",sField);
m_nameDropDown.AddString(sField);
}
// Go to next rowset
recset.MoveNext();
}
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// If a database exception occured, show error msg
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;
Run Code Online (Sandbox Code Playgroud)
MultiRowset.cpp 好像:
#include "stdafx.h"
#include "afxdb.h"
#include "MultiRowset.h"
// Constructor
CMultiRowset::CMultiRowset(CDatabase *pDB)
: CRecordset(pDB)
{
m_NameData = NULL;
m_NameDataLengths = NULL;
m_nFields = 1;
CRecordset::CRecordset(pDB);
}
void CMultiRowset::DoBulkFieldExchange(CFieldExchange *pFX)
{
pFX->SetFieldType(CFieldExchange::outputColumn);
RFX_Text_Bulk(pFX, _T("[NAME]"), &m_NameData, &m_NameDataLengths, 30);
}
Run Code Online (Sandbox Code Playgroud)
MultiRowset.h 好像:
#if !defined(__MULTIROWSET_H_AD12FD1F_0566_4cb2_AE11_057227A594B8__)
#define __MULTIROWSET_H_AD12FD1F_0566_4cb2_AE11_057227A594B8__
class CMultiRowset : public CRecordset
{
public:
// Field data members
LPSTR m_NameData;
// Pointers for the lengths of the field data
long* m_NameDataLengths;
// Constructor
CMultiRowset(CDatabase *);
// Methods
void DoBulkFieldExchange(CFieldExchange *);
};
#endif
Run Code Online (Sandbox Code Playgroud)
在我的数据库中,INFOTABLE看起来像:
NAME AGE
---- ---
Name1 Age1
Name2 Age2
.
.
.
.
Run Code Online (Sandbox Code Playgroud)
我需要做的只是从数据库中读取数据.有人可以告诉我我做错了什么吗?我的代码现在的行为与普通的提取完全相同.没有发生批量抓取.
编辑:
我只是戳在周围DBRFX.cpp,结果发现,RFX_Text_Bulk()将初始化我传递m_NameData的new char[nRowsetSize * nMaxLength]!
这意味着m_NameData只是一个字符数组!我需要获取多个名称,所以我不需要2D字符数组吗?最奇怪的是,同样RFX_Text_Bulk()将初始化我传递m_NDCDataLengths的new long[nRowsetSize].为什么世界上的字符数组需要一个长度数组?!
你几乎做对了。为了获取值,我会改变你的
for( int rowCount = 1; rowCount <= rowsFetched; rowCount++ )
{
recset.SetRowsetCursorPosition(rowCount);
recset.GetFieldValue("NAME",sField);
m_nameDropDown.AddString(sField);
}
Run Code Online (Sandbox Code Playgroud)
通过这样的事情
for( int nPosInRowset = 0; nPosInRowset < rowsFetched; nPosInRowset++ )
{
//Check if value is null
if (*(recset.m_NameDataLengths + nPosInRowset) == SQL_NULL_DATA)
continue;
CString csComboString;
csComboString = (recset.m_NameData + (nPosInRowset * 30)); //Where 30 is the size specified in RFX_Text_Bulk
m_nameDropDown.AddString(csComboString);
}
Run Code Online (Sandbox Code Playgroud)
编辑:要获取多行,请删除 CRecordset::forwardOnly 选项
编辑2:您还可以保留 CRecordset::forwardonly,但添加 CRecordset::useExtendedFetch 选项