Fetching Data
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Fetching Data.
After you open the data source, session, and rowset objects, you can fetch data. Depending on the type of accessor you are using, you might need to bind columns.
To fetch data
Open the rowset using the appropriate Open command.
If you are using
CManualAccessor
, bind the output columns if you have not already done so. To bind the columns, callGetColumnInfo
, and then create an accessor with the bindings, as shown in the following example:// From the DBViewer Sample CDBTreeView::OnQueryEdit // Get the column information ULONG ulColumns = 0; DBCOLUMNINFO* pColumnInfo = NULL; LPOLESTR pStrings = NULL; if (rs.GetColumnInfo(&ulColumns, &pColumnInfo, &pStrings) != S_OK) ThrowMyOLEDBException(rs.m_pRowset, IID_IColumnsInfo); struct MYBIND* pBind = new MYBIND[ulColumns]; rs.CreateAccessor(ulColumns, &pBind[0], sizeof(MYBIND)*ulColumns); for (ULONG l=0; l<ulColumns; l++) rs.AddBindEntry(l+1, DBTYPE_STR, sizeof(TCHAR)*40, &pBind[l].szValue, NULL, &pBind[l].dwStatus); rs.Bind();
Write a
while
loop to retrieve the data. In the loop, callMoveNext
to advance the cursor and test the return value against S_OK, as shown in the following example:while (rs.MoveNext() == S_OK) { // Add code to fetch data here // If you are not using an auto accessor, call rs.GetData() }
Within the
while
loop, you can fetch the data according to your accessor type.If you use the CAccessor class, you should have a user record that contains data members. You can access your data using those data members, as shown in the following example:
while (rs.MoveNext() == S_OK) { // Use the data members directly. In this case, m_nFooID // is declared in a user record that derives from // CAccessor wsprintf_s("%d", rs.m_nFooID); }
If you use the
CDynamicAccessor
orCDynamicParameterAccessor
class, you can fetch data by using the accessing functionsGetValue
andGetColumn
, as shown in the following example. If you want to determine the type of data you are using, useGetType
.while (rs.MoveNext() == S_OK) { // Use the dynamic accessor functions to retrieve your data. ULONG ulColumns = rs.GetColumnCount(); for (ULONG i=0; i<ulColumns; i++) { rs.GetValue(i); } }
If you use
CManualAccessor
, you must specify your own data members, bind them yourself, and access them directly, as shown in the following example:while (rs.MoveNext() == S_OK) { // Use the data members you specified in the calls to // AddBindEntry. wsprintf_s("%s", szFoo); }