?
Solved

Using MS-ADO in VC++

Posted on 2001-08-27
7
Medium Priority
?
544 Views
Last Modified: 2012-08-14
Hello,
I have been trying to use ADO inside a C++ win32 static DLL but am having no luck.  Almost all the documentation says the same thing: type in this #import "..... msado15.dll" and then they just stop explaining, like something magical is supposed to happen after that.  I am putting this #import directive inside "stdafx.h".  I build the DLL successfully, but I was under the impression that some classes were supposed to be generated in my project file based on the ADO dll.  I am not sure when to use, for example, _Recordset or _RecordsetPtr.  I am looking at sample code from a MS KB article that does something like this:  (notice the dot on the first one and the pointer reference on the second one... and their sample code compiles and runs!)....

_RecordsetPtr rs;
rs.CreateInstance( __uuidof( Recordset ) );
rs->Open("select * from myTable", "myDSN", adOpenStatic,adLockBatchOptimistic,-1);

By the way, I am getting a linker error on the "open" call:
     rs->Open("select * from myTable", "myDSN", adOpenForwardOnly, adLockOptimistic, adCmdText);

I know I am just not doing something right.  Any help is greatly appreciated!!  Thank you.
0
Comment
Question by:piratepatrol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 

Expert Comment

by:Arvindtn
ID: 6429000
where did u give the #import statement...

if ur using ATL then

Open stdafx.h

goto the end of the file

Add #import "*.dll" using <namespace>
#endif // !defined(AFX_STDAFX_H__0EB39A16_93E7_11D5_ABA7_00508BBD58BA__INCLUDED)

then give a rebuild all

hope this works for u...

Arvind T.N
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6430160
Have you tried the following

In Class Wizard

Select the ActiveX Events tab

Add new Class from type library

Look for the msado15.dll

It should be in c:\Program Files\Common Files\System\Ado

This should bring up as list of the interfaces etc. supported by the dll.

Pick the ones you require.

then just include msado.h in your code where you need to use ado.

Vin.
 

 
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6430386
All of the imported classes will be in a file called msado15.tlh in your debug or release directory, and you can look there for an idea of what they're doing.  The classes will be in a namespace called ADODB unless you import with the no_namespace directive.  You typically can't use the classes directly.  You will have to use either a pointer to the class or a smart pointer (like _RecordsetPtr).  Then you need to call the CreateInstance member function of _RecordsetPtr to create the ActiveX control.  Using ActiveX controls is a whole subject unto itself, and you might want to look at a tutorial on that.  I hope this helps.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 49

Accepted Solution

by:
DanRollins earned 400 total points
ID: 6431443
ADO access is real easy in langiages such as VB and Javascript and real hard in C++.  One reaosn is all of the conversion to bstr and variants and so forth which is transparent in the other langiages.

Notice how I comment out the # import.  It is not needed after the first time (just the #include)

// #import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","EndOfFile")
#include "msado15.tlh"

_ConnectionPtr m_pDBConnection= NULL;
_RecordsetPtr  m_pRecSet= NULL;

BOOL OpenDatabase( LPCSTR szDatabase )
{    
    HRESULT hr;
    ::CoInitialize(NULL); // really put this elsewhere...
    hr= m_pDBConnection.CreateInstance(__uuidof(Connection));

    CString sConnect;  sConnect.Format(
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;"
        "Mode=Read|Write|Share Deny None;Persist Security Info=False",
        (LPCSTR)szDatabase
    );
    hr= m_pDBConnection->Open(
       _bstr_t( sConnect ),  _bstr_t(""),  _bstr_t(""),
       adModeUnknown
    );
    return TRUE;
}

BOOL DoQuery()
{
   HRESULT       hr;
   _bstr_t       bstrQuery( "SELECT * FROM Customers" );
   _variant_t    vNull( DISP_E_PARAMNOTFOUND, VT_ERROR );        // Null variant

    hr= m_pRecSet.CreateInstance(__uuidof(Recordset));
    m_pRecSet->PutRefActiveConnection( m_pDBConnection );
    hr= m_pRecSet->Open(_variant_t(bstrQuery), vNull,
        adOpenStatic,
        adLockReadOnly,
        adCmdText
    );
    int nNumberIssues= m_pRecSet->GetRecordCount(); // eyeball check
    return TRUE;
}


-- Dan
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6766959
There has been no activity in this question in quite some time, and it looks like it has been abandoned. As part of our ongoing mission to clean up the topic areas, a Moderator will finalize this question within the next seven (7) days. At that time, either I or one of the other Moderators will force/accept the comment of DanRollins.

DO NOT ACCEPT THIS COMMENT AS AN ANSWER. If you have further comments on this question or the recommendation, please leave them here.

piratepatrol,

Some of these questions have been open for some time, and records indicate you logged in as recently as a today. Please resolve them appropriately as soon as possible. Continued disregard of your open questions will result in the force/acceptance of a comment as an answer; other actions affecting your account may also be taken. I will revisit these questions in approximately seven (7) days.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20260974 (new)
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mssql&qid=20258569 (new)
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20174572
http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20168259
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20159537
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20232167

Thanks,

Netminder
Community Support Moderator
Experts Exchange
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6767116
hi piratepatrol,

Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6793086
Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 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