srinivas
05-02-2001, 05:47 AM
Hi
Will anyone please tell me how to use the database connectivity to C++
by the db.h header file( Any other method )
thanking you in advance
srinivas
Luis Araujo
05-03-2001, 09:19 AM
srinivas,
One way to access database from C++ (VC++) is to use ADO (ActiveX Data
Objects). It's a library made up COM objects designed to provide easy access
to databases.
Here as sample code:
//in order to use ADO library, you must import the following dll file
//to get access to the right objects
//To use ADO, you must install MDAC (Microsoft Data Access Components)
//2.5 or later
//The path referred here applies to my machine installation
#import "C:\\Program Files\\Common Files\\System\\Ado\\msado15.dll" no_namespace,
rename("EOF", "EndOfFile")
#include <tchar.h>
#include <iostream>
#include <comdef.h> //needed for _bstr_t and _variant_t
using namespace std;
void main()
{
CoInitialize( NULL); //initialize COM library
cout << "Opening database connection" << endl;
TCHAR *szConnString = "MyConnString", //supply your database ConnString
*szUID = "MyUserId", // supply your user id
*szPWD = "MyPwd"; // supply your pwd
try
{
_ConnectionPtr pConn("ADODB.Connection.2.5"); //This object represents
a connection to your database
pConn->Open(_bstr_t(szConnString),
_bstr_t(szUID),
_bstr_t(szPWD),
adConnectUnspecified); //open a database connection
cout<< "Ok..." << endl;
//we are going to select id, name and hire date of our pre-created
//employee table
TCHAR *szQuery = "select func_id, func_name, hire_date from employee";
_RecordsetPtr pRs("ADODB.Recordset.2.5"); //object that represents a
//recordset
BOOL bRet = TRUE;
_variant_t varFuncId, //variable to hold func_id
varFuncName, //variable to hold func_name
varHireDate; //variable to hold hire_date
pRs->Open(_bstr_t(szQuery),
_variant_t((IDispatch *) pConn.GetInterfacePtr()),
adOpenStatic,
adLockReadOnly,
adCmdText); //execute the query
VariantInit(&varFuncId);
VariantInit(&varFuncName);
VariantInit(&varHireDate);
//let's iterate through the recordset while there are records to read
while (pRs->EndOfFile == VARIANT_FALSE)
{
//put each field value on the rigth variable
varFuncId = pRs->Fields->GetItem(0L)->Value;
varFuncName = pRs->Fields->GetItem(1L)->Value;
varHireDate = pRs->Fields->GetItem(2L)->Value;
//display data on the standard output
cout << "Func Id " << varFuncId.lVal << " Func Name: " << (char *)
_bstr_t(varFuncName.bstrVal) << "Hire Date: " << varHireDate.date << endl;
pRs->MoveNext(); //get next record
}
pRs->Close(); //close Recordset
pConn->Close(); // close database connection
} //try
catch (_com_error &e)
{
//if something got wrong, show error message
cout << (char *) e.ErrorMessage() << endl;
}
catch (...)
{
//handle any uncaught excpetion until here, regardless its type
}
CoUninitialize();
} // main
This code uses the #import directive to get access to the ADO objects
and methods. It also uses Microsoft specific data types such as _bstr_t (a
class that represent UNICODE strings) and _variant_t (a wrapper for a VARIANT
data type)
The ADO basic objects used in this example are: _Connection and _Recordset.
The _ConnectionPtr and _RecordSetPtr are smart pointers the makes ADO Programming
more easier, because you don't need to worry about object's life cycle manageament
(all these objects are COM objects, which need to be created and released
explicitly by the programmer. By using smart pointer wrappers, these tasks
become more easier.
This code opens a connection to a DataSource by using a ConnectionString.
You provide a connection string by supplying several parameters in a pre-defined
way,such as :
Provider=MSDAORA.1;Password=pwd;User ID=myuserid;Data Source=MyDataSource;
The "Provider=" points to the ADO Provider for Oracle, in this example.
The Data Source entry points to the Source of Data (e. g., an Oracle Instance
in this example).
All the code is wrapped by a try/catch block which is intended to handle
any error regarding to COM. This kind of error may be thrown when the code
is executed.
For more information about COM/ADO and database programming using VC++,
vist the MSDN home page at msdn.microsoft.com/library.
I hope this can help you.
Luis Araujo
"srinivas" <sreenevas@indya.com> wrote:
>
>Hi
> Will anyone please tell me how to use the database connectivity to
C++
>by the db.h header file( Any other method )
> thanking you in advance
> srinivas