Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing bulk record fetching
    text
    copied!<p>At the start of my program, I need to read data from a MS Access database (.mdb) into a drop down control. This is done so that whenever the user types in that control, the application can auto-complete.</p> <p>Anyway, the reading from database took forever so I thought I'd implement bulk row fetching.</p> <p>This is the code I have:</p> <pre><code>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( &amp;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 &lt;= 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-&gt;m_strError); } END_CATCH; </code></pre> <p><code>MultiRowset.cpp</code> looks like:</p> <pre><code>#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-&gt;SetFieldType(CFieldExchange::outputColumn); RFX_Text_Bulk(pFX, _T("[NAME]"), &amp;m_NameData, &amp;m_NameDataLengths, 30); } </code></pre> <p><code>MultiRowset.h</code> looks like:</p> <pre><code>#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 </code></pre> <p>And in my database, the <code>INFOTABLE</code> looks like:</p> <pre><code>NAME AGE ---- --- Name1 Age1 Name2 Age2 . . . . </code></pre> <p>All I need to do is only <em>read</em> the data from the database. Can someone please tell me what I'm doing wrong? My code right now behaves exactly like a normal fetch. There's no bulk fetching happening.</p> <p><strong>EDIT:</strong></p> <p>I just poked around in <code>DBRFX.cpp</code> and found out that <code>RFX_Text_Bulk()</code> initializes my passed <code>m_NameData</code> as <code>new char[nRowsetSize * nMaxLength]</code>!</p> <p>This means <code>m_NameData</code> is only a character array! I need to fetch multiple names, so wouldn't I need a 2D character array? The strangest thing is, the same <code>RFX_Text_Bulk()</code> initializes my passed <code>m_NDCDataLengths</code> as <code>new long[nRowsetSize]</code>. Why in the world would a character array need an array of lengths?!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload