Solved

C++ and database connectivity

Posted on 2001-08-16
15
235 Views
Last Modified: 2010-04-02
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
Comment
Question by:rram_v
  • 3
  • 3
  • 2
  • +6
15 Comments
 
LVL 5

Expert Comment

by:ekc
Comment Utility
What compiler, platform are you using?
0
 
LVL 32

Accepted Solution

by:
jhance earned 75 total points
Comment Utility
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
 

Author Comment

by:rram_v
Comment Utility
I am using unix and oracle...
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Well, in that case, you'll need to get the Oracle programming library.  Hopefully your company bought it along with the database.
0
 

Author Comment

by:rram_v
Comment Utility
I am using unix and oracle...
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
>>I am using unix and oracle...

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

Don't hit the REFRESH BUTTON on your browser.
0
 

Author Comment

by:rram_v
Comment Utility
I am using unix and oracle...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
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
 
LVL 4

Expert Comment

by:prashant_n_mhatre
Comment Utility
You may separate out the 'connect' method.
0
 
LVL 1

Expert Comment

by:Aggarwal
Comment Utility
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
 

Expert Comment

by:romilson
Comment Utility
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
 

Expert Comment

by:romilson
Comment Utility
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
 
LVL 1

Expert Comment

by:andymurd
Comment Utility
ODBC is available for UNIX, check out www.unixodbc.org

You can also write OCI code directly, without the Pro*C preprocessor step.
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now