can anyone tell me if it is possible to connect to a database (for example an access database) from visual c ++ 6.0? And if one can how does one do it? Is it some wholy complicated thing or is it as easy as doing it in visual basic 6.0 or java?

yes it can be done, and yes it is more complex then doing it from basic or java.

There are several free c++ classes that make database accessing easier -- see these google links

There are other ways to do it too, but ODBC is the most versatile because your program doesn't have to know anything about which database it is as long as it knows the connection string.

STUDENT DATABASE

AIM: To create a Visual C++ program for Student database application.

ALGORITHM:

STEP 1: Create the database Student.mdb with the table StudTab.

STEP 2: Then set the ODBC driver for this database. Click on the user tab and select the user data source MS Access Database.

STEP 3: Then click Add button and then select the Microsoft Access Driver(*.mdb). Then click Finish button.

STEP 4: Then give Data Source Name as ‘StudentDatabase’ and click the select button.And select the database Student.mdb.Then click the OK button.

STEP 5: Start VC++. Select the option MFC AppWizard (exe). Then give name to the project as DBDemo. Then select the ‘Single Document’ option and click the Next button.

STEP 6: Now select the option ‘Database view with file support’ and click the button ‘Data Source’.

STEP 7: Click the ODBC radio button and then select the database StudentDatabase. Ensure that the radio button for ‘Dynaset’ is clicked. And click OK and Finish Buttons.

STEP 8: Then place the controls in the form.

Control Object ID   Properties
Static text IDC_STATIC  caption=”Roll No”
Static text IDC_STATIC  caption=”Name”
Static text IDC_STATIC  caption=”Marks”
Edit box    IDC_EDIT1   
Edit box    IDC_EDIT2   
Edit box    IDC_EDIT3   
Button  IDC_ADD caption=”Add”
Button  IDC_DELETE  caption=”Delete”
Button  IDC_UPDATE  caption=”Update”
Static text IDC_BUTTON1 caption=”SQL Query INSERT”

STEP 9: Invoke ClassWizard. Click on the member variable tab and for each Edit Box set the variable .

STEP 10: For all the buttons add the BN_CLICKED message for CDBDemoView class. Open the DBDemoView.cpp file and edit the code for these message handlers.

STEP 11: We will use a function named ExecuteSQL in the file ‘DBDemoView.cpp’.This function will take a string of query as a parameter. Hence onSelect() message handler we could write as in program.

STEP 12: Build the application .

// DBDemoView.cpp : 

#include "stdafx.h"
#include "DBDemo.h"

#include "DBDemoSet.h"
#include "DBDemoDoc.h"
#include "DBDemoView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CDBDemoView

IMPLEMENT_DYNCREATE(CDBDemoView, CRecordView)

BEGIN_MESSAGE_MAP(CDBDemoView, CRecordView)
    //{{AFX_MSG_MAP(CDBDemoView)
    ON_BN_CLICKED(IDC_ADD, OnAdd)
    ON_BN_CLICKED(IDC_DELETE, OnDelete)
    ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
    ON_BN_CLICKED(IDC_SELECT, OnSelect)
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

// CDBDemoView construction/destruction

CDBDemoView::CDBDemoView() : CRecordView(CDBDemoView::IDD)
{
    //{{AFX_DATA_INIT(CDBDemoView)
    m_pSet = NULL;
    //}}AFX_DATA_INIT
    // TODO: add construction code here

}

CDBDemoView::~CDBDemoView()
{
}

void CDBDemoView::DoDataExchange(CDataExchange* pDX)
{
    CRecordView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDBDemoView)
    DDX_FieldText(pDX, IDC_EDIT1, m_pSet->m_RollNo, m_pSet);
    DDX_FieldText(pDX, IDC_EDIT2, m_pSet->m_Name, m_pSet);
    DDX_FieldText(pDX, IDC_EDIT3, m_pSet->m_Marks, m_pSet);
    //}}AFX_DATA_MAP
}

BOOL CDBDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    return CRecordView::PreCreateWindow(cs);
}

void CDBDemoView::OnInitialUpdate()
{
    m_pSet = &GetDocument()->m_dBDemoSet;
    CRecordView::OnInitialUpdate();
    GetParentFrame()->RecalcLayout();
    ResizeParentToFit();

}

// CDBDemoView printing

BOOL CDBDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // default preparation
    return DoPreparePrinting(pInfo);
}

void CDBDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add extra initialization before printing
}


void CDBDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add cleanup after printing
}

// CDBDemoView diagnostics

#ifdef _DEBUG

void CDBDemoView::AssertValid() const
{
    CRecordView::AssertValid();
}

void CDBDemoView::Dump(CDumpContext& dc) const
{
    CRecordView::Dump(dc);
}

CDBDemoDoc* CDBDemoView::GetDocument() // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDBDemoDoc)));
    return (CDBDemoDoc*)m_pDocument;
}
#endif //_DEBUG

// CDBDemoView database support
CRecordset* CDBDemoView::OnGetRecordset()
{
    return m_pSet;
}

// CDBDemoView message handlers

void CDBDemoView::OnAdd() 
{
    // TODO: Add your control notification handler code here

    m_pSet->AddNew();
    UpdateData(TRUE);
    m_pSet->Update();
    if(!m_pSet->IsBOF())
        m_pSet->MoveLast();
    m_pSet->Requery();
    UpdateData(FALSE);

}

void CDBDemoView::OnDelete() 
{
    // TODO: Add your control notification handler code here

    CRecordsetStatus flag;
    m_pSet->Delete();
    m_pSet->GetStatus(flag);
    if(flag.m_lCurrentRecord==0)
        m_pSet->MoveLast();
    UpdateData(FALSE);

}


void CDBDemoView::OnUpdate() 
{
    // TODO: Add your control notification handler code here

    m_pSet->Edit();
    UpdateData(TRUE);
    m_pSet->Update();

}

void CDBDemoView::OnSelect() 
{
    // TODO: Add your control notification handler code here

    try
    {
        m_pSet->m_pDatabase->BeginTrans();
        m_pSet->m_pDatabase->ExecuteSQL("INSERT INTO StudTab VALUES(13,'siva',95)");
        if(m_pSet->m_pDatabase->CommitTrans())
            TRACE("Transaction Done");
        else
            TRACE("Error!");
    }
    catch(CDBException *pEX)
    {
        pEX->ReportError();
        m_pSet->m_pDatabase->Rollback();
    }

}

OUTPUT:

Edited 3 Years Ago by Nick Evan: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.