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

📄 oledbcustomerdlg.cpp

📁 要运行程序必须将数据库文件students.mdb与可执行文件放在一起
💻 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_Control(pDX, IDC_BUTTON_PREV, m_BEFORE);
	DDX_Control(pDX, IDC_BUTTON_LAST, m_LAST);
	DDX_Control(pDX, IDC_BUTTON_NEXT, m_NEXT);
	DDX_Control(pDX, IDC_BUTTON_FIRST, m_FIRST);
	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))
		MessageBox("不能打开数据库", "数据库错误", MB_ICONSTOP | MB_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);

	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;
	m_FIRST.EnableWindow(false);
	m_BEFORE.EnableWindow(FALSE);
	m_NEXT.EnableWindow(true);
	m_LAST.EnableWindow(true);
	UpdateData(FALSE);
}


void COledbCustomerDlg::OnButtonPrev()					//到前一记录
{
	HRESULT hResult = m_Students.MoveNext(0,false);	
	if(hResult!=0)
	{
		//return;
		m_FIRST.EnableWindow(false);
		m_BEFORE.EnableWindow(FALSE);
		HRESULT hResult = m_Students.MoveNext();
		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);
	m_NEXT.EnableWindow(true);
	m_LAST.EnableWindow(true);
		
}


void COledbCustomerDlg::OnButtonNext()					//到下一记录
{
	HRESULT hResult = m_Students.MoveNext();
	if(hResult!=0)
	{
		//return;
		m_NEXT.EnableWindow(FALSE);
		m_LAST.EnableWindow(FALSE);
		HRESULT hResult = m_Students.MoveNext(0,false);
		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);
	m_FIRST.EnableWindow(true);
	m_BEFORE.EnableWindow(true);
}


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);
	m_FIRST.EnableWindow(true);
	m_BEFORE.EnableWindow(true);
	m_NEXT.EnableWindow(FALSE);
	m_LAST.EnableWindow(FALSE);
}

void COledbCustomerDlg::OnButtonAdd()					//添加记录
{
	UpdateData( TRUE );
	if(m_nAge<0 || m_nAge>120)
	{
		MessageBox("输入的年龄不符合实际", "输入错误",
			MB_ICONEXCLAMATION | MB_OK);
		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 ) )								//出错
	{
		MessageBox("不能插入当前记录", "数据库错误", MB_ICONSTOP | MB_OK);
		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);
	MessageBox("添加成功!", "提示", MB_ICONINFORMATION| MB_OK);
}

void COledbCustomerDlg::OnButtonDelete()				//删除记录
{
	if (MessageBox("确定要删除吗?","提示",MB_ICONQUESTION|MB_YESNO)==IDYES)
	{
	HRESULT hResult = m_Students.Delete();
	if( FAILED( hResult ) )								//出错
		MessageBox("不能删除该记录", "数据库错误", MB_ICONSTOP | MB_OK);
	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);	
		MessageBox("删除成功!", "提示", MB_ICONINFORMATION| MB_OK);
	}
	}
}

void COledbCustomerDlg::OnButtonModify()				//修改记录
{
	UpdateData( TRUE );
	if(m_nAge<0 || m_nAge>120)
	{
		MessageBox("输入的年龄不符合实际", "输入错误",
			MB_ICONEXCLAMATION | MB_OK);
		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 ) )
		MessageBox("不能更新数据库", "数据库错误", MB_ICONSTOP | MB_OK);
	else
	MessageBox("修改成功!", "提示", MB_ICONINFORMATION| MB_OK);

}

⌨️ 快捷键说明

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