ole db veri kaynakları (ole db) numaralandır

Bu örnek enumerator nesnesi kullanılabilir veri kaynakları listelemek için nasıl kullanılacağını gösterir.

Tüketici için sqloledb numaralayıcı görünür veri kaynakları listelemek için aramalar ISourcesRowset::GetSourcesRowset yöntemi. Bu yöntem bir görünür veri kaynakları hakkında bilgi döndürür.

Kullanılan ağ kitaplığı bağlı olarak, uygun etki alanı için veri kaynakları aranır. Adlandırılmış kanallar için o hangi istemci oturum etki alanıdır. AppleTalk için varsayılan bölge öyle. Listesini öyle spx/IPX için SQL Serverteçhizatları bağlayıcı bulundu. Banyan VINES için öyle SQL Serverteçhizatları bulunan yerel ağ üzerinde. Birden fazla iletişim kuralı ve TCP/IP soketleri desteklememektedir.

Sunucu kapalı veya açıldığında, bu etki alanlarındaki bilgileri güncelleştirmek için birkaç dakika sürebilir.

Bu örneği, sen-ebilmek download--dan AdventureWorks örnek veritabanını gerektirir Microsoft SQL Server örnekleri ve topluluk projeler Ana sayfa.

Güvenlik notuGüvenlik Notu

Mümkünse Windows Kimlik Doğrulaması kullanın. Windows kimlik doğrulaması kimlik bilgilerini zamanında kullanılabilir, istem girmelerini değilse. Kimlik bilgileri bir dosyada saklamak kaçının. Kimlik ederse gerekir, onlarla şifrelemek Win32 şifreleme API.

ole db veri kaynakları numaralandırılamıyor

  1. Kaynak satır kümesi çağırarak almak ISourceRowset::GetSourcesRowset.

  2. Numaralandırıcıları satır kümesi açıklaması arayarak bulmak GetColumnInfo::IColumnInfo.

  3. Sütun bilgileri bağlama yapılarını oluşturun.

  4. Satır kümesi erişeni çağırarak oluşturmak IAccessor::CreateAccessor.

  5. Satır çağırarak getir IRowset::GetNextRows.

  6. Veri satırı satır kümesi 's kopyasından çağırarak almak IRowset::GetDatave bunu

Örnek

Ole32.lib ile derlemek ve c++ aşağıdaki kod listesi yürütün. Bu uygulama için bilgisayarınızın varsayılan bağlar SQL Serverörneği. Bazı Windows işletim sistemlerinde değişiklik (localhost) ya da (Yerel) adına ihtiyacınız olacak, SQL Serverörneği. Adlandırılmış bir örneğine bağlanmak için L"(local) bağlantı dizesini değiştirin" l "(Yerel) \\name" , burada adıdır adlı örneği. Varsayılan olarak, SQL ServerExpress yükler adlandırılmış bir örneğine. Sqlncli.h içeren dizinin, INCLUDE çevre değişkeni içerdiğinden emin olun.

// compile with: ole32.lib
#define UNICODE
#define _UNICODE
#define DBINITCONSTANTS
#define INITGUID
#define OLEDBVER 0x0250   // to include correct interfaces

#include <windows.h>
#include <stddef.h>
#include <oledb.h>
#include <oledberr.h>
#include <sqlncli.h>
#include <stdio.h>

#define NUMROWS_CHUNK  5

// AdjustLen supports binding on four-byte boundaries.
_inline DBLENGTH AdjustLen(DBLENGTH cb) { 
   return ( (cb + 3) & ~3 );
}

// Get the characteristics of the rowset (the IColumnsInfo interface).
HRESULT GetColumnInfo ( IRowset* pIRowset, 
                        DBORDINAL* pnCols, 
                        DBCOLUMNINFO** ppColumnsInfo,
                        OLECHAR** ppColumnStrings ) {
   IColumnsInfo* pIColumnsInfo;
   HRESULT hr;

   *pnCols = 0;
   if (FAILED(pIRowset->QueryInterface(IID_IColumnsInfo, (void**) &pIColumnsInfo)))
      return (E_FAIL);

   hr = pIColumnsInfo->GetColumnInfo(pnCols, ppColumnsInfo, ppColumnStrings);

   if (FAILED(hr)) {}   /* Process error */ 

   pIColumnsInfo->Release();
   return (hr);
}

