Click to See Complete Forum and Search --> : database programing in c++


May 11th, 1999, 11:55 PM
how do u access data from a database(msaccess) in c++ (adding,deleteing,modifying).

Bob Clarke
May 12th, 1999, 03:25 AM
Here's a simple example, using the MFC DAO classes. (The formatting sometimes gets messed up when posting to this board, so keep that in mind...)

AfxDaoInit();

// in the header file, d_db is defined as
// CDaoDatabase d_db;

// file found, let's try to open it (cTemp contains the database name)
// such as "D:\DATA\TEST.MDB"
try
{
d_db.Open( Temp.GetBuffer(3));
}
catch(CDaoException* e)
{
::MessageBox( NULL,
"Cannot open database. Does someone else have it open exclusively?",
"Open Failed", MB_OK | MB_ICONSTOP);
e->Delete();
return;
}

// get the list of contract months.
// in the following loop, m_aMonthNames is a vector<CString>

CDaoTableDef tdContractMonths(&d_db);
CDaoRecordset rsContractMonths(&d_db);

try
{
tdContractMonths.Open("ContractMonths" );
rsContractMonths.Open( &tdContractMonths );
rsContractMonths.SetCurrentIndex( "PrimaryKey");

if( !rsContractMonths.IsBOF() && !rsContractMonths.IsEOF())
{
while( !rsContractMonths.IsEOF() )
{
COleVariant olevar;
rsContractMonths.GetFieldValue( "MonthName"), olevar );
CString cTemp = olevar.pbVal;
m_aMonthNames.push_back(cTemp);

rsContractMonths.MoveNext();
}
}


rsContractMonths.Close();
tdContractMonths.Close();
}
catch(CDaoException* e)
{
::MessageBox( NULL,
"Error opening ContractMonths table.",
"Open Failed", MB_OK | MB_ICONSTOP);
e->Delete();
}
catch( CMemoryException* e )
{
::MessageBox( NULL,
"Memory error while opening ContractMonths table.",
"Memory Problem", MB_OK | MB_ICONSTOP);
e->Delete();
}


*********************

To add records, do something like this:

COleVariant varSomeShortValue( (short) 23 );
rsContractMonths.AddNew();
rsContractMonths.SetFieldValue( "ShortValue", varSomeShortValue );
rsContractMonths.Update();


To edit records, use similar code, except call rsContractMonths.Edit() instead of rsContractMonths.AddNew();

To delete a record, use the Delete() method of the CDaoRecordset class.

AndrewZhang
May 12th, 1999, 04:01 AM
*ppDatabase = new CDaoDatabase;


// now open the database object with error checking
try
{
(*ppDatabase)->Open(fileName);
}
catch (CDaoException *e)
{
// special case--couldn't find the file, so it may be because
// user specified a new file to open
if (e->m_pErrorInfo->m_lErrorCode == 3024)
{
if (bReportNoOpen)
{
// create a message to display
CString message = _T("Couldn't open database--Exception: ");
message += e->m_pErrorInfo->m_strDescription;

// output status
AfxMessageBox(message);
}

// indicate failure but not fatal
nReturnCode = 0;
}
else // other type of DAO exception--always report
{
// create a message to display
CString message = _T("Couldn't open database--Exception: ");
message += e->m_pErrorInfo->m_strDescription;

// output status
AfxMessageBox(message);

// indicate fatal error
nReturnCode = -1;
}

// not rethrowing, so delete exception
e->Delete();

delete *ppDatabase;
*ppDatabase = NULL;
}
catch (CMemoryException *e)
{
// output status
AfxMessageBox(_T("Failed to open database--Memory exception thrown."));

// not rethrowing, so delete exception
e->Delete();

delete *ppDatabase;
*ppDatabase = NULL;

// indicate fatal error
nReturnCode = -1;
}


andrew

ashokayengar
May 12th, 1999, 04:38 AM
what u have sent is vc++ coding using CDaoRecordset which i already know it want to know how u achieve it using pure c++ without using CDaoRecordset

Sheik Mukthar
May 12th, 1999, 06:36 AM
Try using DB- Libarary.

Mukthar

Matt Ellison
May 12th, 1999, 03:20 PM
If you want to do it very easily and get a good handle on using a DB in MFC. then do the following first register your DB in the ODBC (32 Bit) under control panel...it's kinda easy but if you have a problem then write back. then when you open your new project select MFC .exe app. Single Doc Interface
next screen you select DB with file support and select the DB after that you simply accept the general defaults...once inside your app you will have all the general
DB support. you will also have an obect within your view name m_pSet which is a pointer to you DB. it has almost everything you need
for your DB control. if you wish to use a dialog or anything just within the (Add Member variable) you can acutally pick the fields to associate
to your DB...need anymore help just write back hope it helps