📄 subform_3.cpp
字号:
// SubForm_3.cpp : implementation file
//
#include "stdafx.h"
#include "Exam.h"
#include "SubForm_3.h"
#include "ExamDoc.h"
#include "ExamView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSubForm_3 dialog
CSubForm_3::CSubForm_3(CWnd* pParent /*=NULL*/)
: CDialog(CSubForm_3::IDD, pParent)
{
//{{AFX_DATA_INIT(CSubForm_3)
m_sClass = _T("");
m_sExamID = _T("");
m_sGrade = _T("");
m_sName = _T("");
m_sStudentID = _T("");
//}}AFX_DATA_INIT
}
void CSubForm_3::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSubForm_3)
DDX_Control(pDX, IDC_RESULT, m_ctrlResult);
DDX_Control(pDX, IDC_BYCOURSE, m_ctrlCourse);
DDX_CBString(pDX, IDC_BYCLASS, m_sClass);
DDX_Text(pDX, IDC_BYEXAMID, m_sExamID);
DDX_CBString(pDX, IDC_BYGRADE, m_sGrade);
DDX_Text(pDX, IDC_BYNAME, m_sName);
DDX_Text(pDX, IDC_BYSTUDENTID, m_sStudentID);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSubForm_3, CDialog)
//{{AFX_MSG_MAP(CSubForm_3)
ON_BN_CLICKED(IDC_QUERY, OnQuery)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSubForm_3 message handlers
void CSubForm_3::OnQuery()
{
// 得到选中的课程名称
CString sCourse;
int nSel = m_ctrlCourse.GetCurSel();
if (nSel >= 0)
m_ctrlCourse.GetLBText(nSel, sCourse);
UpdateData(TRUE);
int nClassID = -1;
int nCourseID = -1;
int nStudentID = -1;
if (m_sGrade != "" && m_sClass != "")
nClassID = GetClassID();
if (sCourse != "")
nCourseID = GetCourseID(sCourse);
if (m_sName != "" || m_sStudentID != "")
nStudentID = GetStudentID();
CString sCmd = "SELECT * FROM 考生信息 WHERE ";
CString sTemp;
if (m_sExamID != "")
sCmd += "考号 = " + m_sExamID + " AND ";
if (nClassID >= 0)
{
sTemp.Format("班级ID = %d AND ", nClassID);
sCmd += sTemp;
}
if (nCourseID >= 0)
{
sTemp.Format("课程ID = %d AND ", nCourseID);
sCmd += sTemp;
}
if (nStudentID >= 0)
{
sTemp.Format("学生ID = %d AND ", nStudentID);
sCmd += sTemp;
}
sCmd += "1 = 1";
// Recordset对象
_RecordsetPtr pRecordset;
_variant_t vField;
try
{
// 创建Recordset对象
pRecordset.CreateInstance(__uuidof(Recordset));
// 打开记录集,使用静态光标和开放式锁定方式
pRecordset->Open(_bstr_t(sCmd),
_variant_t((IDispatch*)m_pView->m_pConnection, true),
adOpenStatic,
adLockOptimistic,
adCmdText);
// 清空列表框
m_ctrlResult.DeleteAllItems();
for (int i = 0; i < pRecordset->GetRecordCount(); i++)
{
// 考号
vField = pRecordset->GetCollect(long(1));
sTemp.Format("%d", vField.lVal);
m_ctrlResult.InsertItem(i, sTemp, 0);
// 姓名
vField = pRecordset->GetCollect(long(2));
sTemp = GetName(vField.lVal);
m_ctrlResult.SetItemText(i, 1, sTemp);
// 成绩
vField = pRecordset->GetCollect(long(5));
sTemp.Format("%d", vField.lVal);
m_ctrlResult.SetItemText(i, 2, sTemp);
pRecordset->MoveNext();
}
pRecordset->Close();
}
catch (_com_error e)
{
// 显示错误信息
CString errormessage;
errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
AfxMessageBox(errormessage);
return;
}
}
BOOL CSubForm_3::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[3] = {"考号", "姓名", "成绩"};
for (int i = 0; i < 3; 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
}
int CSubForm_3::GetClassID()
{
int ret;
// Recordset对象
_RecordsetPtr pRecordset;
_variant_t vField;
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);
// 得到班级索引
vField = pRecordset->GetCollect(long(0));
ret = vField.lVal;
pRecordset->Close();
}
catch (_com_error e)
{
// 显示错误信息
CString errormessage;
errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
AfxMessageBox(errormessage);
return FALSE;
}
return ret;
}
int CSubForm_3::GetCourseID(CString sCourse)
{
int ret;
// 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);
// 得到班级索引
vField = pRecordset->GetCollect(long(0));
ret = vField.lVal;
pRecordset->Close();
}
catch (_com_error e)
{
// 显示错误信息
CString errormessage;
errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
AfxMessageBox(errormessage);
return FALSE;
}
return ret;
}
int CSubForm_3::GetStudentID()
{
int ret;
// Recordset对象
_RecordsetPtr pRecordset;
_variant_t vField;
try
{
// 创建Recordset对象
pRecordset.CreateInstance(__uuidof(Recordset));
// 打开记录集,使用静态光标和开放式锁定方式
CString sCmd;
if (m_sName != "")
sCmd = "SELECT * FROM 学生信息 WHERE 姓名 = '" + m_sName + "'";
else if (m_sStudentID != "")
sCmd = "SELECT * FROM 学生信息 WHERE 学号 = '" + m_sStudentID + "'";
pRecordset->Open(_bstr_t(sCmd),
_variant_t((IDispatch*)m_pView->m_pConnection, true),
adOpenStatic,
adLockOptimistic,
adCmdText);
// 得到班级索引
vField = pRecordset->GetCollect(long(0));
ret = vField.lVal;
pRecordset->Close();
}
catch (_com_error e)
{
// 显示错误信息
CString errormessage;
errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
AfxMessageBox(errormessage);
return FALSE;
}
return ret;
}
CString CSubForm_3::GetName(int nID)
{
CString ret;
// 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);
// 得到班级索引
vField = pRecordset->GetCollect(long(2));
ret = vField.bstrVal;
pRecordset->Close();
}
catch (_com_error e)
{
// 显示错误信息
CString errormessage;
errormessage.Format("打开记录集失败!\r\n错误信息:%s", e.ErrorMessage());
AfxMessageBox(errormessage);
return "";
}
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -