Database Programming ATL

How to use ATL OLE/DB code with an ADO connection

.

Environment: VC++ 6, Windows 2000

This article is for Visual C++ -- OLE/DB programmers that are using ATL but must interact with components or code (from VB or even Visual C++) using ADO. Some time ago was really a problem for me to use such components without changing my code that was using ATL OLE/DB style for accessing a database.

The key code is where the ADO.s Connection is used to get a CSession:

.  .  .

GUID
IID_ACC={0x00000516,0x0000,0x0010,0x80,0x00,
         0x00,0xAA,0x00,0x6D,0x2E,0xA4};
   HRESULT hr;

   try
   {

      if(!m_pADOconnection->CreateDispatch(
         _T("ADODB.Connection"), e))
             throw e;

      CString strConnection;
      strConnection.Format("Provider=Microsoft.Jet.OLEDB.4.0;
                           DataSource=db.mdb;");

      m_pADOconnection->Open(strConnection,"admin","", 0);

      propd=m_pADOconnection->m_lpDispatch;
      hr=propd->QueryInterface(IID_ACC,(void**)&pUnk);
      if(FAILED(hr)) return hr;
      hr=((ADOConnectionConstruction15*)pUnk)->
                      get_Session(&pSession);
      if(FAILED(hr)) return hr;

      hr=pSession->QueryInterface( IID_IOpenRowset,
                                   (void**)&pOpenRowset);
      if(FAILED(hr)) return hr;
      m_pSession->m_spOpenRowset.Attach(
                           (IOpenRowset*)pOpenRowset);
   }
   catch(...)

   . . .

Downloads

Download source - 35 Kb


Comments

  • How to query.

    Posted by Legacy on 04/10/2003 12:00am

    Originally posted by: csjung

    How can i insert data and query using this code?
    eg) Inset Into mlist VALUES(~~~)
    : to insert data.
    Select * from mlist where FirstName = 'name1'
    : to query record.

    Thanks in advance.
    Jung.

    Reply
  • how to use SQL Server for this example?

    Posted by Legacy on 12/21/2001 12:00am

    Originally posted by: James


    Thanks, I have a SQL Server DB in another computer,
    its Data Source name is: DBTest

    how can I connect to it with your sample?

    James

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 6, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds