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

📄 queryteachdlg.cpp

📁 student manage system
💻 CPP
字号:
// QueryTeachDlg.cpp : implementation file
//

#include "stdafx.h"
#include "StudentScore.h"
#include "QueryTeachDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CQueryTeachDlg dialog


CQueryTeachDlg::CQueryTeachDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQueryTeachDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQueryTeachDlg)
	//}}AFX_DATA_INIT
}


void CQueryTeachDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQueryTeachDlg)
	DDX_Control(pDX, IDC_LIST, m_list);
	DDX_Control(pDX, IDC_COMBO_BY, m_combox);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CQueryTeachDlg, CDialog)
	//{{AFX_MSG_MAP(CQueryTeachDlg)
	ON_CBN_SELCHANGE(IDC_COMBO_BY, OnSelchangeComboBy)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueryTeachDlg message handlers

void CQueryTeachDlg::OnOK() //确定按钮事件
{
	//获得控件并赋值给字符串
	CComboBox* m_type=(CComboBox*) this->GetDlgItem(IDC_COMBO_BY);
	CListBox* m_value=(CListBox*) this->GetDlgItem(IDC_LIST_VALUE);
	CString value,type;
	int i=m_value->GetCurSel();
	m_value->GetText(i,value);
	m_type->GetWindowText(type);
	//判断类型
	if(type=="教师名")
		type="teacher";
	else
		type="course";
	BOOL bSuccess=true;
	if(value=="")//如果查询值为空
	{
		MessageBox("请选择查询值!");
		bSuccess=false;
	}
	if(bSuccess)
	{
		//构造查询语句
		CString strSQL;
		if(type=="teacher")
			strSQL.Format("select * from teach,course where teacher_no in(select teacher_no from teacher where teacher_name='%s') and teach.active_status='Y' and teach.course_no=course.course_no",value); 
		else
			strSQL.Format("select * from teach,teacher where course_no in(select course_no from course where course_name='%s') and teach.active_status='Y' and teach.teacher_no=teacher.teacher_no",value);
		//打开记录集
		m_recordSet.Open(CRecordset::forwardOnly,strSQL);
		//将记录集显示到 CListBox中
		for(int k=m_list.GetCount();k>=0;k--)
			m_list.DeleteString(k);
		for(int j=0;j<m_recordSet.GetRecordCount();j++){
			CString temp;
			if(type=="teacher")
				m_recordSet.GetFieldValue("course_name",temp);
			else
				m_recordSet.GetFieldValue("teacher_name",temp);
			m_list.AddString(temp);
			m_recordSet.MoveNext();
		}
		//关闭记录集
		m_recordSet.Close();

	
	}
	//CDialog::OnOK();
}

BOOL CQueryTeachDlg::OnInitDialog()//初始化对话框
{
	CDialog::OnInitDialog();
	//如果没有打开数据库,则打开数据库
	if(!m_database.IsOpen())
	{
		m_database.Open(_T("studentscore"));
	    m_recordSet.m_pDatabase=&m_database;
	}
	//初始化条件
    m_combox.AddString("课程名");
    m_combox.AddString("教师名");
	m_combox.SelectString(0,"课程名");
	//构造查询语句
	CString strSQL;
	strSQL.Format("select * from course where active_status='Y'");
	//打开记录集
	m_recordSet.Open(CRecordset::forwardOnly,strSQL);
	//将数据显示到ClistBox中
	CListBox* m_value=(CListBox*) this->GetDlgItem(IDC_LIST_VALUE);
	for(int j=0;j<m_recordSet.GetRecordCount();j++){
		CString temp;
		m_recordSet.GetFieldValue("course_name",temp);
        m_value->AddString(temp);
		m_recordSet.MoveNext();
	}
	//关闭数据库
	m_recordSet.Close();
	return true;
}

void CQueryTeachDlg::OnSelchangeComboBy() //选中下拉列表触发事件
{
    CListBox* m_value=(CListBox*) this->GetDlgItem(IDC_LIST_VALUE);
	//清除listBox数据
	for(int j=m_value->GetCount();j>=0;j--)
		m_value->DeleteString(j);
	CString type;
	m_combox.GetWindowText(type);
	if(type=="教师名")
		type="course";
	else
        type="teacher";
	//构造查询语句
	CString strSQL;
	strSQL.Format("select * from %s where active_status='Y'",type);
	type=type+"_name";
	//打开记录集
    m_recordSet.Open(CRecordset::forwardOnly,strSQL);
	//将数据显示在ClistBox里
	for(int k=0;k<m_recordSet.GetRecordCount();k++){
		CString temp;
		m_recordSet.GetFieldValue(type,temp);
        m_value->AddString(temp);
		m_recordSet.MoveNext();
	}
	//关闭数据库
	m_recordSet.Close();	
}

⌨️ 快捷键说明

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