⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oledbcustomerdlg.cpp

📁 Visual C++ 6.0 数据库开发技术与工程实践 chapter 8
💻 CPP
字号:
// OledbCustomerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OledbCustomer.h"
#include "OledbCustomerDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COledbCustomerDlg dialog

COledbCustomerDlg::COledbCustomerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COledbCustomerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COledbCustomerDlg)
	m_nAge = 0;
	m_strDepartment = _T("");
	m_nID = 0;
	m_strName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void COledbCustomerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COledbCustomerDlg)
	DDX_Text(pDX, IDC_EDIT_AGE, m_nAge);
	DDX_Text(pDX, IDC_EDIT_DEPARTMENT, m_strDepartment);
	DDX_Text(pDX, IDC_EDIT_ID, m_nID);
	DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COledbCustomerDlg, CDialog)
	//{{AFX_MSG_MAP(COledbCustomerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_FIRST, OnButtonFirst)
	ON_BN_CLICKED(IDC_BUTTON_PREV, OnButtonPrev)
	ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext)
	ON_BN_CLICKED(IDC_BUTTON_LAST, OnButtonLast)
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COledbCustomerDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	HRESULT result = m_Students.Open();
	if(FAILED(result))
		AfxMessageBox("open database failed!");

	m_Students.MoveFirst();
	m_nID = m_Students.m_id;
	m_strName = m_Students.m_name;
	m_nAge = m_Students.m_age;
	m_strDepartment = m_Students.m_department;
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void COledbCustomerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 COledbCustomerDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COledbCustomerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//到第一条记录
void COledbCustomerDlg::OnButtonFirst() 
{
	m_Students.MoveFirst();
	m_nID = m_Students.m_id;
	m_strName = m_Students.m_name;
	m_nAge = m_Students.m_age;
	m_strDepartment = m_Students.m_department;
	UpdateData(FALSE);

}

//到上一条记录
void COledbCustomerDlg::OnButtonPrev() 
{
	HRESULT hResult = m_Students.MoveNext(0,false);
	if( FAILED( hResult ) )
	{
		return;
	}
	m_nID = m_Students.m_id;
	m_strName = m_Students.m_name;
	m_nAge = m_Students.m_age;
	m_strDepartment = m_Students.m_department;
	UpdateData(FALSE);
}

//到下一条记录
void COledbCustomerDlg::OnButtonNext() 
{
	HRESULT hResult = m_Students.MoveNext();
	if( FAILED( hResult ) )
	{
		return;
	}
	m_nID = m_Students.m_id;
	m_strName = m_Students.m_name;
	m_nAge = m_Students.m_age;
	m_strDepartment = m_Students.m_department;
	UpdateData(FALSE);
}

//到最后一条记录
void COledbCustomerDlg::OnButtonLast() 
{
	m_Students.MoveLast();
	m_nID = m_Students.m_id;
	m_strName = m_Students.m_name;
	m_nAge = m_Students.m_age;
	m_strDepartment = m_Students.m_department;
	UpdateData(FALSE);
}

void COledbCustomerDlg::OnButtonAdd() 
{
	UpdateData( TRUE );
	if(m_nAge<0 || m_nAge>120)
	{
		AfxMessageBox("输入的年龄不符合实际地方");
		return;
	}
	//先移到记录集的末尾,并赋值
	m_Students.MoveLast();
	m_Students.m_id = m_nID;
	_tcscpy( m_Students.m_name, m_strName );
	m_Students.m_age = m_nAge;
	_tcscpy( m_Students.m_department, m_strDepartment );
	//插入新记录
	HRESULT hResult = m_Students.Insert();
	if( FAILED( hResult ) )
	{//出错
		AfxMessageBox( _T( "Error inserting the current record" ) );
		return;
	}
	//从新获得记录集
	m_Students.GetData( 0 );
	m_Students.MoveFirst();
	m_nID = m_Students.m_id;
	m_strName = m_Students.m_name;
	m_nAge = m_Students.m_age;
	m_strDepartment = m_Students.m_department;
	UpdateData(FALSE);

}

void COledbCustomerDlg::OnButtonDelete() 
{
	HRESULT hResult = m_Students.Delete();
	if( FAILED( hResult ) )
	{//出错
		AfxMessageBox( _T( "Unable to delete the record" ) );
	}
	else
	{	//移到下一条记录并取出数据
		hResult = m_Students.MoveNext();
		if( hResult != S_OK )
		{m_Students.MoveFirst();
		}
		m_nID = m_Students.m_id;
		m_strName = m_Students.m_name;
		m_nAge = m_Students.m_age;
		m_strDepartment = m_Students.m_department;
		UpdateData(FALSE);
	}
}

void COledbCustomerDlg::OnButtonModify() 
{
	UpdateData( TRUE );
	if(m_nAge<0 || m_nAge>120)
	{
		AfxMessageBox("输入的年龄不符合实际");
		return;
	}
	//更新变量
	m_Students.m_id = m_nID;
	_tcscpy( m_Students.m_name, m_strName );
	m_Students.m_age = m_nAge;
	_tcscpy( m_Students.m_department, m_strDepartment );

	//更新数据库
    HRESULT hResult = m_Students.SetData();
	if( FAILED( hResult ) )
	{
		AfxMessageBox( _T( "Unable to save the rowset" ) );
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -