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

📄 odbcdepartmentview.cpp

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

#include "stdafx.h"
#include "ODBCDepartment.h"

#include "ODBCDepartmentSet.h"
#include "ODBCDepartmentDoc.h"
#include "ODBCDepartmentView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CODBCDepartmentView

IMPLEMENT_DYNCREATE(CODBCDepartmentView, CRecordView)

BEGIN_MESSAGE_MAP(CODBCDepartmentView, CRecordView)
	//{{AFX_MSG_MAP(CODBCDepartmentView)
	ON_COMMAND(ID_RECORD_DELETERECORD, OnRecordDeleterecord)
	ON_UPDATE_COMMAND_UI(ID_RECORD_DELETERECORD, OnUpdateRecordDeleterecord)
	ON_COMMAND(ID_RECORD_QUERYRECORD, OnRecordQueryrecord)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_WM_DESTROY()
	ON_EN_CHANGE(IDC_DEPARTMENTCODE, OnChangeDepartmentcode)
	ON_EN_CHANGE(IDC_DEPARTMENTNAME, OnChangeDepartmentname)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
	ON_EN_CHANGE(IDC_EMAIL, OnChangeEmail)
	ON_EN_CHANGE(IDC_INSTRUCTORNAME, OnChangeInstructorname)
	ON_EN_CHANGE(IDC_NOTES, OnChangeNotes)
	ON_COMMAND(ID_INSTRUCTOR_ADDINSTRUCTOR, OnInstructorAddinstructor)
	ON_COMMAND(ID_INSTRUCTOR_DELETEINSTRUCTOR, OnInstructorDeleteinstructor)
	ON_COMMAND(ID_INSTRUCTOR_FIRSTRECORD, OnInstructorFirstrecord)
	ON_COMMAND(ID_INSTRUCTOR_LASTRECORD, OnInstructorLastrecord)
	ON_COMMAND(ID_INSTRUCTOR_NEXTRECORD, OnInstructorNextrecord)
	ON_COMMAND(ID_INSTRUCTOR_PREVIOUSRECORD, OnInstructorPreviousrecord)
	ON_UPDATE_COMMAND_UI(ID_INSTRUCTOR_DELETEINSTRUCTOR, OnUpdateInstructorDeleteinstructor)
	ON_UPDATE_COMMAND_UI(ID_INSTRUCTOR_FIRSTRECORD, OnUpdateInstructorFirstrecord)
	ON_UPDATE_COMMAND_UI(ID_INSTRUCTOR_LASTRECORD, OnUpdateInstructorLastrecord)
	ON_UPDATE_COMMAND_UI(ID_INSTRUCTOR_NEXTRECORD, OnUpdateInstructorNextrecord)
	ON_UPDATE_COMMAND_UI(ID_INSTRUCTOR_PREVIOUSRECORD, OnUpdateInstructorPreviousrecord)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODBCDepartmentView construction/destruction

CODBCDepartmentView::CODBCDepartmentView()
	: CRecordView(CODBCDepartmentView::IDD)
{
	//{{AFX_DATA_INIT(CODBCDepartmentView)
	m_pSet = NULL;
	m_FindDeptCode = _T("");
	//}}AFX_DATA_INIT
	m_bAddingRecord = FALSE;
	//Next two lines added by Chuck Wood for
	//instructor table support
	m_pInstructorSet = NULL;
	m_bAddingInstructor = FALSE;
	m_bFieldsChanged = FALSE;
}

CODBCDepartmentView::~CODBCDepartmentView()
{
}

void CODBCDepartmentView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CODBCDepartmentView)
	DDX_Text(pDX, IDC_FINDCODE, m_FindDeptCode);
	DDV_MaxChars(pDX, m_FindDeptCode, 4);
	DDX_FieldText(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, m_pSet);
	DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
	DDX_FieldText(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, m_pSet);
	DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 50);
	//}}AFX_DATA_MAP
	//The rest of the lines in this function are added 
	//by Chuck Wood for instructor table support
	DDX_Text(pDX, IDC_EMAIL, m_pInstructorSet->m_EMAIL);
	DDV_MaxChars(pDX, m_pInstructorSet->m_EMAIL, 50);
	DDX_Text(pDX, IDC_INSTRUCTORNAME, m_pInstructorSet->m_Name);
	DDV_MaxChars(pDX, m_pInstructorSet->m_Name, 50);
	DDX_Text(pDX, IDC_NOTES, m_pInstructorSet->m_Notes);
}

BOOL CODBCDepartmentView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CRecordView::PreCreateWindow(cs);
}

void CODBCDepartmentView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_oDBCDepartmentSet;
	m_pInstructorSet = new CODBCInstructorSet(m_pSet->m_pDatabase);
	m_pInstructorSet->m_pDepartmentCode = 
		&GetDocument()->m_oDBCDepartmentSet.m_DepartmentCode;
 	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	m_pSet->Close();	//Close to start transaction
	if (m_pSet->m_pDatabase->CanTransact()) {
		m_pSet->m_pDatabase->BeginTrans();
	}
	m_pSet->Open();	//Open after starting transaction
	m_pSet->MoveFirst();
	OpenInstructorRecordset();	// Display related instructors
	UpdateData(FALSE);	//Update dialog box fields
}

/////////////////////////////////////////////////////////////////////////////
// CODBCDepartmentView diagnostics

