Solved

ADO OLE MFC

Posted on 2002-04-25
6
767 Views
Last Modified: 2013-11-25
I have been using MFC CRecordset CDatabase for some time. With VB I use ADO.
Well, I am starting a small new project and want to use different database access methods under VC++, such as ADO. But after looking through this site, does not look like many people use this.

?If I want to enhance my skill set, should I look into ADO or OLE under VC++ 6.0.
?If so, which one, and links to some tutorials.
?What about OLE DB consumer templates, what the hell is this?
?Are there wizards in VC++ 6.0 that help with integerating ADO such as with CRecordset class or is it all straight coding.

Back-end db is SQL server 7.0




0
Comment
Question by:Paullkha
  • 3
  • 2
6 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 6970063
>>?If I want to enhance my skill set, should I look into ADO or OLE under VC++ 6.0.
Probably OLEDB since it is the coming wave.  But you will find more examples in MDSN and elsewhere for ADO.  Both are infinitely harder to use than CRecordset objects.

>>?Are there wizards in VC++ 6.0 that help with
>> integerating ADO such as with CRecordset class or is it
>> all straight coding.
All straight coding, and not much of it is easy.  If you use CRecordset, the tne ClassWizard makes named variables for each field of each table.  Change the table and the ClassWizard can update your object with a few clicks.

If you use ADO or OLEDB, you will be digging around all through your code to locate the changed spelling of the table/field name.  And the error won't occur unti runtime (then the data provider says "I've never heard of that field" and rejects the whole query.)

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6970069
Incidently, here is the best code sample for ADO access that I've ever seen.  It is PERFECT because it does the bare minimum:

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

#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
                                  if (FAILED(_hr)) _com_issue_error(_hr); }

#define RsITEM(rs,x) rs->Fields->Item[_variant_t(x)]->Value
#define UC (char *)
struct InitOle {
    InitOle()  { ::CoInitialize(NULL); }
    ~InitOle() { ::CoUninitialize();   }
} _init_InitOle_;       // Global Instance to force load/unload of OLE

void main(){
    _RecordsetPtr   spRS;
    _ConnectionPtr  spCON;
    try{
        CREATEiNSTANCE(spCON,Connection);
        spCON->ConnectionString = L"driver={sql server};SERVER=(local);Database=pubs;UID=sa; PWD=;";
        // spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};DBQ=authors.MDB;DefaultDir=C:\\test;";
             
        spCON->Open( "", "", "", -1 );
        CREATEiNSTANCE(spRS,Recordset)
        spRS->PutRefActiveConnection( spCON );
        spRS->Open("select au_lname, au_fname from authors",
               vtMissing, adOpenKeyset,
            adLockBatchOptimistic, -1
          );
        while(spRS->adoEOF == false){
            printf("au_lname = %s  au_fname = %s \n",
                    UC _bstr_t(RsITEM(spRS,0L)),
                    UC _bstr_t(RsITEM(spRS,"au_fname"))
               );
            spRS->MoveNext();
        }
        spRS->Close();
        spCON->Close();
    }
    catch( _com_error &e){
        _bstr_t bstrSource(e.Source());
        _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ")
            + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ")
            + _bstr_t(e.Description());
       
        MessageBox(0,bs,bstrSource, MB_OK);
    }          
}

-- Dan
0
 
LVL 2

Author Comment

by:Paullkha
ID: 6978094

Looks like no-one else disagrees.

When should one use ADO over the CRecordset object, since my reservations about ADO seem validated? I have seen it used in some examples on the web, but are there any compelling reasons to use it?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
ID: 6979211
ADO vs CRecordset:

With ADO and OLEDB, you use table/field names directly --  so your code looks about the same whether you are programming in VB, JavaScript, or C++ (of course C++ has all of that ugly COM overhead, as I've said).  And you can write very flexible SQL commands.  The data comes and goes as VARIENT data type which is very convenient for VB and JS, but awkward in C++.

With CRecordset, you let the framework transfer data between member variables and the database.  A VARCHAR data type comes in as an easy-to-use CString, etc.  The drawback is that there is a lot of 'inflexible' binding going on.  For instance, even if all you need is three columns, you will probably use a CRecordset that populates all member fields.  That means that there is a lot of data transfer going on that you don't need.

-- Dan
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6979377
This question is current, others are not and need your attention.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still inactive in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20292692.html
http://www.experts-exchange.com/questions/Q.20255048.html
http://www.experts-exchange.com/questions/Q.20284498.html
http://www.experts-exchange.com/questions/Q.20284957.html
http://www.experts-exchange.com/questions/Q.20293564.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20089266.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6986285
Thank you for returning and finalizing this.
Moondancer - EE Moderator
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now