Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Oracle Query using C++

Status
Not open for further replies.

BobWaterhouse

Programmer
Dec 11, 2002
27
0
0
US
I am very new to both using C++ and Oracle. I was wondering if there was a simple demo script out there that would show me how to connect and query an Oracle Database within a C/C++ prorgam.
 
I should note that I am trying to query a table where I match Field 1 with a variable, then pull Field 3 back in to another variable.

Database: DB_NAME
Table: DB_TABLE_NAME

Name Type Null?
Field 1 NUMBER(10) NOT NULL
Field 2 VARCHAR2(60) NOT NULL
Field 3 NUMBER(10)
 
If you did not find the answer yet here is the standard procedure to access any DB from VC++ 6.0:
1. Create Data Source to your Oracle DB. You need to do it via Data Source in Control Panel
2. In StdAfx.h header file #include <afxdb.h> if you did not set your program with DB access
2. Add new class to your program for the table you want to access. View/Class Wizard/Add Class/Select Recordset or OleRecordSet/Select Data Source/Select table
3. Wizard creates header and .cpp files for you
4. Set up pointers to the Data Base and table:
CDatabase pdb; /*instance of DB class */
CPrice prc( &pdb ); /*instance of CPrice */
CPrice* pPRC = &prc; /*pointer to price instance*/
5. Open DB
try
{
int bRet = pdb.Open(NSTSource,FALSE,FALSE);
}

catch(CDBException* e)
{
AfxMessageBox("Unable to open NST data base, \n error:"
+ e->m_strError, MB_OK, 0);
}

6.Open your table with string to get your data:
CString szPriceString = "SELECT TOP 20 * FROM PriceHist ORDER BY Date DESC";
try
{
pPRC->Open(CRecordset::snapshot, szPriceString, CRecordset::none);
}

catch(CDBException* e)
{
CString szFunction("pPRC->Open");
CString szStock("All");;
HandleError(e, szFunction, szStock);
}
7. Do your navigation in the table with MoveFirst, MoveNext, MovePrev, etc.
Good luck, Pavel.
 
Thanks for the Input. Unfortunately I am not able to get to the Class Wizard, perhaps my version Microsoft C++ V6.0 is too old. The Copyright says 1994-1998. Do you think that's the issue?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top