Teach Yourself Database Programming
with Visual C++ 6 in 21 days


Appendix E

Using ADO via the OLE DB SDK


To use ADO in your C++ applications, you can use the ADO header files and import library from the OLE DB SDK. You include the ADO header files (adoid.h and adoint.h) in your source, and add the ADO import library adoid.lib to your linker input. This enables you to create instances of the ADO objects and access their member functions. The code listings that follow show the changes you would need to make to a typical MFC application to use ADO via the OLE DB SDK. A sample MFC application called ADOSDK is on the CD.


Listing E.1  Additions to StdAfx.h

 1:  #include <adoid.h>
 2:  #include <adoint.h>
 3:  #include <comdef.h>


Listing E.2   Additions to the Document Header File

 1:  class CADOSDKDoc : public CDocument
 2:  {
 3:  protected: // create from serialization only
 4:    CADOSDKDoc();
 5:    DECLARE_DYNCREATE(CADOSDKDoc)
 6:
 7:  // Attributes
 8:  public:
 9:    ADOConnection* m_piConnection;
10:    BOOL m_ConnectionOpen;

Lines 9 and 10 are the new lines you need to add. The other lines should be there already.


Listing E.3  Additions to the Document Constructor

 1:  CADOSDKDoc::CADOSDKDoc()
 2:  {
 3:    m_piConnection = NULL;
 4:    m_ConnectionOpen = FALSE;
 5:  }

Lines 4 and 5 are the new lines you need to add. The other lines should be there already.


Listing E.4  Additions to the ONNEWDOCUMENT Function

 1:  BOOL CADOSDKDoc::OnNewDocument()
 2:  {
 3:    if (!CDocument::OnNewDocument())
 4:      return FALSE;
 5:
 6:    HRESULT hr;
 7:
 8:    hr = CoCreateInstance(CLSID_CADOConnection, NULL,
 9:      CLSCTX_INPROC_SERVER, IID_IADOConnection,
10:      (LPVOID *)&m_piConnection);
11:    if (!FAILED(hr))
12:    {
13:      hr = m_piConnection->Open(bstr_t(
14:        _ L"Provider=Microsoft.Jet.OLEDB.3.51;Data 
                       Source=c:\\tysdbvc\\vcdb.mdb;"),
15:        NULL, NULL);
16:      if (!FAILED(hr))
17:      {
18:        m_ConnectionOpen = TRUE;
19:      }
21:    }
22:
23:    return TRUE;
24:  }

Lines 6-21 are the new lines you need to add. The other lines should be there already. Line 14 will change, depending on the location of the file.


Listing E.5  Additions to the ONCLOSEDOCUMENT Function

 1:  void CADOSDKDoc::OnCloseDocument()
 2:  {
 3:    if (m_ConnectionOpen)
 4:    {
 5:      m_piConnection->Close();
 6:      m_ConnectionOpen = FALSE;
 7:    }
 8:    if (m_piConnection)
 9:    {
10:      m_piConnection->Release();
11:    }
12:
13:    CDocument::OnCloseDocument();
14:  }

Lines 3-11 are the new lines you need to add. The other lines should be there already from ClassWizard.


Listing E.6  Additions to the ONRBUTTONDOWN Function

 1:  void CADOSDKView::OnRButtonDown(UINT nFlags, CPoint point)
 2:  {
 3:    CADOSDKDoc * pDoc = GetDocument();
 4:    HRESULT hr;
 5:    ADORecordset * pRs = NULL;
 6:    short sEOF;
 7:    _variant_t vLastName;
 8:
 9:    if (pDoc->m_ConnectionOpen)
10:    {
11:      hr = pDoc->m_piConnection->Execute(
12:        _bstr_t(L"SELECT * FROM Customers"),
13:        &(_variant_t(0L)),
14:        adCmdText,
15:        &pRs);
16:
17:      if (SUCCEEDED(hr))
18:      {
19:        pRs->get_EOF(&sEOF);
20:        while (!sEOF)
21:        {
22:          hr = pRs->get_Collect(_variant_t(L"CustLastName"), 
                &vLastName);
23:          if (SUCCEEDED(hr))
24:          {
25:            TRACE("Last Name:%s.\n", (LPCTSTR) (_bstr_t) vLastName);
26:          }
27:          pRs->MoveNext();
28:          pRs->get_EOF(&sEOF);
29:        }
30:        pRs->Close();
31:        pRs->Release();
32:      }
33:
34:    }
35:
36:    CView::OnRButtonDown(nFlags, point);
37:  }

Lines 3-34 are the new lines you need to add. The other lines should be there already from ClassWizard.


© Copyright, Sams Publishing. All rights reserved.