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

📄 examview.cpp

📁 对学生信息的考试数据管理的数据库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ExamView.cpp : implementation of the CExamView class
//

#include "stdafx.h"
#include "Exam.h"

#include "ExamDoc.h"
#include "ExamView.h"
#include "NewTeacher.h"
#include "DelTeacher.h"
#include "NewCourse.h"
#include "DelCourse.h"
#include "NewClass.h"
#include "DelClass.h"
#include "NewStudent.h"
#include "DelStudent.h"



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

/////////////////////////////////////////////////////////////////////////////
// CExamView

IMPLEMENT_DYNCREATE(CExamView, CFormView)

BEGIN_MESSAGE_MAP(CExamView, CFormView)
	//{{AFX_MSG_MAP(CExamView)
	ON_COMMAND(ID_ADD_COURSE, OnAddCourse)
	ON_COMMAND(ID_DELETE_COURSE, OnDeleteCourse)
	ON_COMMAND(ID_ADD_TEACHER, OnAddTeacher)
	ON_COMMAND(ID_DELETE_TEACHER, OnDeleteTeacher)
	ON_COMMAND(ID_DELETE_STUDENT, OnDeleteStudent)
	ON_COMMAND(ID_ADD_CLASS, OnAddClass)
	ON_COMMAND(ID_DELETE_CLASS, OnDeleteClass)
	ON_COMMAND(ID_INPUT_MARK, OnInputMark)
	ON_COMMAND(ID_SORTBYMARK, OnSortbymark)
	ON_COMMAND(ID_QUERY, OnQuery)
	ON_COMMAND(ID_COLLECT, OnCollect)
	ON_COMMAND(ID_ADD_STUDENT, OnAddStudent)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExamView construction/destruction

CExamView::CExamView()
	: CFormView(CExamView::IDD)
{
	//{{AFX_DATA_INIT(CExamView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

	m_nCurrPage = 0;
	memset( m_arhwndPage, 0, sizeof(HWND)*3 );
}

CExamView::~CExamView()
{
}

