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
// Operations.cpp : implementation file
//

#include "stdafx.h"
#include "ODBCExample.h"
#include "ODBCExampleDlg.h"
#include "Operations.h"
#include "SetOperations.h"

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

/////////////////////////////////////////////////////////////////////////////
// COperations dialog


COperations::COperations(CWnd* pParent /*=NULL*/)
	: CDialog(COperations::IDD, pParent)
{
	//{{AFX_DATA_INIT(COperations)
	m_strSQL = _T("");
	m_strSetOpen = _T("Sets currently open: 0");
	//}}AFX_DATA_INIT
}


void COperations::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COperations)
	DDX_Text(pDX, IDC_SQL, m_strSQL);
	DDX_Text(pDX, IDC_SETOPEN, m_strSetOpen);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COperations, CDialog)
	//{{AFX_MSG_MAP(COperations)
	ON_BN_CLICKED(IDC_BEGINTRANS, OnBegintrans)
	ON_BN_CLICKED(IDC_ENDTRANS, OnEndtrans)
	ON_BN_CLICKED(IDC_ROLLBACK, OnRollback)
	ON_BN_CLICKED(IDC_SENDSQL, OnSendsql)
	ON_EN_CHANGE(IDC_SQL, OnChangeSql)
	ON_BN_CLICKED(IDC_RECORDSET, OnRecordset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COperations message handlers

//Begins a transaction
void COperations::OnBegintrans() 
{
	//Try and begin a transaction:
	if(!db->StartTransaction())
	{
		//Show the last error message:
		CODBCExampleDlg::DisplayErrorBox(db);
		return;
	}

	//OK:
	GetDlgItem(IDC_BEGINTRANS)->EnableWindow(false);
	GetDlgItem(IDC_ENDTRANS)->EnableWindow(true);
	GetDlgItem(IDC_ROLLBACK)->EnableWindow(true);
	MessageBox("CODBCAccess::StartTransaction() was successful!",
		"Success!", MB_ICONINFORMATION);
}

void COperations::OnEndtrans() 
{
	//Try and end the transaction:
	if(!db->EndTransaction())
	{
		//Show the last error message:
		CODBCExampleDlg::DisplayErrorBox(db);
		return;
	}

	//OK:
	GetDlgItem(IDC_BEGINTRANS)->EnableWindow(true);
	GetDlgItem(IDC_ENDTRANS)->EnableWindow(false);
	GetDlgItem(IDC_ROLLBACK)->EnableWindow(false);
	MessageBox("CODBCAccess::EndTransaction() was successful!  The "
		"changes you made were saved to the database.",
		"Success!", MB_ICONINFORMATION);
}

void COperations::OnRollback() 
{
	//Try and rollback changes:
	if(!db->Rollback())
	{
		//Show the last error message:
		CODBCExampleDlg::DisplayErrorBox(db);
		return;
	}

	//OK:
	GetDlgItem(IDC_BEGINTRANS)->EnableWindow(true);
	GetDlgItem(IDC_ENDTRANS)->EnableWindow(false);
	GetDlgItem(IDC_ROLLBACK)->EnableWindow(false);
	MessageBox("CODBCAccess::Rollback() was successful!  The "
		"changes you made were discarded.",
		"Success!", MB_ICONINFORMATION);
}

void COperations::OnSendsql() 
{
	UpdateData(true);

	//Make sure there is an SQL statement:
	if(m_strSQL == _T(""))
	{
		MessageBox("Please enter an SQL statement.", "No SQL Statement",
			MB_ICONEXCLAMATION);
		GetDlgItem(IDC_SQL)->SetFocus();
		return;
	}

	//Try and send the SQL statement:
	if(!db->SendSQL(m_strSQL))
	{
		//Show the last error message:
		CODBCExampleDlg::DisplayErrorBox(db);
		return;
	}

	//Success
	MessageBox("CODBCAccess::SendSQL() was successful!",
		"Success!", MB_ICONINFORMATION);
}

void COperations::OnChangeSql() 
{
	//Is there text?
	UpdateData(true);
	GetDlgItem(IDC_SENDSQL)->EnableWindow(m_strSQL != _T(""));
}

BOOL COperations::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	OnChangeSql();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void COperations::OnOK() 
{
	//Are we in a transaction?
	if(db->IsInTrans())
	{
		if(MessageBox("You are currently in a transaction.  Continuing "
			"will rollback any changes and close the recordset.  Are you "
			"sure you want to continue?", "Are you sure you want to continue?",
			MB_ICONQUESTION | MB_YESNO) == IDNO)
		{
			return;
		}

		//Rollback changes:
		db->Rollback();
	}

	//How many open sets?
	if(atoi(m_strSetOpen.Mid(m_strSetOpen.Find(": ")+2)) > 0)
	{
		if(MessageBox("You currently have 1 or more sets open.  Continuing will close these "
			"sets.  Are you sure you want to continue?", "Are you sure you want to continue?",
			MB_ICONQUESTION | MB_YESNO) == IDNO)
		{
			return;
		}

		//When disconnect is called, the sets will be closed and memory freed
	}
	
	//Main dialog closes db:
	CDialog::OnOK();
}

//Display recordset options
void COperations::OnRecordset() 
{
	UpdateData(true);
	CSetOperations set;
	set.db = db;

	//Open and wait for end of dialog:
	set.DoModal();

	//Set the number of open sets now:
	m_strSetOpen.Format("Sets currently open: %d", db->GetNumSets());
	UpdateData(false);
}

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

Share

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 | Terms of Use | Mobile
Web03 | 2.8.1411022.1 | Last Updated 13 Mar 2003
Article Copyright 2001 by Alexander Wiseman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid