Microsoft Knowledge Base Email Alertz

(191746) - This article demonstrates how to use ATL Consumer Classes with the SQLOLEDB provider to connect to SQL Server 6.5, 7.0, and 2000.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 191746 - Last Review: March 2, 2005 - Revision: 2.3

HOWTO: Use ATL Consumer Classes to Connect to SQL Server

This article was previously published under Q191746

On This Page

SUMMARY

This article demonstrates how to use ATL Consumer Classes with the SQLOLEDB provider to connect to SQL Server 6.5, 7.0, and 2000.

MORE INFORMATION

The following sample code is an OLE DB Consumer Application that uses OLE DB Consumer Template classes. It demonstrates three ways to use the SQLOLEDB provider to connect to a SQL Server database.

Sample Code

Note You must change User ID <username> and Password <strong password> to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database.
   HRESULT hr ;

   //Initialize COM.
   hr = CoInitialize(NULL);

   CDataSource connection1 ;
   CDataSource connection2 ;
   CDataSource connection3 ;


   // ===================================================
   // Method #1: Setting OLE DB Initialization Properties
   // ===================================================
   CDBPropSet propset(DBPROPSET_DBINIT);
   propset.AddProperty(DBPROP_INIT_DATASOURCE, L"mySQLServer");
   propset.AddProperty(DBPROP_INIT_CATALOG, L"pubs");
   propset.AddProperty(DBPROP_AUTH_USERID, L"<username>");
   propset.AddProperty(DBPROP_AUTH_PASSWORD, L"<strong name>");
   hr = connection1.Open("SQLOLEDB", &propset);
   connection1.Close();

   // ===================================================
   // Method #2: Using MS Data Link dialog
   // ===================================================

   hr = connection2.Open(GetDesktopWindow());
   BSTR bstr;
   connection2.GetInitializationString(&bstr);
   // bstr contains the initialization string.
    connection2.Close();

   // ===================================================
   // Method #3: Supplying a provider string
   // ===================================================
   // This may take a relatively longer amount of time.

   hr = connection3.OpenFromInitializationString
        (L"Provider=SQLOLEDB;User ID=<username>;Password=<strong name>;Data Source=mySQLServer;
        Initial Catalog=pubs;");
   connection3.Close();
				

REFERENCES

MSDN Library Visual Studio 6.0.

The following article in the Microsoft Knowledge Base describes the requirements to add OLE DB template support to Visual C++ projects:
190959  (http://kbalertz.com/Feedback.aspx?kbNumber=190959/EN-US/ ) PRB: OLE DB Consumer Template Wizard Requires ATL Project

APPLIES TO
  • Microsoft OLE DB 2.0, when used with:
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
Keywords: 
kbconsumer kbdatabase kbdtl kbhowto KB191746
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate