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

📄 mydbview.cpp

📁 这个小例子可以查询雇员的平均工资和基本信息
💻 CPP
字号:
// mydbView.cpp : implementation of the CMydbView class
//

#include "stdafx.h"
#include "mydb.h"

#include "mydbSet.h"
#include "mydbDoc.h"
#include "mydbView.h"
#include "AVGSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMydbView

IMPLEMENT_DYNCREATE(CMydbView, CRecordView)

BEGIN_MESSAGE_MAP(CMydbView, CRecordView)
	//{{AFX_MSG_MAP(CMydbView)
	ON_EN_CHANGE(IDC_ID, OnChangeId)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMydbView construction/destruction

CMydbView::CMydbView()
	: CRecordView(CMydbView::IDD)
{
	//{{AFX_DATA_INIT(CMydbView)
	m_pSet = NULL;
	m_DepartID = _T("");
	m_Birthday = _T("");
	m_EmployeeID = _T("");
	m_EmployeeName = _T("");
	m_HomeTele = _T("");
	m_Salary = 0.0;
	m_Manager = _T("");
	m_DepartName = _T("");
	m_Office = _T("");
	m_EDepartID = _T("");
	m_DDepartID = _T("");
	m_AVGSalary = 0.0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CMydbView::~CMydbView()
{
}

void CMydbView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMydbView)
	DDX_FieldText(pDX, IDC_MANAGER, m_pSet->m_Manager, m_pSet);
	DDX_FieldText(pDX, IDC_NAME, m_pSet->m_DepartName, m_pSet);
	DDX_FieldText(pDX, IDC_ROOM, m_pSet->m_Office, m_pSet);
	DDX_FieldText(pDX, IDC_EMPLOYBIRTH, m_pSet->m_Birthday, m_pSet);
	DDX_FieldText(pDX, IDC_EMPLOYEEID, m_pSet->m_EmployeeID, m_pSet);
	DDX_FieldText(pDX, IDC_EMPLOYEENAME, m_pSet->m_EmployeeName, m_pSet);
	DDX_FieldText(pDX, IDC_HOMETELE, m_pSet->m_HomeTele, m_pSet);
	DDX_FieldText(pDX, IDC_SALARY, m_pSet->m_Salary, m_pSet);
	DDX_FieldText(pDX, IDC_DEPARTID, m_pSet->m_EDepartID, m_pSet);
	DDX_FieldText(pDX, IDC_ID, m_pSet->m_DDepartID, m_pSet);
	//DDX_DoubleText(pDX, IDC_AVGSALARY, m_pSet->m_AVGSalary, m_pSet);
	//}}AFX_DATA_MAP
}

BOOL CMydbView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CMydbView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_mydbSet;
	m_pSet->m_strFilter="Employee.DepartID=Department.DepartID";
	m_pSet->m_strSort="EmployeeID";
	CRecordView::OnInitialUpdate();
	CalAVG();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CMydbView printing

BOOL CMydbView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMydbView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMydbView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMydbView diagnostics

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

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

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

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


/////////////////////////////////////////////////////////////////////////////
// CMydbView message handlers

BOOL CMydbView::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CMydbDoc*pDoc=GetDocument();
	if(pDoc->m_bInAdding){
		pDoc->m_bInAdding=FALSE;
		UpdateData(TRUE);
		if(!m_pSet->CanUpdate()){
			AfxMessageBox("Can't Update!");
			return FALSE;
		}

		try
		{
		m_pSet->Update();
		}
		
		catch(CDBException*e){
			AfxMessageBox(e->m_strError);
		}

		if(!m_pSet->CanRestart()){
		AfxMessageBox("Can't Requery!");
		return FALSE;
		}

		try{

			m_pSet->Requery();
		}
		catch(CDBException*e){
			AfxMessageBox(e->m_strError);
		}
		UpdateData(FALSE);
		}

	return CRecordView::OnMove(nIDMoveCommand);
}

void CMydbView::OnChangeId() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CRecordView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CMydbView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(lHint==SELECT_NEW_DEPART)
	{
		CMydbDoc*pDoc=GetDocument();
		CString DocString=pDoc->m_SelectedDepartName;
		m_pSet->m_strFilter="Employee.DepartID=Department.DepartID";
		if(DocString!="ALL")
		{
			m_pSet->m_strFilter+=" AND DepartName=?";
			m_pSet->m_DepartNameParam=DocString;
		}
		if(!m_pSet->CanRestart())
		{
			AfxMessageBox("Can't Requery!");
			return;
		}
		try
		{
			m_pSet->Requery();
		}
		catch(CDBException*e)
		{
			AfxMessageBox(e->m_strError);
		}
		UpdateData(FALSE);
		CalAVG();
	}
	CRecordView::OnUpdate(pSender,lHint,pHint);
	
}
//在记录视图类中新加一个成员函数
void CMydbView::CalAVG()
{
	CMydbDoc *pDoc=GetDocument();
	CString DocString=pDoc->m_SelectedDepartName;
	CAVGSet m_AVGSet;
	m_AVGSet.m_strFilter="Employee.DepartID=Department.DepartID";
	if(DocString!="ALL")
	{
		m_AVGSet.m_strFilter+=" AND DepartName='";
		m_AVGSet.m_strFilter+=DocString;
		m_AVGSet.m_strFilter+="'";
	}
	m_AVGSet.Open();
	CEdit*pEdit=(CEdit*)GetDlgItem(IDC_AVGSALARY);
	CString OutText="("+DocString.Left(4)+")"+":";
	char s[20];
	sprintf(s,"%7.2f",m_AVGSet.m_AVGSalary);
	OutText+=s;
	pEdit->SetWindowText(OutText);
	m_AVGSet.Close();
}

⌨️ 快捷键说明

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