void CExamView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExamView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CExamView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	try
	{
		// 创建Connection对象
		HRESULT hr = m_pConnection.CreateInstance(__uuidof(Connection)); 
		if (SUCCEEDED(hr))
		{
			// 确定UDL文件路径
			CString sConnectionString = "File Name=" + GetExePath() + "\\Datasource.udl";
			char temp[100];
			::strcpy(temp, sConnectionString);
			_bstr_t t(temp);

			// 指定UDL文件
			m_pConnection->ConnectionString = t;
			
			// 连接数据库
			m_pConnection->Open("", "", "", NULL); 
		}
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("连接数据库失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}

	// 创建子窗口
	CRect rcBaseFrm;
	GetClientRect(&rcBaseFrm);
	
	m_Form1.Create(IDD_FORMVIEW_1, this);
	m_arhwndPage[0] = m_Form1.GetSafeHwnd();
	
	m_Form2.Create(IDD_FORMVIEW_2, this);
	m_arhwndPage[1] = m_Form2.GetSafeHwnd();

	m_Form3.Create(IDD_FORMVIEW_3, this);
	m_arhwndPage[2] = m_Form3.GetSafeHwnd();
	
	m_Form4.Create(IDD_FORMVIEW_4, this);
	m_arhwndPage[3] = m_Form4.GetSafeHwnd();

	for (int i = 0; i < 3; i++)
		::MoveWindow(m_arhwndPage[i], rcBaseFrm.left + 2, rcBaseFrm.top + 2, rcBaseFrm.Width() - 4, rcBaseFrm.Height() - 4, 0);
	::ShowWindow(m_arhwndPage[0], SW_SHOW);
}
CString CExamView::GetExePath()
{
	// 得到模块全路径
	char exeFullPath[MAX_PATH];
	CString strPath;
	GetModuleFileName(NULL, exeFullPath, MAX_PATH);
	strPath = CString(exeFullPath);

	// 转换为配置文件全路径
	strPath = strPath.Left(strPath.ReverseFind('\\'));
	return strPath;
}

/////////////////////////////////////////////////////////////////////////////
// CExamView printing

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

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

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

void CExamView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CExamView diagnostics

#ifdef _DEBUG
void CExamView::AssertValid() const
{
	CFormView::AssertValid();
}

void CExamView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CExamView message handlers




BOOL CExamView::DestroyWindow() 
{
	try
	{
		// 关闭连接
		m_pConnection->Close();
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("关闭对象失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
	}
	
	return CFormView::DestroyWindow();
}

void CExamView::OnAddCourse() 
{
/*	// Recordset对象
	_RecordsetPtr pRecordset;
	
	try
	{
		// 创建Recordset对象
		pRecordset.CreateInstance(__uuidof(Recordset));
	
		// 打开记录集,使用静态光标和开放式锁定方式
		pRecordset->Open(_bstr_t("SELECT * FROM 教师信息"), 
						 _variant_t((IDispatch*)m_pConnection, true), 
						 adOpenStatic,
						 adLockOptimistic,
						 adCmdText);
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}

	// 显示信息录入对话框
	CNewCourse dlg;
	_variant_t vField;
	try
	{
		// 取得所有教师姓名
		for (int i = 0; i < pRecordset->GetRecordCount(); i++)
		{
			vField = pRecordset->GetCollect(long(1));
			dlg.m_sTeacher[i] = vField.bstrVal;
			dlg.m_nTeacherNum++;
			pRecordset->MoveNext();
		}

		// 关闭记录集
		pRecordset->Close();
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("访问记录字段失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
	}

	// 显示对话框
	if (dlg.DoModal() != IDOK)
		return;

	try
	{
		// 打开指定的记录集,使用静态光标和开放式锁定方式
		CString sCmd = "SELECT * FROM 教师信息 WHERE 姓名 = '" + dlg.m_sTeacher[0] + "'";
		pRecordset->Open(_bstr_t(sCmd), 
						 _variant_t((IDispatch*)m_pConnection, true), 
						 adOpenStatic,
						 adLockOptimistic,
						 adCmdText);

		// 得到教师ID索引
		vField = pRecordset->GetCollect(long(0));

		// 关闭记录集
		pRecordset->Close();
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("访问记录字段失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
	}

	try
	{
		// 打开记录集,使用静态光标和开放式锁定方式
		pRecordset->Open(_bstr_t("SELECT * FROM 课程信息"), 
						 _variant_t((IDispatch*)m_pConnection, true), 
						 adOpenStatic,
						 adLockOptimistic,
						 adCmdText);
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}

	try
	{
		// 添加新记录
		pRecordset->AddNew();
		pRecordset->PutCollect("课程名称", _variant_t(dlg.m_sName));
		CString sTemp;
		sTemp.Format("%d", dlg.m_nPeriod);
		pRecordset->PutCollect("学时", _variant_t(sTemp));
		pRecordset->PutCollect("教师ID", vField);
		sTemp.Format("%d", dlg.m_nMust);
		pRecordset->PutCollect("选修/必修", _variant_t(sTemp));
		pRecordset->PutCollect("选用教材", _variant_t(dlg.m_sBook));

		// 更新记录集
		pRecordset->Update();

		// 关闭记录集
		pRecordset->Close();
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("添加记录失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}*/
}

void CExamView::OnDeleteCourse() 
{
/*	// 显示信息删除对话框
	CDelCourse dlg;
	if (dlg.DoModal() != IDOK)
		return;

	// Recordset对象
	_RecordsetPtr pRecordset;
	
	try
	{
		// 创建Recordset对象
		pRecordset.CreateInstance(__uuidof(Recordset));
	
		// 打开指定的记录集,使用静态光标和开放式锁定方式
		CString sCmd = "SELECT * FROM 课程信息 WHERE 课程名称 = '" + dlg.m_sCourse + "'";
		pRecordset->Open(_bstr_t(sCmd), 
						 _variant_t((IDispatch*)m_pConnection, true), 
						 adOpenStatic,
						 adLockOptimistic,
						 adCmdText);
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}

	try
	{
		// 删除当前记录
		pRecordset->Delete(adAffectCurrent);

		// 关闭记录集
		pRecordset->Close();
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("删除记录失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}*/
}

void CExamView::OnAddTeacher() 
{
	// 显示信息录入对话框
/*	CNewTeacher dlg;
	if (dlg.DoModal() != IDOK)
		return;

	// Recordset对象
	_RecordsetPtr pRecordset;
	
	try
	{
		// 创建Recordset对象
		pRecordset.CreateInstance(__uuidof(Recordset));
	
		// 打开记录集,使用静态光标和开放式锁定方式
		pRecordset->Open(_bstr_t("SELECT * FROM 教师信息"), 
						 _variant_t((IDispatch*)m_pConnection, true), 
						 adOpenStatic,
						 adLockOptimistic,
						 adCmdText);
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}

	try
	{
		// 添加新记录
		pRecordset->AddNew();
		pRecordset->PutCollect("姓名", _variant_t(dlg.m_sName));
		pRecordset->PutCollect("电话", _variant_t(dlg.m_sPhone));
		pRecordset->PutCollect("职称", _variant_t(dlg.m_sLevel));
		pRecordset->PutCollect("年龄", _variant_t(dlg.m_sAge));
		pRecordset->PutCollect("简历", _variant_t(dlg.m_sRemark));

		// 更新记录集
		pRecordset->Update();

		// 关闭记录集
		pRecordset->Close();
	}
	catch (_com_error e)
	{
		// 显示错误信息
		CString errormessage;
		errormessage.Format("添加记录失败!\r\n错误信息:%s", e.ErrorMessage());
		AfxMessageBox(errormessage);
		return;
	}*/
}

void CExamView::OnDeleteTeacher() 

⌨️ 快捷键说明

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