?? queryteachdlg.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 + -