데이터 페치

데이터 원본, 세션 및 행 집합 개체를 연 후 데이터를 가져올 수 있습니다. 사용 중인 접근자 유형에 따라 열을 바인딩해야 할 수 있습니다.

데이터를 가져오려면

  1. 적절한 Open 명령을 사용하여 행 집합을 엽니다 .

  2. 사용 CManualAccessor중인 경우 출력 열을 아직 바인딩하지 않은 경우 바인딩합니다. 다음 예제는 DBViewer 샘플에서 가져옵니다. 열을 바인딩하려면 다음 예제와 같이 바인딩을 사용하여 접근자를 호출 GetColumnInfo한 다음 만듭니다.

    // 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();
    
  3. 루프를 while 작성하여 데이터를 검색합니다. 루프에서 다음 예제와 같이 커서를 앞으로 이동하고 S_OK 대해 반환 값을 테스트하기 위해 호출 MoveNext 합니다.

    while (rs.MoveNext() == S_OK)
    {
        // Add code to fetch data here
        // If you are not using an auto accessor, call rs.GetData()
    }
    
  4. 루프 내에서 while 접근자 유형에 따라 데이터를 가져올 수 있습니다.

    • CAccessor 클래스를 사용하는 경우 데이터 멤버를 포함하는 사용자 레코드가 있어야 합니다. 다음 예제와 같이 해당 데이터 멤버를 사용하여 데이터에 액세스할 수 있습니다.

      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);
      }
      
    • 또는 CDynamicParameterAccessor 클래스를 CDynamicAccessor 사용하는 경우 다음 예제와 같이 액세스 함수를 GetValue GetColumn사용하여 데이터를 가져올 수 있습니다. 사용 중인 데이터 형식을 확인하려면 .를 사용합니다 GetType.

      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);
          }
      }
      
    • 사용하는 CManualAccessor경우 다음 예제와 같이 사용자 고유의 데이터 멤버를 지정하고, 직접 바인딩하고, 직접 액세스해야 합니다.

      while (rs.MoveNext() == S_OK)
      {
          // Use the data members you specified in the calls to
          // AddBindEntry.
      
          wsprintf_s("%s", szFoo);
      }
      

참고 항목

OLE DB 소비자 템플릿 작업