// Create binding structures from column information. Binding structures
// will be used to create an accessor that allows row value retrieval.
void CreateDBBindings ( DBORDINAL nCols, 
                        DBCOLUMNINFO* pColumnsInfo, 
                        DBBINDING** ppDBBindings,
                        BYTE** ppRowValues ) {
   ULONG nCol;
   DBLENGTH cbRow = 0;
   DBLENGTH cbCol;
   DBBINDING* pDBBindings;
   BYTE* pRowValues;

   pDBBindings = new DBBINDING[nCols];
   if (!(pDBBindings /* = new DBBINDING[nCols] */ ))
      return;

   for ( nCol = 0 ; nCol < nCols ; nCol++ ) {
      pDBBindings[nCol].iOrdinal = nCol + 1;
      pDBBindings[nCol].pTypeInfo = NULL;
      pDBBindings[nCol].pObject = NULL;
      pDBBindings[nCol].pBindExt = NULL;
      pDBBindings[nCol].dwPart = DBPART_VALUE;
      pDBBindings[nCol].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
      pDBBindings[nCol].eParamIO = DBPARAMIO_NOTPARAM;
      pDBBindings[nCol].dwFlags = 0;
      pDBBindings[nCol].wType = pColumnsInfo[nCol].wType;
      pDBBindings[nCol].bPrecision = pColumnsInfo[nCol].bPrecision;
      pDBBindings[nCol].bScale = pColumnsInfo[nCol].bScale;

      cbCol = pColumnsInfo[nCol].ulColumnSize;

      switch (pColumnsInfo[nCol].wType) {
      case DBTYPE_STR: {
            cbCol += 1;
            break;
         }

      case DBTYPE_WSTR: {
            cbCol = (cbCol + 1) * sizeof(WCHAR);
            break;
         }

      default:
         break;
      }

      pDBBindings[nCol].obValue = cbRow;
      pDBBindings[nCol].cbMaxLen = cbCol;
      cbRow += AdjustLen(cbCol);
   }

   pRowValues = new BYTE[cbRow];
   *ppDBBindings = pDBBindings;
   *ppRowValues = pRowValues;
}

int main() {
   ISourcesRowset* pISourceRowset = NULL;    
   IRowset* pIRowset = NULL;        
   IAccessor* pIAccessor = NULL;
   DBBINDING* pDBBindings = NULL;            

   HROW* pRows = new HROW[500];    
   HACCESSOR hAccessorRetrieve = NULL;        
   ULONG DSSeqNumber = 0;

   HRESULT hr;
   DBORDINAL nCols;
   DBCOLUMNINFO* pColumnsInfo = NULL;
   OLECHAR* pColumnStrings = NULL;
   DBBINDSTATUS* pDBBindStatus = NULL;

   BYTE* pRowValues = NULL;
   DBCOUNTITEM cRowsObtained;
   ULONG iRow;
   char* pMultiByte = NULL;
   short* psSourceType = NULL;
   BYTE* pDatasource = NULL;

   if (!pRows)
      return (0);

   // Initialize COM library.
   CoInitialize(NULL);

   // Initialize the enumerator.
   if (FAILED(CoCreateInstance(CLSID_SQLNCLI11_ENUMERATOR, 
                               NULL,
                               CLSCTX_INPROC_SERVER, 
                               IID_ISourcesRowset, 
                               (void**)&pISourceRowset))) {
      // Process error.
      return TRUE;
   }

   // Retrieve the source rowset.
   hr = pISourceRowset->GetSourcesRowset(NULL, IID_IRowset, 0, NULL, (IUnknown**)&pIRowset);

   pISourceRowset->Release();
   if (FAILED(hr)) {
      // Process error.
      return TRUE;
   }

   // Get the description of the enumerator's rowset.
   if (FAILED(hr = GetColumnInfo(pIRowset, &nCols, &pColumnsInfo, &pColumnStrings))) {
      // Process error.
      goto SAFE_EXIT;
   }

   // Create the binding structures.
   CreateDBBindings(nCols, pColumnsInfo, &pDBBindings, &pRowValues);
   pDBBindStatus = new DBBINDSTATUS[nCols];

   if (sizeof(TCHAR) != sizeof(WCHAR))
      pMultiByte = new char[pDBBindings[0].cbMaxLen];

   if (FAILED(pIRowset->QueryInterface(IID_IAccessor, (void**)&pIAccessor))) {
      // Process error.
      goto SAFE_EXIT;
   }

   // Create the rowset accessor.
   if (FAILED(hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, 
                                              nCols,
                                              pDBBindings, 
                                              0, 
                                              &hAccessorRetrieve, 
                                              pDBBindStatus))) {
      // Process error.
      goto SAFE_EXIT;
   }

   // Process all the rows, NUMROWS_CHUNK rows at a time.
   while (SUCCEEDED(hr)) {
      hr = pIRowset->GetNextRows(NULL, 0, NUMROWS_CHUNK, &cRowsObtained, &pRows);
      if( FAILED(hr)) {
         // process error
      }
      if (cRowsObtained == 0 || FAILED(hr))
         break;

      for (iRow = 0 ; iRow < cRowsObtained ; iRow++) {
         // Get the rowset data.
         if (SUCCEEDED(hr = pIRowset->GetData(pRows[iRow], hAccessorRetrieve, pRowValues))) {
            psSourceType = (short *)(pRowValues + pDBBindings[3].obValue);

            if (*psSourceType == DBSOURCETYPE_DATASOURCE) {
               DSSeqNumber = DSSeqNumber + 1;   // Data source counter.
               pDatasource = (pRowValues + pDBBindings[0].obValue);

               if ( sizeof(TCHAR) != sizeof(WCHAR) ) {
                  WideCharToMultiByte(CP_ACP, 
                                      0,
                                      (WCHAR*)pDatasource, 
                                      -1, 
                                      pMultiByte,
                                      static_cast<int>(pDBBindings[0].cbMaxLen), 
                                      NULL, 
                                      NULL);

                  printf( "DataSource# %d\tName: %S\n", 
                          DSSeqNumber, 
                          (WCHAR *) pMultiByte );
               }
               else {
                  printf( "DataSource# %d\tName: %S\n", 
                          DSSeqNumber, 
                          (WCHAR *) pDatasource );
               }
            }
         }
      }
      pIRowset->ReleaseRows(cRowsObtained, pRows, NULL, NULL, NULL);
   }

   // Release COM library.
   CoUninitialize();

