Google Search

Saturday, January 29, 2011

Data Access through ODBC 600459– Visual Programming Anna University lab manual download fourth semester


Data Access through ODBC 600459– Visual Programming Anna University lab manual download fourth semester

Aim:

            To write a VC++ program to access the Student database through MFC ODBC.

Logical Description:

The program isolates the database access code from user interface code so that we can see how to add ODBC database capability to any MFC application. Here ClassWizard to generate a CRecordset class, but won't be using the CRecordView class that AppWizard generates when you ask for a database view application.
This application is fairly simple. It displays the rows from the student database table in a scrolling view. The student table is part of the Student NameList database that's included with Visual C++.

Steps for building the Student application:

  1. Create the database stud.mdb using MS Access.

  1. Run the ODBC Data Source Administrator to install the Student Registration data source.
a.       Click the ODBC icon in the Windows Control Panel.
b.      Click the Drivers tab to see whether the Microsoft Access driver is available.
c.       Click the Add button (the Add button is on the User DSN tab),
d.      choose Microsoft Access Driver in the Add Data Source dialog box (select the Microsoft Access Driver in the Create New Data Source dialog box and click the Finish button),
e.       And fill in the ODBC Microsoft Access Setup dialog box.
3.      Examine the data members that ClassWizard generates.

a.       Click on the Member Variables tab for the newly generated CStudentSet class.
b.      ClassWizard should have generated data members based on student column names.
  1. Declare an embedded recordset object in StudentDoc.h.
a.       Add the following public data member in the CStudentDoc class declaration:
CStudentSet m_StudentSet;
  1. Edit the StudentDoc.cpp file.
Add the line
#include "StudentSet.h"
just before the line
#include "StudentDoc.h"
  1. Declare a recordset pointer in StudentView.h.
a.       Add the following private data member in the CStudentView class declaration:
CStudentSet* m_pSet;
  1. Edit the OnDraw and OnInitialUpdate functions in StudentView.cpp.
a.       Add the following boldface code:
void CStudentView::OnDraw(CDC* pDC)
{
    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
    int nLineHeight=tm.tmHeight+tm.tmExternalLeading;
    CPoint pText(0,0);
 
    int y = 0;
    CString str;
    if (m_pSet->IsBOF()) 
    { // detects empty recordset
        return;
    }
    m_pSet->MoveFirst();   // fails if recordset is empty
    while (!m_pSet->IsEOF()) 
   {
        str.Format("%ld", m_pSet->m_RegNo);
        pDC->TextOut(pText.x, pText.y, str);
        pDC->TextOut(pText.x+1000, pText.y, m_pSet->m_Name);
        m_pSet->MoveNext();
        pText.y -= nLineHeight;
    }
}
 
void CStudentView::OnInitialUpdate() 
{
    CScrollView::OnInitialUpdate();
    CSize sizeTotal(8000, 10500);
 
    SetScrollSizes(MM_HIENGLISH, sizeTotal);
 
    m_pSet = &GetDocument()->m_StudentSet;
    // Remember that documents/views are reused in SDI applications!
    if (m_pSet->IsOpen()) {
        m_pSet->Close();
    }
    m_pSet->Open();
} 
b.      Also in StudentView.cpp, add the line
#include "StudentSet.h"
just before the line
#include "StudentDoc.h"
  1. Edit the Student.cpp file.
Add the line
#include "StudentSet.h"
just before the line
#include "StudentDoc.h"

0 comments:

Post a Comment