Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++ and database connectivity

Posted on 2001-08-16
15
238 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

808 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