Wrox Programmer Forums
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2005, 01:19 PM
Registered User
 
Join Date: Feb 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple ADO with Access

Can anyone direct me to an example of a C++ program that connects to an Access database (with ADO or anything) and does a simple retrive of data. Don't need any grids, just get the data.

Thanks,
Geoff :)

 
Old March 15th, 2005, 06:38 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")
#include <comdef.h>
#include <atlbase.h>



    _ConnectionPtr m_pConnection;
    _CommandPtr m_pCommand;
    _RecordsetPtr m_pRecordset;




    try
    {
// AfxMessageBox("Befor open");
        m_pConnection->ConnectionTimeout = 180;
// m_pConnection->ConnectionString = "DSN=ankur;UID=sbpfinder;PWD=projectfind;";
        hr = m_pConnection->Open(L"ankur",L"sbpfinder",L"projectfind",NULL) ;

        //hr = m_pConnection->Open(strCnn,"","",adConnectUnspecified);
        if(!SUCCEEDED(hr))
            return FALSE;
        m_pCommand->ActiveConnection = m_pConnection;

        hr = m_pConnection->Errors->Clear();
        if(!SUCCEEDED(hr))
            return FALSE;

        m_pRecordset->Open ("permission",_variant_t((IDispatch *)m_pConnection,
            true),adOpenStatic,adLockOptimistic, adCmdTable);

        m_pConnection->Execute(strDeterminePermission,NULL,adExecuteNoRe cords);
        m_pRecordset->Requery(adCmdUnknown);
        while(!m_pRecordset->adoEOF)
        {

    _variant_t TheValue6;
    _variant_t TheValue7;
    _variant_t TheValue8;
    _variant_t TheValue9;
    _variant_t TheValue10;

            TheValue6 = m_pRecordset->GetCollect("book_id");
            TheValue7 = m_pRecordset->GetCollect("st_lesson_id");
            TheValue8 = m_pRecordset->GetCollect("end_lesson_id");
            TheValue9 = m_pRecordset->GetCollect("author_id");
            TheValue10 = m_pRecordset->GetCollect("permission_level");

            TheValue6.ChangeType(VT_BSTR);
            TheValue7.ChangeType(VT_BSTR);
            TheValue8.ChangeType(VT_BSTR);
            TheValue9.ChangeType(VT_INT);
            TheValue10.ChangeType(VT_INT);

            if((TheValue9.intVal == AuthorID)
                && strcmp(OLE2T(TheValue6.bstrVal),((CAdminModuleApp *)AfxGetApp())->cmdInfo.m_strBookArg) == 0)
            {
                permissioninfo * newnode = new permissioninfo;
                newnode->BookID = TheValue6.bstrVal;
                newnode->stLessonID = TheValue7.bstrVal;
                newnode->endLessonID = TheValue8.bstrVal;
                newnode->Permission = TheValue10.intVal;

                if(m_pTocInfo.GetCount() == 0)
                    m_pPermissionInfo.AddHead(newnode);
                else
                    m_pPermissionInfo.AddTail(newnode);

TRACE("UNDER WHILE\n");

            }
            m_pRecordset->MoveNext();
        }

        m_pRecordset->Close();
        /*********************/

        m_pRecordset->Open ("book",_variant_t((IDispatch *)m_pConnection,
            true),adOpenStatic,adLockOptimistic, adCmdTable);

        m_pConnection->Execute(strFindBookName,NULL,adExecuteNoRecords );
        m_pRecordset->Requery(adCmdUnknown);

        while(!m_pRecordset->adoEOF)
        {
    _variant_t BName;
    _variant_t BDecs;
    _variant_t BID;

            BName = m_pRecordset->GetCollect("book_name");
            BDecs = m_pRecordset->GetCollect("book_desc");
            BID = m_pRecordset->GetCollect("book_id");
            BID.ChangeType(VT_BSTR);

            if(((CAdminModuleApp *)AfxGetApp())->cmdInfo.m_strBookArg == OLE2T(BID.bstrVal))
            {
                m_strBookName.Format("%s",(char*)(_bstr_t(BName))) ;
                m_strBookDesc.Format("%s",(char*)(_bstr_t(BDecs))) ;
                //---used by dev---//
                _variant_t BId;
                BId = m_pRecordset->GetCollect("book_id");
                g_BookId.Format("%s",(char*)(_bstr_t(BId)));
                g_BookDesc = m_strBookName;
                //-----------------//
            }
            m_pRecordset->MoveNext();
        }

        m_pRecordset->Close();

        /************************************************** *************/
        m_pRecordset->Open ("toc",_variant_t((IDispatch *)m_pConnection,
            true),adOpenStatic,adLockOptimistic, adCmdTable);

        m_pConnection->Execute(strSQLChange,NULL,adExecuteNoRecords);
        m_pRecordset->Requery(adCmdUnknown);

        int noofrec = 0;
        while(!m_pRecordset->adoEOF)
        {
            ++noofrec;
            m_pRecordset->MoveNext();
        }

        m_pRecordset->Requery(adCmdUnknown);
        noofrec = -1;
        dlg.m_progress.StepIt();
        while(!m_pRecordset->adoEOF)
        {


_variant_t TheValue;
_variant_t TheValue1;
_variant_t TheValue2;
_variant_t TheValue3;
_variant_t TheValue4;
_variant_t TheValue5;


            TheValue = m_pRecordset->GetCollect("book_id");
            TheValue1 = m_pRecordset->GetCollect("lesson_id");
            TheValue2 = m_pRecordset->GetCollect("sublesson_id");
            TheValue3 = m_pRecordset->GetCollect("toc_id");
            TheValue4 = m_pRecordset->GetCollect("toc_title");
            TheValue5 = m_pRecordset->GetCollect("modified_date");
            if(TheValue.vt!=VT_NULL || TheValue1.vt!=VT_NULL || TheValue2.vt!=VT_NULL)
            {
                CString ss;ss.Format("%04s%02s",OLE2T(TheValue.bstrVal),OL E2T(TheValue1.bstrVal));
                int a = SearchForThePermissionOfALesson(ss);
                if(a == -1)
                {
                    if(!m_pRecordset->adoEOF)
                        m_pRecordset->MoveNext();
                    continue;
                }

                wsprintf(ssss,"%s%s%s%s",
                    (char*)_bstr_t(TheValue),
                    (char*)_bstr_t(TheValue1),
                    (char*)_bstr_t(TheValue2),
                    (char*)_bstr_t(TheValue3));

                ++noofrec;
                nodeinfo * newnode = new nodeinfo;

                TheValue.ChangeType(VT_INT);
                newnode->BookID = TheValue.intVal;

                TheValue1.ChangeType(VT_INT);
                newnode->LessonID = TheValue1.intVal;

                TheValue2.ChangeType(VT_INT);
                newnode->SubLessonID = TheValue2.intVal;

                TheValue3.ChangeType(VT_INT);
                newnode->SubSubLessonID = TheValue3.intVal;

                newnode->Text.Format("%s",(char*)(_bstr_t(TheValue4)));
                newnode->Permission = a;

                newnode->ID.Format("%s",ssss);

                if(m_pTocInfo.GetCount() == 0)
                    m_pTocInfo.AddHead(newnode);
                else
                    m_pTocInfo.AddTail(newnode);
            }
            if(!m_pRecordset->adoEOF)
                m_pRecordset->MoveNext();
        }
        m_pRecordset->Close();
        wsprintf(ssss,"%d",noofrec);
        m_iTocRecords = noofrec+1;
    }
    catch(_com_error &e)
    {
        char ss[256];
        ErrorPtr pErr = NULL;

        if( (m_pConnection->Errors->Count) > 0)
        {
            long nCount = m_pConnection->Errors->Count;

            // Collection ranges from 0 to nCount -1.
            for(long i = 0; i < nCount; i++)
            {
                pErr = m_pConnection->Errors->GetItem(i);
                wsprintf(ss,"Error number: %x\t%s\n", pErr->Number,
                    (LPCSTR)pErr->Description);
                AfxMessageBox(ss);
            }
        }
        m_pRecordset->Close();
        return TRUE;
    }
    catch(...)
    {
        MessageBox("A problem faced while interacting with server","Administrator Module Error",MB_ICONSTOP|MB_OK);
        m_pRecordset->Close();
        return TRUE;
    }






This code is not complete nor is it simple but it will provide you all the elements that make up database intaraction through ADO in VC++



Regards
Ankur Verma





Similar Threads
Thread Thread Starter Forum Replies Last Post
Access is denied" load a simple xml file nana XML 12 May 12th, 2010 07:01 AM
Excel to Access using ADO JezLisle Excel VBA 2 July 22nd, 2007 05:55 PM
Settings to a simple access to a SQL database tedhill Classic ASP Databases 0 July 21st, 2005 04:25 AM
ado excel/access loane Excel VBA 5 August 17th, 2004 10:18 AM
ADO Recordset to Access MDB sasidhar79 SQL Server ASP 1 June 23rd, 2004 02:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.