Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » ODBC » Downloads
 
Add your own
alternative version

CODBCAccess: a CDatabase and CRecordset Wrapper

, 12 Mar 2003
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

About the Author

Alexander Wiseman

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 13 Mar 2003
Article Copyright 2001 by Alexander Wiseman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid