⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 igetasadorecordset.cpp

📁 The ATL OLE DB Provider templates only seem to support read-only rowsets and making them support upd
💻 CPP
字号:
#include "stdafx.h"

#define INITGUID
#include "IGetAsADORecordset.h"

#import "D:\Program Files\Common Files\System\ado\msado15.dll" rename( "EOF", "adoEOF" ) 

HRESULT IGetAsADORecordsetImpl_GetRecordset(
   IUnknown *pUnkOuter,
   REFIID riid,
   IUnknown **ppRecordset,
   IUnknown **ppUnknown)
{
   if (!ppRecordset || (pUnkOuter && !ppUnknown))
   {
      return E_POINTER;
   }

   HRESULT hr = E_FAIL;

   try
   {
      CLSID clsid;

		hr = CLSIDFromProgID(L"ADODB.Recordset", &clsid);

      if (SUCCEEDED(hr))
      {
         IUnknown *pIUnknown = 0;

         hr = CoCreateInstance(clsid, pUnkOuter, CLSCTX_INPROC_SERVER, IID_IUnknown, reinterpret_cast<void**>(&pIUnknown));

			if (FAILED(hr))
         {
				return hr;
			}

			hr = pIUnknown->QueryInterface(riid, reinterpret_cast<void**>(ppRecordset));

         if (SUCCEEDED(hr) && pUnkOuter)
         {
            *ppUnknown = pIUnknown;
            pIUnknown->AddRef();
         }

			pIUnknown->Release();

      }
   }
   catch(_com_error &e)
   {
      return e.Error();
   }
   catch(...)
   {
      return hr;
   }

   return S_OK;
}

///////////////////////////////////////////////////////////////////////////////
// IGetAsADORecordsetImpl_GetAsRecordSetHelper
///////////////////////////////////////////////////////////////////////////////

STDMETHODIMP IGetAsADORecordsetImpl_GetAsRecordSetHelper(
   LPCTSTR lpszProvider,
   IUnknown *pUnknown, 
   CursorLocationEnum cusorLocation,
   LockTypeEnum lockType,
   CursorTypeEnum cursorType,
   IUnknown *pRecordset)
{
   try
   {
      // Create a connection to our OLEDB "provider"

      ADODB::_ConnectionPtr Connection(L"ADODB.Connection");

      Connection->Open(
         _bstr_t( lpszProvider ),
         _bstr_t(""), 
         _bstr_t(""), 
         -1);

      // Build our command, this has a single parameter which is the 
      // IUnknown of the object we're returning a recordset representation
      // of... Once inside the command we'll QI for _IGetAsOLEDBRowset and
      // allow the object to do the real work of providing a recordset 
      // representation of itself.

      ADODB::_CommandPtr Command(L"ADODB.Command");

      Command->CommandText = _bstr_t(L"CONVERT");

      // The command object appears to take ownership of the unknown passed
      // in to it as a parameter but doesnt AddRef it, it just assumes it
      // can Release it. This means we need to AddRef it here if we want to
      // be able to shut down without a crash...

      pUnknown->AddRef();

      ADODB::_ParameterPtr Param1 = Command->CreateParameter(
         _bstr_t(L"IUnknown"),
         ADODB::adIUnknown,
         ADODB::adParamInput,
         -1,
         _variant_t(pUnknown));

      Command->Parameters->Append( Param1 );   
      Command->ActiveConnection = Connection;

      CComQIPtr<IDispatch> spCommand = Command;

      // Using our recordset, set the cursor and lock information and then open it
      // using the command above...
      // Note: We MUST do things this way if we want the cursor and lock type information
      // to be as we specify, all other ways that look as if they might work, dont.
      
      ADODB::_RecordsetPtr Rs1(pRecordset);

      _variant_t  vtEmpty (DISP_E_PARAMNOTFOUND, VT_ERROR);

      Rs1->CursorLocation = (ADODB::CursorLocationEnum)cusorLocation; 
      Rs1->CursorType = (ADODB::CursorTypeEnum)cursorType;
      Rs1->LockType = (ADODB::LockTypeEnum)lockType;
      Rs1->Open(
         _variant_t(spCommand), 
         vtEmpty,                   // mustn't specify a connection, we use the one in the command
         ADODB::adOpenUnspecified,  // mustn't specify here, must set before calling open
         ADODB::adLockUnspecified,  // mustn't specify here, must set before calling open
         -1);

   }
   catch(_com_error &e)
   {
      return e.Error();
   }
   catch(...)
   {
      return E_FAIL;
   }

   return S_OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -