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

📄 mydbview.cpp

📁 对两个表联合操作
💻 CPP
字号:
// mydbView.cpp : implementation of the CMydbView class
//

#include "stdafx.h"
#include "mydb.h"
#include "InputDDlg.h"
#include "mydbSet.h"
#include "mydbDoc.h"
#include "mydbView.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_COMMAND(ID_RECORD_FILTER, OnRecordFilter)
	ON_COMMAND(ID_RECORD_SORT, OnRecordSort)
	ON_LBN_SELCHANGE(IDC_FIELD_LIST, OnSelchangeFieldList)
	ON_LBN_SELCHANGE(IDC_DEPART_LIST, OnSelchangeDepartList)
	ON_COMMAND(ID_QUERY_EMPLOYEE, OnQueryEmployee)
	//}}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;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CMydbView::~CMydbView()
{
}

void CMydbView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMydbView)
	DDX_Control(pDX, IDC_FIELD_LIST, m_FieldList);
	DDX_Control(pDX, IDC_DEPART_LIST, m_DepartIDList);
	DDX_FieldText(pDX, IDC_DEPARTID, m_pSet->m_DepartID, 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);
	//}}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;
	CRecordView::OnInitialUpdate();
	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::OnRecordFilter() 
{
	// TODO: Add your command handler code here
	CStringArray m_DepartIDArray,m_AddedArray;
	m_pSet->m_strFilter.Empty();    //将筛选字符串置为空
if(!m_pSet->IsOpen())   //判断记录集对象是否打开
		m_pSet->Open();  //打开记录对象
else
	m_pSet->Requery();   //重新查询记录集
while(!m_pSet->IsEOF())
{
	m_DepartIDArray.Add(m_pSet->m_DepartID);  
	m_pSet->MoveNext();
}  //将部门ID存入字符串数组m_DepartIDArray中 
	
	m_DepartIDList.ResetContent();   //将列表框中内容删除
	m_DepartIDList.AddString("<ALL>");   //将<ALL>加入到列表框
	for(int i=0;i<m_DepartIDArray.GetSize();i++)
	{
		CString m_CurID;
		BOOL bFlag=TRUE;   //m_CurID可加入到列表框
		m_CurID=m_DepartIDArray.GetAt(i);  //将字符串赋给变量
	for(int j=0;j<m_AddedArray.GetSize();j++)
	{
		if(!m_CurID.Compare(m_AddedArray.GetAt(j)))
			bFlag=FALSE;  //遇到相同字符串则不重复加入
	}
	if(bFlag)
	{
		m_AddedArray.Add(m_CurID);
		m_DepartIDList.AddString(m_CurID);  //将m_CurID加入到数组和列表框
	}
}
	m_DepartIDList.SetCurSel(0);  //将出始标记设置在列表框的第一条目上
	
}

void CMydbView::OnRecordSort() 
{
	// TODO: Add your command handler code here
   m_FieldList.ResetContent();   //清除列表框中的内容
	m_FieldList.AddString("EmployeeID");   //将数据库表中列名加入到列表框中
	m_FieldList.AddString("EmployeeName");
	m_FieldList.AddString("Birthday");
	m_FieldList.AddString("HomeTele");
	m_FieldList.AddString("DepartID");
	m_FieldList.AddString("Salary");
	m_FieldList.SetCurSel(0);   //将出始标记设置在列表框的第一条目上
}

void CMydbView::OnSelchangeFieldList() 
{
	// TODO: Add your control notification handler code here
	CString m_SelectedField;
	int index=m_FieldList.GetCurSel();
	m_FieldList.GetText(index,m_SelectedField);//得到选中的用来排序的域名
	m_pSet->m_strSort=m_SelectedField;//将域名值赋给字符串变量m_strSort
	if(!m_pSet->CanRestart())//重新打开记录集
	{
		AfxMessageBox("Can't Requery the database!");
			return;
	}
		  try
	{
			m_pSet->Requery();//重新查询记录集
		}
		
		catch(CDBException*e){
			AfxMessageBox(e->m_strError);
		}
		UpdateData(FALSE);//将新记录显示在视图窗口中 
}

void CMydbView::OnSelchangeDepartList() 
{
	// TODO: Add your control notification handler code here
	CString m_SelectedDepartID;
	CString filter;
	int index=m_DepartIDList.GetCurSel();   //得到列表框中被选中条目的位置 
	m_DepartIDList.GetText(index,m_SelectedDepartID);  //将内容赋给变量
	if(!m_SelectedDepartID.Compare("<ALL>"))
	{
		filter.Empty ();
	}  //如被选中条目是<ALL>察看所有部门情况
	else{


		filter="DepartID=";
		filter+=m_SelectedDepartID;
		filter+=" ";
	}  //察看某一部门情况
	m_pSet->m_strFilter=filter;  //将设置好的字符串赋给筛选字符串m_strFilter
	if(!m_pSet->CanRestart())
	{
			AfxMessageBox("Can't Requery the database!");
			return;
	}
    try
	{
			m_pSet->Requery();  //重新查询记录集
	}
		
		catch(CDBException*e)
		{
			AfxMessageBox(e->m_strError);
		}
		UpdateData(FALSE);  //将新记录显示出来
	
}

void CMydbView::OnQueryEmployee() 
{
	// TODO: Add your command handler code here
	CInputDDlg InputDlg;//声明一个对话框类的对象
    if(InputDlg.DoModal()==IDOK)//将对话框显示出来,等待用户输入,如果用户输入并确定则进入IF语句
	{
		m_pSet->m_strFilter="EmployeeID=?";//设置记录集的筛选字符串

		m_pSet->m_EmployeeIDParam=InputDlg.m_InputID;//将ID号的字符串赋值给成员变量
		if(!m_pSet->CanRestart())//进行重新查询
		{
			AfxMessageBox("Can't Requery the Database!");
			return;
		}
			try
			{

				m_pSet->Requery();//重新查询记录集
			}
			catch(CDBException*e)
			{
				AfxMessageBox(e->m_strError);//异常处理
		}
		UpdateData(FALSE);//将新记录显示出来
		}	

}

⌨️ 快捷键说明

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