#ifdef _DEBUG
void CODBCDepartmentView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CODBCDepartmentView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CODBCDepartmentView database support
CRecordset* CODBCDepartmentView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CODBCDepartmentView message handlers

BOOL CODBCDepartmentView::OnMove(UINT nIDMoveCommand) 
{
	if (m_bAddingRecord) {	//Currently adding?
		//If so, Update the record before the move
		UpdateData(TRUE);	//Get data from dialog box
		m_pSet->Update();	//Update data if needed
		m_pSet->MoveLast();	//Go to the added record
		m_bAddingRecord = FALSE;	//Reset flag
	}
	UpdateInstructor();
	BOOL returnCode = CRecordView::OnMove(nIDMoveCommand);
	m_pInstructorSet->Requery();
	CRecordsetStatus rStatus;		//Status variable
	m_pInstructorSet->GetStatus(rStatus); //Get CRecordset status
	if (rStatus.m_lCurrentRecord < 0) {	//no records Exist?
		//No isntructor records, so add one.
		AddInstructor();
	}
	UpdateData(FALSE);	//Update dialog box fields
	return returnCode;
}

void CODBCDepartmentView::OnRecordDeleterecord() 
{
	if (AfxMessageBox(	//Be sure to verify your deletes
			"Are you sure you want to delete this department?",
			MB_YESNO)
		!= IDYES) {
		return;
	}
	if (m_bAddingRecord) {	//Currently adding?
		//Don't delete, just cancel add.
		m_pSet->CancelUpdate();
		m_bAddingRecord = FALSE;
		OnMove(ID_RECORD_PREV);
		return;
	}
	try {
		m_pSet->Delete();	//Delete record
	}
	catch(CDBException* e1) {	//Failed
		AfxMessageBox("Delete Failed:\n" +
			e1->m_strError,
			MB_ICONEXCLAMATION);
		OnMove(ID_RECORD_FIRST);	//We lost our place.
		e1->Delete();		//Delete Error Message
		UpdateData(FALSE);	//Update dialog box fields
		return;
	}
	try {
		OnMove(ID_RECORD_NEXT);			//Go to next record
		if (m_pSet->IsDeleted()) {	//Was there a next record?
			OnMove(ID_RECORD_FIRST);	//Deleted last record
		}
		if (m_pSet->IsDeleted()) {	//Can't find a record
			AfxThrowDBException(SQL_ERROR, 
				m_pSet->m_pDatabase,
				m_pSet->m_hstmt);   
		}
		UpdateData(FALSE);	//Update dialog box fields
	}
	catch(CDBException* e2) {	//No records exist
		AfxMessageBox("No more records",
			MB_ICONEXCLAMATION);
		e2->Delete();		//Delete Error Message
		//Close and Open to get rid of the Deleted record
		m_pSet->Close();
		m_pSet->Open();
		//No records, so set up an add record
		OnFileNew();
	}
}

void CODBCDepartmentView::OnUpdateRecordDeleterecord(CCmdUI* pCmdUI) 
{
	//Disable delete functionality if no record is found
	pCmdUI->Enable(		//Enable delete if there's a record
		!m_pSet->IsBOF() &&
		!m_pSet->IsDeleted() &&
		!m_pSet->IsEOF());
}

void CODBCDepartmentView::OnRecordQueryrecord() 
{
	CString newFilter = "";	//Default is no filter
	UpdateData(TRUE);	//Get data from dialog box
	if (!m_bAddingRecord) {	//Currently adding?
		//Set to update
		m_pSet->Edit();		
	}
	m_pSet->Update();	//Update data if needed
	m_bAddingRecord = FALSE;	//Reset flag
	if (m_FindDeptCode != "") {
		//Setup new filter
		newFilter = "DepartmentCode = '" + m_FindDeptCode + "'";
	}
	if (newFilter != m_pSet->m_strFilter) {	//Filter has changed
		m_pSet->m_strFilter = newFilter;	//Assign new filter
		if (!m_pSet->Requery()) {			//Requery
			AfxMessageBox("Requery has failed");	//Error occurred
			m_pSet->m_strFilter = "";		//Try to get back
			m_pSet->Requery();				//Requery again
		}
		try {
			//Go to the first record of the new filtered recordset
			OnMove(ID_RECORD_FIRST);
		}
		catch(CDBException* e)    {
			//Move failed because there are no records
			AfxMessageBox("No records were found", MB_ICONEXCLAMATION );
			e->Delete();		//Delete Error Message
			//No records, so set up an add record
			OnFileNew();
		}
	}
	UpdateData(FALSE);	//Update dialog box fields
}

void CODBCDepartmentView::OnFileNew() 
{
	CRecordsetStatus rStatus;		//Status variable
	m_pSet->GetStatus(rStatus);		//Get CRecordset status
	if (rStatus.m_lCurrentRecord >= 0) {	//Records Exist?
		UpdateData(TRUE);	//Get data from dialog box
		if (!m_bAddingRecord) {	//Currently adding?
			//If not, set CRecordset in edit mode for updating
			m_pSet->Edit();		
		}
		m_pSet->Update();	//Update data if needed
		OnMove(ID_RECORD_LAST);	//Get off record 1
	}
	m_bAddingRecord = TRUE;	//Set flag
	m_pSet->SetFieldNull(NULL);	//Clear all fields
	m_pSet->AddNew();	//Set database in AddNew mode
	UpdateData(FALSE);	//Update dialog box fields

⌨️ 快捷键说明

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