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

📄 oledbdepartmentmfcview.cpp

📁 vc++6.0数据库编程大全一书得各个章节得源码,比较详细.可以仔细参照学习!
💻 CPP
字号:
// OLEDBDepartmentMFCView.cpp : implementation of the COLEDBDepartmentMFCView class
//

#include "stdafx.h"
#include "OLEDBDepartmentMFC.h"

#include "OLEDBDepartmentMFCSet.h"
#include "OLEDBDepartmentMFCDoc.h"
#include "OLEDBDepartmentMFCView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//Added by Chuck Wood for query dialog support
#include "OpenDepartment.h"	
/////////////////////////////////////////////////////////////////////////////
// COLEDBDepartmentMFCView

IMPLEMENT_DYNCREATE(COLEDBDepartmentMFCView, COleDBRecordView)

BEGIN_MESSAGE_MAP(COLEDBDepartmentMFCView, COleDBRecordView)
	//{{AFX_MSG_MAP(COLEDBDepartmentMFCView)
	ON_WM_DESTROY()
	ON_COMMAND(ID_EDIT_DELETETCTRLDEL, OnEditDeletetctrldel)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COLEDBDepartmentMFCView construction/destruction

COLEDBDepartmentMFCView::COLEDBDepartmentMFCView()
	: COleDBRecordView(COLEDBDepartmentMFCView::IDD)
{
	//{{AFX_DATA_INIT(COLEDBDepartmentMFCView)
		// NOTE: the ClassWizard will add member initialization here
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	m_bAddingRecord = FALSE;
}

COLEDBDepartmentMFCView::~COLEDBDepartmentMFCView()
{
}

void COLEDBDepartmentMFCView::DoDataExchange(CDataExchange* pDX)
{
	COleDBRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COLEDBDepartmentMFCView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, 5);
	DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
	DDX_Text(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, 51);
	DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 51);
}

BOOL COLEDBDepartmentMFCView::PreCreateWindow(CREATESTRUCT& cs)
{
	return COleDBRecordView::PreCreateWindow(cs);
}

void COLEDBDepartmentMFCView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_oLEDBDepartmentMFCSet;
	{
		CWaitCursor wait;
		HRESULT hr = m_pSet->Open();
		if (hr != S_OK)
		{
			AfxMessageBox(_T("Record set failed to open."), MB_OK);
			// Disable the Next and Previous record commands,
			// since attempting to change the current record without an
			// open RecordSet will cause a crash.
			m_bOnFirstRecord = TRUE;
			m_bOnLastRecord = TRUE;
		}				
	}
	COleDBRecordView::OnInitialUpdate();

}

/////////////////////////////////////////////////////////////////////////////
// COLEDBDepartmentMFCView diagnostics

#ifdef _DEBUG
void COLEDBDepartmentMFCView::AssertValid() const
{
	COleDBRecordView::AssertValid();
}

void COLEDBDepartmentMFCView::Dump(CDumpContext& dc) const
{
	COleDBRecordView::Dump(dc);
}

COLEDBDepartmentMFCDoc* COLEDBDepartmentMFCView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COLEDBDepartmentMFCDoc)));
	return (COLEDBDepartmentMFCDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// COLEDBDepartmentMFCView database support
CRowset* COLEDBDepartmentMFCView::OnGetRowset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// COLEDBDepartmentMFCView message handlers

BOOL COLEDBDepartmentMFCView::OnMove(UINT nIDMoveCommand) 
{
	SaveRecord();
	return COleDBRecordView::OnMove(nIDMoveCommand);
}

void COLEDBDepartmentMFCView::OnDestroy() 
{
	SaveRecord();
	COleDBRecordView::OnDestroy();
}

void COLEDBDepartmentMFCView::OnEditDeletetctrldel() 
{
	if (AfxMessageBox(	//Be sure to verify your deletes
			"Are you sure you want to delete?",
			MB_YESNO)
		!= IDYES) {
		return;
	}
	//Delete record and test
	if (m_bAddingRecord) {
		//Just abort the add
		m_bAddingRecord = FALSE;
	}
	else if (FAILED(m_pSet->Delete())) {
		AfxMessageBox("Delete Failed:\n", MB_ICONEXCLAMATION);
	}
	ResetRowSet();
}

void COLEDBDepartmentMFCView::OnFileNew() 
{
	SaveRecord();
	AddRecord();
}

void COLEDBDepartmentMFCView::OnFileOpen() 
{
	COpenDepartment od;	//Query dialog box declaration

	SaveRecord();		//Save any changes first
	if (od.DoModal() == IDOK) {		//OK was clicked?
		CString newFilter;	//Define a new filter
		if (od.m_strDepartment > "") {	//Anything entered?
			//Fill the new filter with a WHERE clause
			newFilter =
				" WHERE Department.DepartmentCode = '" +
				od.m_strDepartment +"'";
		}
		else {
			//Empty out the filter if nothing entered
			newFilter = "";
		}
		if (m_pSet->m_strFilter.CompareNoCase(newFilter)) {
			//Strings are different
			m_pSet->m_strFilter = newFilter;
			//Close and open to requery
			m_pSet->Close();
			m_pSet->Open();
			ResetRowSet();	//Display results
		}
	}
}

void COLEDBDepartmentMFCView::AddRecord()
{
	m_bAddingRecord = TRUE;
	m_pSet->ClearFields();
	UpdateData(FALSE);		// Show cleared values on window
}

BOOL COLEDBDepartmentMFCView::SaveRecord()
{
	if (!UpdateData(TRUE)) {	//Save changes from the screen
		return FALSE;
	}
	if (m_bAddingRecord) {
		m_bAddingRecord = FALSE;
		if (FAILED(m_pSet->Insert())) {
			AfxMessageBox("Insert failed",
				MB_ICONEXCLAMATION);
			m_pSet->MoveFirst();
			UpdateData(FALSE);		// Show cleared values on window
			return FALSE;
		}
	}
	else if (FAILED(m_pSet->SetData(0))) {	//Save changes from this accessor
		AfxMessageBox("Update via SetData failed",
			MB_ICONEXCLAMATION);
		UpdateData(FALSE);		// Show cleared values on window
		return FALSE;
	}
	return TRUE;
}

void COLEDBDepartmentMFCView::ResetRowSet()
{
	if (m_pSet->MoveNext() != S_OK) {	//Go to next record
		if (m_pSet->MoveFirst() != S_OK) {
			AfxMessageBox("No more records",
				MB_ICONEXCLAMATION);
			//No records, so set up an add record
			AddRecord();
		}
	}
	UpdateData(FALSE);	//Write changes to window
}

⌨️ 快捷键说明

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