CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Guest

    database programing in c++

    how do u access data from a database(msaccess) in c++ (adding,deleteing,modifying).


  2. #2
    Join Date
    May 1999
    Posts
    42

    Re: database programing in c++

    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.


  3. #3
    Join Date
    May 1999
    Posts
    11

    Re: database programing in c++

    *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

  4. #4
    Join Date
    May 1999
    Posts
    7

    Re: database programing in c++

    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


  5. #5
    Join Date
    Apr 1999
    Posts
    11

    Re: database programing in c++

    Try using DB- Libarary.

    Mukthar

  6. #6
    Join Date
    May 1999
    Location
    Mid-West
    Posts
    50

    Re: database programing in c++

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured