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

📄 subform_2.cpp

📁 考试管理系统
💻 CPP
字号:
// SubForm_2.cpp : implementation file
//

#include "stdafx.h"
#include "Exam.h"
#include "SubForm_2.h"
#include "ExamDoc.h"
#include "ExamView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSubForm_2 dialog


CSubForm_2::CSubForm_2(CWnd* pParent /*=NULL*/)
	: CDialog(CSubForm_2::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSubForm_2)
	m_sClass = _T("");
	m_sGrade = _T("");
	//}}AFX_DATA_INIT
}


void CSubForm_2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSubForm_2)
	DDX_Control(pDX, IDC_COURSE, m_ctrlCourse);
	DDX_Control(pDX, IDC_RESULT, m_ctrlResult);
	DDX_CBString(pDX, IDC_CLASS, m_sClass);
	DDX_CBString(pDX, IDC_GRADE, m_sGrade);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSubForm_2, CDialog)
	//{{AFX_MSG_MAP(CSubForm_2)
	ON_BN_CLICKED(IDC_BYGRADE, OnBygrade)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubForm_2 message handlers

BOOL CSubForm_2::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// 设置列表框的扩展风格
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
	m_ctrlResult.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);						

	LV_COLUMN lvColumn;
	lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvColumn.fmt = LVCFMT_LEFT;
	lvColumn.cx = 67;

	// 对各列进行处理
	CString sField[7] = {"名次", "姓名", "考号", "学号", "年级", "班级", "成绩"};
	for (int i = 0; i < 7; i++)
	{    
		// 得到并插入字段名
		int len = sField[i].GetLength();
		CString temp = sField[i];
		TCHAR* szBuffer = new TCHAR[len + 1];
		strcpy(szBuffer, temp.GetBuffer(len));
		temp.ReleaseBuffer();
		lvColumn.pszText = szBuffer;
	    m_ctrlResult.InsertColumn(i, &lvColumn);
		delete szBuffer;
	}

	m_pView = (CExamView*)GetParent();

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

	// 显示信息录入对话框
	_variant_t vField;
	try
	{
		// 取得所有课程名称
		for (int i = 0; i < pRecordset->GetRecordCount(); i++)
		{
			vField = pRecordset->GetCollect(long(1));
			m_ctrlCourse.AddString(CString(vField.bstrVal));
			pRecordset->MoveNext();
		}

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

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSubForm_2::OnBygrade() 
{
	// 得到选中的课程名称
	CString sCourse;
	int nSel = m_ctrlCourse.GetCurSel();
	if (nSel >= 0)
		m_ctrlCourse.GetLBText(nSel, sCourse);
	UpdateData();

	if (sCourse != "")		
	{
		// Recordset对象
		_RecordsetPtr pRecordset;
		_variant_t vField;
		try
		{
			// 创建Recordset对象
			pRecordset.CreateInstance(__uuidof(Recordset));
		
			// 打开记录集,使用静态光标和开放式锁定方式
			CString sCmd = "SELECT * FROM 课程信息 WHERE 课程名称 = '" + sCourse + "'";
			pRecordset->Open(_bstr_t(sCmd), 
							 _variant_t((IDispatch*)m_pView->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);
		}

		_variant_t vField2;
		if (m_sGrade != "" && m_sClass != "")
		{
			try
			{
				// 创建Recordset对象
				pRecordset.CreateInstance(__uuidof(Recordset));
			
				// 打开记录集,使用静态光标和开放式锁定方式
				CString sCmd = "SELECT * FROM 班级信息 WHERE 班级名称 = '" + m_sClass + "' AND 年级 = '" + m_sGrade + "'";
				pRecordset->Open(_bstr_t(sCmd), 
								 _variant_t((IDispatch*)m_pView->m_pConnection, true), 
								 adOpenStatic,
								 adLockOptimistic,
								 adCmdText);

				// 得到班级ID索引
				vField2 = pRecordset->GetCollect(long(0));

				// 关闭记录集
				pRecordset->Close();
			}
			catch (_com_error e)
			{
				// 显示错误信息
				CString errormessage;
				errormessage.Format("访问记录字段失败!\r\n错误信息:%s", e.ErrorMessage());
				AfxMessageBox(errormessage);
			}
		}
	
		try
		{
			// 创建Recordset对象
			pRecordset.CreateInstance(__uuidof(Recordset));
		
			// 打开记录集,使用静态光标和开放式锁定方式
			CString sCmd;
			if (m_sGrade != "" && m_sClass != "")
				sCmd.Format("Select * from 考生信息 WHERE 课程ID = %d AND 班级ID = %d ORDER BY 年级名次", vField.lVal, vField2.lVal);
			else
				sCmd.Format("Select * from 考生信息 WHERE 课程ID = %d ORDER BY 年级名次", vField.lVal);


			pRecordset->Open(_bstr_t(sCmd), 
							 _variant_t((IDispatch*)m_pView->m_pConnection, true), 
							 adOpenStatic,
							 adLockOptimistic,
							 adCmdText);
			// 清空列表框
			m_ctrlResult.DeleteAllItems();

			CString sTemp;
			for (int i = 0; i < pRecordset->GetRecordCount(); i++)
			{
				// 名次
				vField = pRecordset->GetCollect(long(7));
				sTemp.Format("%d", vField.lVal);
				m_ctrlResult.InsertItem(i, sTemp, 0);

				// 学生ID=>姓名, 学号,年级,班级
				vField = pRecordset->GetCollect(long(2));
				CString sName, sStudentID, sGrade, sClass;
				GetStudentInfo(vField.lVal, &sName, &sStudentID, &sGrade, &sClass);

				m_ctrlResult.SetItemText(i, 1, sName);
				m_ctrlResult.SetItemText(i, 3, sStudentID);
				m_ctrlResult.SetItemText(i, 4, sGrade);
				m_ctrlResult.SetItemText(i, 5, sClass);

				// 考号
				vField = pRecordset->GetCollect(long(0));
				sTemp.Format("%d", vField.lVal);
				m_ctrlResult.SetItemText(i, 2, sTemp);

				// 成绩
				vField = pRecordset->GetCollect(long(5));
				sTemp.Format("%d", vField.lVal);
				m_ctrlResult.SetItemText(i, 6, sTemp);
				pRecordset->MoveNext();
			}

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

void CSubForm_2::GetStudentInfo(int nID, CString *sName, CString *sStudentID, CString *sGrade, CString *sClass)
{
	// Recordset对象
	_RecordsetPtr pRecordset;
	_variant_t vField;

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

	try
	{
		// 学号。姓名,班级ID

		// 学号
		vField = pRecordset->GetCollect(long(1));
		(*sStudentID).Format("%d", vField.lVal);

		// 姓名
		vField = pRecordset->GetCollect(long(2));
		*sName = vField.bstrVal;

		// 班级ID
		vField = pRecordset->GetCollect(long(3));

		// 年级与班级
		GetClassInfo(vField.lVal, sGrade, sClass);

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

void CSubForm_2::GetClassInfo(int nID, CString *sGrade, CString *sClass)
{
	// Recordset对象
	_RecordsetPtr pRecordset;
	_variant_t vField;

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

	try
	{
		// 年级
		vField = pRecordset->GetCollect(long(2));
		*sGrade = vField.bstrVal;

		// 班级
		vField = pRecordset->GetCollect(long(1));
		*sClass = vField.bstrVal;

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

⌨️ 快捷键说明

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