实现批量记录提取

时间:2013-03-01 10:23:37

标签: c++ database mfc fetch bulk

在程序开始时,我需要将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;

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

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

在我的数据库中,INFOTABLE看起来像是:

NAME    AGE
----    ---
Name1   Age1
Name2   Age2
      .
      .
      .
      .

我只需要读取数据库中的数据。有人可以告诉我我做错了什么吗?我的代码现在的行为与普通的提取完全相同。没有发生批量提取。

修改

我刚刚在DBRFX.cpp中探究过,发现RFX_Text_Bulk()将我已通过的m_NameData初始化为new char[nRowsetSize * nMaxLength]

这意味着m_NameData只是一个字符数组!我需要获取多个名称,所以我不需要2D字符数组吗?最奇怪的是,同一RFX_Text_Bulk()将我传递的m_NDCDataLengths初始化为new long[nRowsetSize]。为什么世界上的字符数组需要一个长度数组?!

3 个答案:

答案 0 :(得分:3)

根据http://msdn.microsoft.com/en-us/library/77dcbckz.aspx#_core_how_crecordset_supports_bulk_row_fetching,你必须在调用SetRowsetSize之前用CRecordset :: useMultiRowFetch标志打开CRecordset:

  

要实现批量行提取,您必须指定   在dwOptions参数中的CRecordset :: useMultiRowFetch选项   开放会员功能。要更改行集大小的设置,请调用   SetRowsetSize。

答案 1 :(得分:2)

你几乎做对了。要获取值, 我会改变你的

        for( int rowCount = 1; rowCount <= rowsFetched; rowCount++ )
        {
            recset.SetRowsetCursorPosition(rowCount);
            recset.GetFieldValue("NAME",sField);
            m_nameDropDown.AddString(sField);
        }

通过类似的东西

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

编辑:要获取多行,请删除CRecordset :: forwardOnly选项

编辑2:您也可以保留CRecordset :: forwardonly,但添加CRecordset :: useExtendedFetch选项

答案 2 :(得分:0)

刚遇到同样的问题。 您应该recset.Open()仅使用dwOptions参数CRecordset::useMultiRowFetch,而不是CRecordset::readOnly | CRecordset::useMultiRowFetch。 希望这有助于某人...

编辑: - 重新检查后的情况 - 使用批量记录集并打开CRecordset::forwardOnlyCRecordset::readOnly时,您还必须指定CRecordset::useExtendedFetchdwOptions。对于其他类型的滚动,使用CRecordset::readOnly | CRecordset::useMultiRowFetch就可以了。

相关问题