description | title | ms.date | helpviewer_keywords | ms.assetid | ||||
---|---|---|---|---|---|---|---|---|
Learn more about: Fetching Data |
Fetching Data |
10/19/2018 |
|
b07f747f-9855-4f27-a03d-b1d5b10fa284 |
After you open the data source, session, and rowset objects, you can fetch data. Depending on the type of accessor you're using, you might need to bind columns.
-
Open the rowset using the appropriate Open command.
-
If you're using
CManualAccessor
, bind the output columns if you haven't already done so. The following example is taken from the DBViewer sample. 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're 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); }
-