Solved

C++ and database connectivity

Posted on 2001-08-16
15
236 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
ID: 6394744
What compiler, platform are you using?
0
 
LVL 32

Accepted Solution

by:
jhance earned 75 total points
ID: 6394749
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
ID: 6394823
I am using unix and oracle...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6394844
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
ID: 6394850
I am using unix and oracle...
0
 
LVL 32

Expert Comment

by:jhance
ID: 6394857
>>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
ID: 6394942
I am using unix and oracle...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:prashant_n_mhatre
ID: 6395050
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
ID: 6395054
You may separate out the 'connect' method.
0
 
LVL 1

Expert Comment

by:Aggarwal
ID: 6395184
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
ID: 6396964
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
ID: 6396986
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
ID: 6516941
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
ID: 6858025
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
ID: 6883640
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 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

12 Experts available now in Live!

Get 1:1 Help Now