Home Search Contact us About us
Title Reading from an MS Access database using ADO.
Summary ADO is currently flavour of the year for accessing databases. This article provides a simple example of how to read from a MS Access database using ADO.
Contributor John McTainsh
Published 22-Feb-2001
Last updated 22-Feb-2001
Page rating   80% for 5 votes Useless Brilliant

Description.

ADO is presently all the rage with fashionable developers so I thought I better work out how to use it. Here you will find a very simple example of reading from a database, the ADO way

Using the correct header.

Before we get too excited we better include some stuff to get started at the correct location.

#include <atlbase.h>                    //This may not be necessary
//Load the ADO driver
#pragma warning(disable : 4146)
#import "c:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#pragma warning(default: 4146)

Setting up and using.

Add to your class header.

_RecordsetPtr   m_pRs;                          //Access to the database record set

Add to the implementation file the following two handy functions. One to turn bad HRESULTS into exceptions and the other to extract a string from a record given the field name.

// ***************************************************************************
//DESCRIPTION:
//      Handy function to turn bad hresult into exception
//PARAMS:
//      hr  hresult to throw the execption on
//CREATED:
//      10-12-2000, 9:04:00 PM by john@mctainsh.com
// ***************************************************************************
inline void ThrowHr( HRESULT hr ) 
{ 
    if( FAILED( hr ) )
        _com_issue_error( hr ); 
}

// ***************************************************************************
//DESCRIPTION:
//      Read the given string from the record set
//PARAMS:
//      pRs             Record sert to read from.
//      sColumnName     Title of column to read
//RETURN:
//      the string from the column of the given title.
//CREATED:
//      7-11-2000, 9:07:54 PM by john@mctainsh.com
// ***************************************************************************
CString GetFieldString( _RecordsetPtr pRs, CString sColumnName )
{
    //Read the filed
    FieldPtr field = pRs->Fields->GetItem( (_bstr_t)sColumnName );
    variant_t FldVal = field->GetValue();
    
    //Convert to a string
    CString sReturn;
    if( FldVal.vt != VT_NULL )
    {
        ASSERT( FldVal.vt == VT_BSTR );
        sReturn = (LPCSTR)(_bstr_t)FldVal.bstrVal;
    }
    return sReturn;
}

Now lets open and read from the database.

    CString sProvider = _T("Driver={Microsoft Access Driver (*.mdb)};")
                            _T("DBQ=D:\\Inetpub\\DevelopmentPrivate\\VisualCIdeas.mdb;"); 
    //
    //Open a comment to an ADO record set from [Page] table
    //
    try 
    {
        m_pRs.CreateInstance( "ADODB.Recordset" );

        ThrowHr( m_pRs->Open("Page",
                            sProvider, 
                            adOpenStatic, adLockOptimistic, adCmdTable ) );
        ThrowHr( m_pRs->MoveFirst() );      
    }
    catch (_com_error &e)
    {
        TRACE( _T("Error:       OpenDatabase\n") );
        TRACE( _T("Code         = %08lx\n"), e.Error() );
        TRACE( _T("Code meaning = %s\n"),    e.ErrorMessage() );
        TRACE( _T("Source       = %s\n"),    (LPCSTR)e.Source() );
        TRACE( _T("Description  = %s\n"),    (LPCSTR)e.Description() );
        ASSERT( true );
    }


...

    //
    //Read all data from the [Page] database
    //
    try 
    {       
        //Loop on until all items are read
        while( !m_pRs->EndOfFile )
        {       
            //Read the record data
            CString sTitle      = GetFieldString( m_pRs, "Title" );
            CString sSummary        = GetFieldString( m_pRs, "Summary" );
            CString sContrubutor    = GetFieldString( m_pRs, "Contrubutor" );
            CString sReadPath       = GetFieldString( m_pRs, "Path" );

            m_sHitTitle         = sTitle;
            m_sHitSummary           = sSummary;

            //Progress display
            TRACE( "Checking - %s\n", m_sHitTitle );

            //Lets move on
            ThrowHr( m_pRs->MoveNext() );
        }
        
        m_pRs->Close();
        m_pRs->Release();
    }
    catch (_com_error &e)
    {
        TRACE( _T("Error:       GetHit\n") );
        TRACE( _T("Code         = %08lx\n"), e.Error() );
        TRACE( _T("Code meaning = %s\n"),    e.ErrorMessage() );
        TRACE( _T("Source       = %s\n"),    (LPCSTR)e.Source() );
        TRACE( _T("Description  = %s\n"),    (LPCSTR)e.Description() );
        ASSERT( true );
    }

Coming soon a notes on how to modify and delete using ADO.

Comments Date
Home Search Contact us About us