Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » OLE DB » Downloads
 
Add your own
alternative version
Go to top

High Performance OLE DB library : Ease of ADO, power of OLE DB

, 2 Dec 2001
Introduction to OLE DB extended classes
// ODLTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ODLTest.h"
#include "ODLTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CODLTestDlg dialog

CODLTestDlg::CODLTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CODLTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CODLTestDlg)
	m_SalesManName = _T("");
	m_CustomerName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CODLTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CODLTestDlg)
	DDX_Control(pDX, IDC_LIST1, m_CustList);
	DDX_Text(pDX, IDC_EDIT1, m_SalesManName);
	DDV_MaxChars(pDX, m_SalesManName, 15);
	DDX_Text(pDX, IDC_EDIT2, m_CustomerName);
	DDV_MaxChars(pDX, m_CustomerName, 15);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CODLTestDlg, CDialog)
	//{{AFX_MSG_MAP(CODLTestDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_COMMAND(ID_DATABASE_CONNECT, OnDatabaseConnect)
	ON_COMMAND(ID_DATABASE_DISCONNECT, OnDatabaseDisconnect)
	ON_COMMAND(ID_DELETE_CUSTOMERS, OnDeleteCustomers)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODLTestDlg message handlers

BOOL CODLTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CODLTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//************************************************

HCURSOR CODLTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//************************************************

void CODLTestDlg::OnButton1() 
{
	UpdateData();

	if(m_CustomerName.IsEmpty())
	{
		AfxMessageBox("Enter customer name first.");
		return;
	}
	else
		m_CustList.AddString(m_CustomerName);

	m_CustomerName = "";
	UpdateData(FALSE);
}

//************************************************

void CODLTestDlg::OnOK() 
{
	UpdateData();

	if(!m_Cnn.IsOpen()) // Check if database is opened
	{
		AfxMessageBox("You are not connected to the database yet.");
		return;
	}

	if(m_SalesManName.IsEmpty())
	{
		AfxMessageBox("Salesman Name must be entered.");
		return;
	}

	int CustCount = m_CustList.GetCount();

	if(CustCount == 0)
	{
		AfxMessageBox("At least one customer must be entered.");
		return;
	}

	// Create recordset for parent table 
	CSypODLRecordBase<CAccessor<CSalesManAccessor> >  mRsParent; 

	// Create recordset for child table 
	CSypODLRecordBase<CAccessor<CCustomerAccessor> >  mRsChild; 
																
	try
	{
		long SID;

		mRsParent.Open("SalesMan", &m_Cnn, 1); // Open parent table recordset
		if(mRsParent.IsEOF()) // Check if there is any record
			SID = 1;
		else
		{
			mRsParent.MoveLast(); // Move to the last record
			SID = mRsParent.GetAccessor()->m_SalesManID + 1; // Create new SalesmanID
		}
		
		m_Cnn.StartTransaction(); // Start transaction

		mRsChild.Open("Customer", &m_Cnn, 1); // Open child table recordset

		mRsParent.GetAccessor()->ClearRecord(); // Field buffer must be cleared
		mRsParent.GetAccessor()->m_SalesManID = SID; // Add SID to field buffer
		lstrcpy(mRsParent.GetAccessor()->m_SalesManName, m_SalesManName); // Add salesman name

		mRsParent.Insert(); // Insert row in the table

		for(int i=0; i<CustCount; i++)
		{
			CString mCustName;
			m_CustList.GetText(i, mCustName);

			mRsChild.GetAccessor()->ClearRecord(); // Field buffer must be cleared

			mRsChild.GetAccessor()->m_CustomerID_status = DBSTATUS_S_IGNORE;
			
			lstrcpy(mRsChild.GetAccessor()->m_CustomerName, mCustName);
			
			mRsChild.GetAccessor()->m_Address_status = DBSTATUS_S_ISNULL; // Insert NULL

			mRsChild.GetAccessor()->m_SalesManID = SID;

			mRsChild.Insert(); // Insert row in the child table

		}

	
		m_Cnn.Commit(); // Commit transaction.
	}
	catch(CSypODLException e)
	{
		m_Cnn.Abort(); // Rollback transaction in case of error
		e.DisplayError();
	}

	AfxMessageBox("Record added successfully.");
}

//************************************************
// Connect to database
void CODLTestDlg::OnDatabaseConnect() 
{

	try
	{
		if(!m_Cnn.IsOpen()) // Check if database is opened
			m_Cnn.Open("TESTDB", "Admin", "");

		AfxMessageBox("Database connection successful");
	}
	catch(CSypODLException e)
	{
		e.DisplayError();
		AfxMessageBox("Solution : You have to create an ODBC DSN called 'TESTDB' to the \
		database that is provided with this application for test purpose.");
	}
}

//************************************************
// Disconnect from database
void CODLTestDlg::OnDatabaseDisconnect() 
{
	m_Cnn.Close();
}

//************************************************

void CODLTestDlg::OnDeleteCustomers() 
{
	if(!m_Cnn.IsOpen()) // Check if database is opened
	{
		AfxMessageBox("You are not connected to the database yet.");
		return;
	}

	int Result = ::MessageBox(NULL, "Delete all customers?", "SURE!", MB_YESNO | MB_ICONSTOP);
	if(Result == IDNO)
		return;

	try
	{
		m_Cnn.Execute("DELETE FROM Customer"); // Execute method is used for action queries.

		AfxMessageBox("All customers are deleted.");
	}
	catch(CSypODLException e)
	{
		e.DisplayError();
	}
	
}

//************************************************

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

Ghanshyam Naik

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 3 Dec 2001
Article Copyright 2001 by Ghanshyam Naik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid