Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Database » Add DAO support to your SDI Application

Add DAO support to your SDI Application

This article guides you to add CRecordview support to your MFC application. MFC AppWizard let you add ODBC support to your application by adding few simple extra steps.

Author Rank :
Page Views : 1376
Downloads : 15
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
add_dao_sup.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Creating Skeleton of Your Application

MFC AppWizard adds DAO support to your application by selecting option Database Support on AppWizard's page 2.

Select a project type MFC AppWizard(exe) and give your project name.

Select SDI Support.

Select Database view without file support option.

Click DAO radio button and select your access database. My database is "D:\mcb.krz" which is an access database. Here I have shown mcb.krz as mcb.mdb.

Select a table from your database.

Leave other AppWizard options as default and Click Finish. Build and Run the project. You should be able to run the project with no errors.

Under the Hood

Under the hood, AppWizard has added a class called CAddDaoSpSet. This class is derived from CDaoRecordset.

CAddDaoSpSet class

CAddDaoSpSet class is a CDaoRecordset derived class. Declaration header file looks like this:

class CAddDaoSpSet : public CDaoRecordset
{
public:
CAddDaoSpSet(CDaoDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(CAddDaoSpSet)
// Field/Param Data
//{{AFX_FIELD(CAddDaoSpSet, CDaoRecordset)
CString m_Template;
CString m_SearchName;
CString m_Search;
//}}AFX_FIELD
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAddDaoSpSet)
public:
virtual CString GetDefaultDBName(); // REVIEW: Get a comment here
virtual CString GetDefaultSQL(); // default SQL for Recordset
virtual void DoFieldExchange(CDaoFieldExchange* pFX); // RFX support
//}}AFX_VIRTUAL
// Implementation
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};

This class has a member variable corresponding to each column of the table. Besides this, it has three pure virtual functions with a default constructor, AssertValid, and Dump function. Here is implementation of this class:

// CAddDaoSpSet implementation
IMPLEMENT_DYNAMIC(CAddDaoSpSet, CDaoRecordset)
CAddDaoSpSet::CAddDaoSpSet(CDaoDatabase* pdb)
: CDaoRecordset(pdb)
{
//{{AFX_FIELD_INIT(CAddDaoSpSet)
m_Template = _T("");
m_SearchName = _T("");
m_Search = _T("");
m_nFields = 3;
//}}AFX_FIELD_INIT
m_nDefaultType = dbOpenDynaset;
}
CString CAddDaoSpSet::GetDefaultDBName()
{
return _T("D:\\mcb.krz");
}
CString CAddDaoSpSet::GetDefaultSQL()
{
return _T("[Hunt]");
}
void CAddDaoSpSet::DoFieldExchange(CDaoFieldExchange* pFX)
{
//{{AFX_FIELD_MAP(CAddDaoSpSet)
pFX->SetFieldType(CDaoFieldExchange::outputColumn);
DFX_Text(pFX, _T("[Template]"), m_Template);
DFX_Text(pFX, _T("[SearchName]"), m_SearchName);
DFX_Text(pFX, _T("[Search]"), m_Search);
//}}AFX_FIELD_MAP
}
// CAddDaoSpSet diagnostics
#ifdef _DEBUG
void CAddDaoSpSet::AssertValid() const
{
CDaoRecordset::AssertValid();
}
void CAddDaoSpSet::Dump(CDumpContext& dc) const
{
CDaoRecordset::Dump(dc);
}
#endif //_DEBUG
 

GetDefaultConnect returns the database name.

GetDefaultSQL returns the table name which you are connected to.

DoFieldExchange connects table fields to a member of the class. See CDaoRecordset class in MSDN for more details.

CAddDaoSpDoc Class Besides this, AppWizard has added a member variable in Doc class of application.

CAddDaoSpSet m_addDaoSpSet;

CAddViewSpView Class 

View class has more additions than the doc. One variable of CDaoRecordset* type has added to the view class of the project.

CAddDaoSpSet* m_pSet;

Which is being initialized in the constructor:

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

OnInitialUpdate is overridden with this code:

void CAddDaoSpView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_addDaoSpSet;
CDaoRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}

and a new function OnGetRecordset has been added to the class which returns pointer to CDaoRecorset.

CDaoRecordset* CAddDaoSpView::OnGetRecordset()
{
return m_pSet;
}

Resource Additions

AppWizard has added a dialog template, one menu item 'Record' with four submenus and four tool bar buttons. We will see all these in our sample project.

Customizing The Project

Database support has been added to our application. Now let's customize the application according to our needs.

Add Controls to The Dialog

This is what my application will look like. MFC AppWizard has added a dialog to your application. Go to Resources from ClassView and double click on newly added dialog. Add three edit fields, three static fields, a list box, and a button. Three exit boxes will show three fields of the table which we can move by using menu or toolbar options.

FillList button click will add one column's data of the table to the list box.

Add Members

Add member variables corresponding to all three edit boxes by using ClassWizard. Click ClassWizard from the menu and select Member Variable Tab.

Now Click Add Variable button and add three variables corresponding to IDC_EDIT1, IDC_EDIT2, and IDC-EDIT3. Instead of adding new names, select m_pSet members from the drop-down list.

After adding these data members, your ClassWizard would look like this:

Now write a command handler for FillList button by double clicking it and write this code:

CListBox* list = (CListBox*)GetDlgItem(IDC_LIST1) ;
list->ResetContent();
ASSERT ( ! m_pSet->IsEOF() ) ;
while ( ! m_pSet->IsEOF() )
{
list->AddString( m_pSet->m_SearchName );
m_pSet->MoveNext();
}
m_pSet->MoveFirst();
UpdateData(FALSE);

Build and Run the application. Here is how output looks like:

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.