SAFE_EXIT:
   // Do the clean-up.
   return TRUE;
}

// compile with: ole32.lib
#define UNICODE
#define _UNICODE
#define DBINITCONSTANTS
#define INITGUID
#define OLEDBVER 0x0250   // to include correct interfaces

#include <windows.h>
#include <stddef.h>
#include <oledb.h>
#include <oledberr.h>
#include <sqlncli.h>
#include <stdio.h>

#define NUMROWS_CHUNK  5

// AdjustLen supports binding on four-byte boundaries.
_inline DBLENGTH AdjustLen(DBLENGTH cb) { 
   return ( (cb + 3) & ~3 );
}

// Get the characteristics of the rowset (the IColumnsInfo interface).
HRESULT GetColumnInfo ( IRowset* pIRowset, 
                        DBORDINAL* pnCols, 
                        DBCOLUMNINFO** ppColumnsInfo,
                        OLECHAR** ppColumnStrings ) {
   IColumnsInfo* pIColumnsInfo;
   HRESULT hr;

   *pnCols = 0;
   if (FAILED(pIRowset->QueryInterface(IID_IColumnsInfo, (void**) &pIColumnsInfo)))
      return (E_FAIL);

   hr = pIColumnsInfo->GetColumnInfo(pnCols, ppColumnsInfo, ppColumnStrings);

   if (FAILED(hr)) {}   /* Process error */ 

   pIColumnsInfo->Release();
   return (hr);
}

// Create binding structures from column information. Binding structures
// will be used to create an accessor that allows row value retrieval.
void CreateDBBindings ( DBORDINAL nCols, 
                        DBCOLUMNINFO* pColumnsInfo, 
                        DBBINDING** ppDBBindings,
                        BYTE** ppRowValues ) {
   ULONG nCol;
   DBLENGTH cbRow = 0;
   DBLENGTH cbCol;
   DBBINDING* pDBBindings;
   BYTE* pRowValues;

   pDBBindings = new DBBINDING[nCols];
   if (!(pDBBindings /* = new DBBINDING[nCols] */ ))
      return;

   for ( nCol = 0 ; nCol < nCols ; nCol++ ) {
      pDBBindings[nCol].iOrdinal = nCol + 1;
      pDBBindings[nCol].pTypeInfo = NULL;
      pDBBindings[nCol].pObject = NULL;
      pDBBindings[nCol].pBindExt = NULL;
      pDBBindings[nCol].dwPart = DBPART_VALUE;
      pDBBindings[nCol].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
      pDBBindings[nCol].eParamIO = DBPARAMIO_NOTPARAM;
      pDBBindings[nCol].dwFlags = 0;
      pDBBindings[nCol].wType = pColumnsInfo[nCol].wType;
      pDBBindings[nCol].bPrecision = pColumnsInfo[nCol].bPrecision;
      pDBBindings[nCol].bScale = pColumnsInfo[nCol].bScale;

      cbCol = pColumnsInfo[nCol].ulColumnSize;

      switch (pColumnsInfo[nCol].wType) {
      case DBTYPE_STR: {
            cbCol += 1;
            break;
         }

      case DBTYPE_WSTR: {
            cbCol = (cbCol + 1) * sizeof(WCHAR);
            break;
         }

      default:
         break;
      }

      pDBBindings[nCol].obValue = cbRow;
      pDBBindings[nCol].cbMaxLen = cbCol;
      cbRow += AdjustLen(cbCol);
   }

   pRowValues = new BYTE[cbRow];
   *ppDBBindings = pDBBindings;
   *ppRowValues = pRowValues;
}

