Knowledge Base

How To Pass Multiple DBPROPSETs to CDataSource::Open

Article ID: 191747

Article Last Modified on 3/2/2005


APPLIES TO


This article was previously published under Q191747

SUMMARY

This article demonstrates how to use ATL OLE DB Consumer Classes for passing multiple DBPROPSETs to CDataSource::Open. It also shows how to use provider-specific properties of the SQLOLEDB provider because this represents a good example of when you would want to use multiple property sets.

MORE INFORMATION

Sample Code

// In the provider-specific property set DBPROPSET_SQLSERVERDBINIT,
// SQLOLEDB defines additional initialization properties such as
// SSPROP_INIT_APPNAME ( VT_BSTR ).

// You need the SQLOLEDB provider include file, sqloledb.h.
   #include <atldbcli.h>
   #define DBINITCONSTANTS
   #include <sqloledb.h>

   void SomeFunc()
   {
     ...
      CDataSource connection1 ;

      CDBPropSet propset[2]={DBPROPSET_DBINIT, DBPROPSET_SQLSERVERDBINIT};

      propset[0].AddProperty(DBPROP_INIT_DATASOURCE, L"mySQLServer");
      propset[0].AddProperty(DBPROP_INIT_CATALOG, L"pubs");
      propset[0].AddProperty(DBPROP_AUTH_USERID, L"sa");

      propset[1].AddProperty(SSPROP_INIT_APPNAME,
                             L"SQLOLEDB ATL Consumer");
      hr = connection1.Open("SQLOLEDB", propset, 2);
      BSTR bstr;
      connection1.GetInitializationString(&bstr);
     ...
   }
				
To see the complete connection string including the extended properties, drag the address of the BSTR and drop it in the debugger's memory window.

NOTE: there is an error in the online Help for CDataSource::Open. It does not show the third parameter, the number of DBPROPSETs.

NOTE: SQLOLEDB.h is not installed by Visual C++, version 6.0. The following article in the Microsoft Knowledge Base shows how to get the header file:

190725 FILE: SqlOleDb.h Not Installed by VC++ 6.0

REFERENCES

The following article in the Microsoft Knowledge Base describes the requirements to add OLE DB template support to Visual C++ projects:

190959 PRB: OLE DB Consumer Template Wizard Requires ATL Project

Keywords: kbhowto kbconsumer kbdatabase kbdtl KB191747