Connecting directly to a DB 
Author Message
 Connecting directly to a DB

How to connect to DB without using DSN and ODBC.

You specify the driver, server, port,database,user and password.

How do you do that in V6.0C++



Fri, 27 Aug 2004 18:10:09 GMT  
 Connecting directly to a DB
Hi Nicolai

You have to specify this in the connection string.

I send you hereby a code sample using ADO and OLEDB. I hope this will help
you

Serge

#import "C:\Program Files\Fichiers communs\System\ADO\msado15.dll" \
    no_namespace rename("EOF", "EndOfFile")

#include <OraOLEDB.h>
#include <iostream.h>
#include <stdio.h>

inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};

void main()
{
 if(FAILED(::CoInitialize(NULL)))
        return;

 _ConnectionPtr pConnection = NULL;
 _RecordsetPtr rec = NULL;

 _bstr_t RequeteSQL ("select * from status");

 try
 {
//  _bstr_t StrCnn("provider=OraOLEDB.Oracle;Data Source=CPAO;"
//   "User Id=CPAOP; Password=CPAOP");
  _bstr_t StrCnn("provider=msdaora;Data Source=CPAO;"
  "User Id=CPAOP; Password=CPAOP");

  TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
  TESTHR(pConnection->Open(StrCnn,"","", NULL));

 }
 catch(_com_error &e)
 {
  cout << e.ErrorMessage() << endl;
 }

 try
 {

  TESTHR(rec.CreateInstance(__uuidof(Recordset)));
  TESTHR( rec->Open(RequeteSQL, _variant_t((IDispatch*)pConnection, true),
   adOpenDynamic, adLockReadOnly, adCmdText));

  while(! rec->EndOfFile)
  {
   char NumPoste[100];
   sprintf(NumPoste, "%s", (LPCSTR) (_bstr_t)
rec->Fields->Item["NUM_POSTE"]->Value);
   cout << NumPoste << endl;
   rec->MoveNext();
  }
  rec->Close();
  rec = NULL;

 }
 catch(_com_error &e)
 {
  cout << e.ErrorMessage() << endl;
 }

    pConnection->Close();
 pConnection = NULL;

 ::CoUninitialize();

Quote:
}



Quote:
> How to connect to DB without using DSN and ODBC.

> You specify the driver, server, port,database,user and password.

> How do you do that in V6.0C++



Fri, 27 Aug 2004 22:19:22 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Trying to connect to Access DB over HTTP

2. Simple question about connecting to Oracle DB

3. Connecting to DB using ADO

4. HELP - ISAPI app unable to connect to DB

5. Connecting DB/2 with DAO or ADO?

6. connecting to a db

7. Problem connecting ODBC db acess to my program

8. Using OLE DB or ADO to connect Oracle and SQL Server

9. Question about using ADO to connect Oracle DB??Thank you

10. For VC++, ADO or OLEDB is better for connect remote Oracle 9i DB??Thank you

11. Question about using Oracle Client to connect Oracle 9i DB (window version)?Thank you

12. Connecting to our ODBC Database, once adding db suppor to an inital program

 

 
Powered by phpBB® Forum Software