int main() {
   ISourcesRowset* pISourceRowset = NULL;    
   IRowset* pIRowset = NULL;        
   IAccessor* pIAccessor = NULL;
   DBBINDING* pDBBindings = NULL;            

   HROW* pRows = new HROW[500];    
   HACCESSOR hAccessorRetrieve = NULL;        
   ULONG DSSeqNumber = 0;

   HRESULT hr;
   DBORDINAL nCols;
   DBCOLUMNINFO* pColumnsInfo = NULL;
   OLECHAR* pColumnStrings = NULL;
   DBBINDSTATUS* pDBBindStatus = NULL;

   BYTE* pRowValues = NULL;
   DBCOUNTITEM cRowsObtained;
   ULONG iRow;
   char* pMultiByte = NULL;
   short* psSourceType = NULL;
   BYTE* pDatasource = NULL;

   if (!pRows)
      return (0);

   // Initialize COM library.
   CoInitialize(NULL);

   // Initialize the enumerator.
   if (FAILED(CoCreateInstance(CLSID_SQLNCLI11_ENUMERATOR, 
                               NULL,
                               CLSCTX_INPROC_SERVER, 
                               IID_ISourcesRowset, 
                               (void**)&pISourceRowset))) {
      // Process error.
      return TRUE;
   }

   // Retrieve the source rowset.
   hr = pISourceRowset->GetSourcesRowset(NULL, IID_IRowset, 0, NULL, (IUnknown**)&pIRowset);

   pISourceRowset->Release();
   if (FAILED(hr)) {
      // Process error.
      return TRUE;
   }

   // Get the description of the enumerator's rowset.
   if (FAILED(hr = GetColumnInfo(pIRowset, &nCols, &pColumnsInfo, &pColumnStrings))) {
      // Process error.
      goto SAFE_EXIT;
   }

   // Create the binding structures.
   CreateDBBindings(nCols, pColumnsInfo, &pDBBindings, &pRowValues);
   pDBBindStatus = new DBBINDSTATUS[nCols];

   if (sizeof(TCHAR) != sizeof(WCHAR))
      pMultiByte = new char[pDBBindings[0].cbMaxLen];

   if (FAILED(pIRowset->QueryInterface(IID_IAccessor, (void**)&pIAccessor))) {
      // Process error.
      goto SAFE_EXIT;
   }

   // Create the rowset accessor.
   if (FAILED(hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, 
                                              nCols,
                                              pDBBindings, 
                                              0, 
                                              &hAccessorRetrieve, 
                                              pDBBindStatus))) {
      // Process error.
      goto SAFE_EXIT;
   }

   // Process all the rows, NUMROWS_CHUNK rows at a time.
   while (SUCCEEDED(hr)) {
      hr = pIRowset->GetNextRows(NULL, 0, NUMROWS_CHUNK, &cRowsObtained, &pRows);
      if( FAILED(hr)) {
         // process error
      }
      if (cRowsObtained == 0 || FAILED(hr))
         break;

      for (iRow = 0 ; iRow < cRowsObtained ; iRow++) {
         // Get the rowset data.
         if (SUCCEEDED(hr = pIRowset->GetData(pRows[iRow], hAccessorRetrieve, pRowValues))) {
            psSourceType = (short *)(pRowValues + pDBBindings[3].obValue);

            if (*psSourceType == DBSOURCETYPE_DATASOURCE) {
               DSSeqNumber = DSSeqNumber + 1;   // Data source counter.
               pDatasource = (pRowValues + pDBBindings[0].obValue);

               if ( sizeof(TCHAR) != sizeof(WCHAR) ) {
                  WideCharToMultiByte(CP_ACP, 
                                      0,
                                      (WCHAR*)pDatasource, 
                                      -1, 
                                      pMultiByte,
                                      static_cast<int>(pDBBindings[0].cbMaxLen), 
                                      NULL, 
                                      NULL);

                  printf( "DataSource# %d\tName: %S\n", 
                          DSSeqNumber, 
                          (WCHAR *) pMultiByte );
               }
               else {
                  printf( "DataSource# %d\tName: %S\n", 
                          DSSeqNumber, 
                          (WCHAR *) pDatasource );
               }
            }
         }
      }
      pIRowset->ReleaseRows(cRowsObtained, pRows, NULL, NULL, NULL);
   }

   // Release COM library.
   CoUninitialize();

SAFE_EXIT:
   // Do the clean-up.
   return TRUE;
}