Click here to Skip to main content
15,894,540 members
Articles / Desktop Programming / MFC

CODBCAccess: a CDatabase and CRecordset Wrapper

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
12 Mar 200312 min read 210.4K   4.3K   55  
A class to wrap the use of CDatabase and CRecordset into one object to communicate with databases
// SetOperations.cpp : implementation file
//

#include "stdafx.h"
#include "ODBCExample.h"
#include "ODBCExampleDlg.h"
#include "SetOperations.h"
#include "NamePrompt.h"
#include "ViewSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSetOperations dialog


CSetOperations::CSetOperations(CWnd* pParent /*=NULL*/)
	: CDialog(CSetOperations::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetOperations)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CSetOperations::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetOperations)
	DDX_Control(pDX, IDC_SETS, m_ctlSets);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetOperations, CDialog)
	//{{AFX_MSG_MAP(CSetOperations)
	ON_BN_CLICKED(IDC_OPENNEW, OnOpennew)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_VIEW, OnView)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetOperations message handlers

//Called to open a new set
void CSetOperations::OnOpennew() 
{
	//Prompt for a friendly name:
	CNamePrompt name;
	name.db = db;
	if(name.DoModal() != IDOK)
	{
		return;
	}

	//Open a new set:
	if(name.m_bUseSQL)
	{
		//Open with SQL:
		if(!db->OpenNewSet(name.m_strName, name.m_strSQL))
		{
			//Show the error:
			CODBCExampleDlg::DisplayErrorBox(db);
			return;
		}
	}
	else
	{
		//Open with table:
		if(!db->OpenNewSet(name.m_strName, "", name.sTable))
		{
			//Show the error:
			CODBCExampleDlg::DisplayErrorBox(db);
			return;
		}
	}

	//Add it to the list control
	m_ctlSets.InsertItem(m_ctlSets.GetItemCount(), name.m_strName);

	//Set successfully opened:
	MessageBox("CODBCAccess::OpenNewSet() succeeded!  A new set has been opened with friendly name "
		"of " + name.m_strName + ".", "Success!", MB_ICONINFORMATION);
}

//Called to close a set
void CSetOperations::OnClose() 
{
	UpdateData(true);
	
	//Make sure of valid selection:
	int sel = m_ctlSets.GetSelectionMark();
	if(sel < 0)
	{
		MessageBox("Please select a valid set name from the list.", "No Set Name Selected",
			MB_ICONEXCLAMATION);
		return;
	}

	//Close the set:
	if(!db->CloseSet(m_ctlSets.GetItemText(sel, 0)))
	{
		//Display error:
		CODBCExampleDlg::DisplayErrorBox(db);
		return;
	}

	//Remove from list control:
	m_ctlSets.DeleteItem(sel);

	//Success!
	MessageBox("CODBCAccess::CloseSet() was successful!  The set has been closed and memory "
		"has been freed.", "Success!", MB_ICONINFORMATION);
}

BOOL CSetOperations::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//Setup list control:
	CRect win;
	m_ctlSets.GetClientRect(win);
	m_ctlSets.InsertColumn(0, "Set Friendly Name", LVCFMT_LEFT, win.Width(), 0);
	m_ctlSets.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	//Get all the names of open sets:
	CStringArray names;
	db->GetNamesOfOpenSets(names);

	//Add them to the list control:
	for(int h = 0; h < names.GetSize(); h++)
	{
		//Add this one:
		m_ctlSets.InsertItem(h, names.ElementAt(h));
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSetOperations::OnView() 
{
	UpdateData(true);
	
	//Make sure of valid selection:
	int sel = m_ctlSets.GetSelectionMark();
	if(sel < 0)
	{
		MessageBox("Please select a valid set name from the list.", "No Set Name Selected",
			MB_ICONEXCLAMATION);
		return;
	}

	//Open a viewSet dialog:
	CViewSet vw;

	//Plug in the right set pointer:
	db->GetSet(m_ctlSets.GetItemText(sel, 0), vw.set);

	//Open it:
	vw.DoModal();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
I work with C++, C#, Perl, PHP, HTML (with a lot of CSS), and a little bit of ASP.NET. I have been programming for 7 years.

I also enjoy translating Latin, and especially reading authors whose primary language was Latin, including Virgil (or Vergil for the Latinate spelling), Julius Caesar, Catullus, Cicero, and some others (like Martial). I enjoy classical music, most especially Mozart and Bach.

Comments and Discussions