Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database connectivity to visual c++

Status
Not open for further replies.

cheeky

Programmer
Mar 27, 2001
2
ZA
Can u tell me how to connect MS:visual c++ (version 6) fields to an access database using dao odbc and how to add, delete and modify records from a table.

an explanation in detail will be appreciated.
 
not exactly sure if this is what your looking for. What I think you need to do is set up a DB provider and consumer model. To first make the provider you need to open up a new database project and fill out the info for the driver to be created. Then you have the choice of buiding a consumer app through the wizards provided (MFC AppWizard.exe) or Insert->ATL object -> data access -> consumer. Either way a header will be generated for you. #include the header onto your .cpp and make an instance of the class (not the accessor class) and just do myclass.Open(). This should open a connection and then you should beable to do things like MoveNext and Insert and all that fancy stuff.
 
Insert->ATL object -> data access -> consumer is for using of ADO ATL, it is not DAO connection ...
 
This is a short sample using ADO.

#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include<atlbase.h>
#include<adoint.h>
using namespace std;
int main(int argc, char* argv[])
{
CoInitialize(0);
{
CComPtr<_Connection> pConn;
CComPtr<IUnknown> pUnk;
HRESULT hr=pConn.CoCreateInstance(L&quot;ADODB.Connection&quot;,0,CLSCTX_INPROC_SERVER);
if(FAILED(hr))
{
cout<<&quot;failed&quot;<<endl;
}
hr=pConn->Open(SysAllocString(L&quot;driver={Microsof Access Driver (*mdb)};database={Path:\...\YourDB.mdb};&quot;),
SysAllocString(L&quot;&quot;),SysAllocString(L&quot;&quot;),0);
if(FAILED(hr))
{
cout<<&quot;failed&quot;<<endl;
}
hr=pConn->Execute(SysAllocString(L&quot;create table SomeTable(a int,b char(1))&quot;),0,0,0);
if(FAILED(hr))
{
cout<<&quot;failed&quot;<<endl;
}
}
CoUninitialize();
return 0;
}
John Fill
1c.bmp


ivfmd@mail.md
 
I think you also need to import the ado driver in MS VC++, after the includes at the top of the implementation file put:

#import &quot;c:\program files\common files\system\ado\msado15.dll&quot; rename (&quot;EOF&quot;,&quot;adoEOF&quot;) no_namespace

If it doesn't work, check the path of msado15.dll and edit to suit you.

 
LOL jtm11, you shouldn't. Try it yourself and see. John Fill
1c.bmp


ivfmd@mail.md
 
All of you wrote example about ADO, but cheeky need help with DAO ...
 
is too few differences. MFC DAO and ADO wrappers are the same. And MFC ODBC wrapper also is the same. Maybe some small differences in the class names (one-two letters, no more) John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top