• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

C++ and database connectivity

How can i connect to a database like oracle or sql server from c++ code.... can anyone post me a sample code for achieving this....
0
rram_v
Asked:
rram_v
  • 3
  • 3
  • 2
  • +6
1 Solution
 
ekcCommented:
What compiler, platform are you using?
0
 
jhanceCommented:
My suggestion (assuming you are programming on a Windows platform with Visual Studio) is to use MFC and the CDaoDatabase classes.  All decent database engines (including Oracle and SQL s\Server) have DAO client support.
0
 
rram_vAuthor Commented:
I am using unix and oracle...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jhanceCommented:
Well, in that case, you'll need to get the Oracle programming library.  Hopefully your company bought it along with the database.
0
 
rram_vAuthor Commented:
I am using unix and oracle...
0
 
jhanceCommented:
>>I am using unix and oracle...

Yeh, I know.  You said that once already...

Don't hit the REFRESH BUTTON on your browser.
0
 
rram_vAuthor Commented:
I am using unix and oracle...
0
 
prashant_n_mhatreCommented:
This is what I use. I've removed unnecessary part.


connectStr is a string containing username and password.

like:

john@mypassword.DATABASE

here: john is user name
passwd: mypassword
DATABASE is a database connect string.

void DataBaseAccess::connect (const std::string& connectStr) {

    EXEC SQL BEGIN DECLARE SECTION;
        const Char* oraConnectStr = connectStr.c_str ();
    EXEC SQL END DECLARE SECTION;

    // Register behavior.
    EXEC SQL WHENEVER SQLERROR DO throwDBError (CONNECT_ERR);
    EXEC SQL WHENEVER NOTFOUND CONTINUE;

    // Attempt to connect.
    EXEC SQL CONNECT :oraConnectStr IDENTIFIED BY :"";
}
0
 
prashant_n_mhatreCommented:
You may separate out the 'connect' method.
0
 
AggarwalCommented:
Unix and oracle --

In case most efficient behaviour is needed -- use OCI

in case u want it to work on windows ( and that's for both oracle and SQL Server ) ...using ODBC Calls ( SQLCOnnect(), SQLPrepareStatement() , SQLExecute() ...) etc. is the nice idea !!


Now comes connecting to Oracle/SQLserver from unix.
1. u can use OCI for oracle and native apis for SQL Server ..and may be link dynamically dpanding on the db type.

OR use the ODBC layer ...if odbc libs for your unix flavor are available !!

CHeers
0
 
romilsonCommented:
Hi ,
I have some programs wrotes in unix c++ to connect a oracle server. you must to access by Oracle Pro *C.
The Oracle Pro *C ( oracle precompiler is a powerfull way to access oracle server ).

You write a program.pc
so , you compile it with the Pro *C

proc program.pc (..arguments..)  (note: is pc)

now , the oracle Pro *C transforms your sql code to OCI (Oracle Call Interface) ...

and you use the g++ or gcc compiler..

gcc program.cpp (note: is cpp)


You will find the complete informations of Pro *C
int the Oracle Tech Net.You must do the subscription
on http://otn.oracle.com/index.html

later , you can access the Pro *C documentation on

http://technet.oracle.com/doc/products.htm

To documentation of Oracle 8i Pro *C , go to
http://technet.oracle.com/doc/server.805/a58233/toc.htm


Good Luck !

Romilson Cruz de Carvalho


0
 
romilsonCommented:
If you want , send me a mail ,
that I send to you some examples..

Ah. . The Oracle Pro *C is avaiable on the Oracle instalation program. And  this have some samples of Pro *C use.

The Pro*C can be used as on Ruindows as on Unix , since your normal c/c++ code is protable.


Good luck

Romilson
0
 
andymurdCommented:
ODBC is available for UNIX, check out www.unixodbc.org

You can also write OCI code directly, without the Pro*C preprocessor step.
0
 
griesshCommented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jhance" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
NetminderCommented:
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now