Click here to Skip to main content
15,886,519 members
Articles / Desktop Programming / MFC

Creating an OLE DB Data Provider

Rate me:
Please Sign up or sign in to vote.
3.45/5 (13 votes)
12 Jan 20029 min read 128.1K   3.4K   69  
This article shows how to create an OLE DB Data Provider that wraps both a C struct and C++ class containing data that is to be made accessible by a SQL query.
///////////////////////////////////////////////////////////////////////////
//
//				The code was borrowed from an article by Len Holgate
//				www.codeproject.com/database/oledb-1.asp 
//
///////////////////////////////////////////////////////////////////////////

#ifndef __I_COMMAND_WITH_PARAMETERS_IMPL__INCLUDED__
#define __I_COMMAND_WITH_PARAMETERS_IMPL__INCLUDED__

#include <atlcom.h>
#include <atldb.h>

///////////////////////////////////////////////////////////////////////////
// class ICommandWithPamametersImpl

template <class T>
class ATL_NO_VTABLE ICommandWithParametersImpl : public ICommandWithParameters
{
public:

   STDMETHOD(GetParameterInfo)(
      ULONG *pcParams, 
      DBPARAMINFO **prgParamInfo, 
      OLECHAR **ppNamesBuffer)
   {
		ATLTRACE2(atlTraceDBProvider, 0, "ICommandWithPamameterImpl::GetParametersInfo\n");
      
      HRESULT hr = E_FAIL;

      if (!pcParams || !prgParamInfo || !ppNamesBuffer)
      {
			ATLTRACE2(atlTraceDBProvider, 0, 
            "ICommandWithPamameterImpl::GetParametersInfo: - Error - a pointer is null\n");
         
         return E_INVALIDARG;
      }

      T *pT = 0;

      *pcParams = 0;
      
      // The way that we use the command parameters means that this never gets called...

      return DB_E_PARAMUNAVAILABLE;
   }
	
   STDMETHOD(MapParameterNames)(
      ULONG cParamNames, 
      const OLECHAR *rgParamnNames[], 
      LONG rgParamOrdinals[])
   {
      ATLTRACE2(atlTraceDBProvider, 0, "ICommandWithPamametersImpl::MapParameterNames\n");

      if (0 == cParamNames)
      {
         return S_OK;
      }

      // The way that we use the command parameters means that this never gets called...

      return E_FAIL;
   }

   STDMETHOD(SetParameterInfo)(
      ULONG cParams, 
      const ULONG rgParamOrdinals[], 
      const DBPARAMBINDINFO rgParamBindInfo[])
   {
      ATLTRACE2(atlTraceDBProvider, 0, "ICommandWithPamametersImpl::SetParameterInfo\n");

      if (0 == cParams)
      {
         return S_OK;
      }

      // We should really check to see that:
      // cParams is 1, 
      // rgParamOrdinals[0] is 1
      // rgParamBindInfo[0]->dwFlags is DBPARAMFLAGS_ISINPUT
      // rgParamBindInfo[0]->pwszDataSourceType is "DBTYPE_IUNKNOWN"
      
      return S_OK;
   }
};

#endif // __I_COMMAND_WITH_PARAMETERS_IMPL__INCLUDED__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
David Utz
Senior Software Engineer
Analytical Graphics, Inc.
40 General Warren Blvd.
Malvern, PA 19355
dutz@stk.com

Comments and Discussions