Dlg.h
private:
adoConn *m_pAdoConn;
int m_checked;
public:
void updateCombo();
void updateComboEnable();
void renewListInfo();
void clearListInfo();
CString GetCollect(_RecordsetPtr pRset, CString str);
Dlg.cpp
CString Cschool3Dlg::GetCollect(_RecordsetPtr pRset, CString str)
{
_variant_t var;
CString strVal;
var = pRset->GetCollect((_variant_t)str);
if (var.vt != VT_NULL)
strVal = (LPCSTR)_bstr_t(var);
return strVal;
}
void Cschool3Dlg::updateCombo()
{
CString vSQL = _T("select sname from student");
_RecordsetPtr pRset = m_pAdoConn->ExcuteSQL(vSQL);
CString str;
while (!pRset->adoEOF)
{
str = GetCollect(pRset, _T("sname"));
m_combo_sname.AddString(str);
pRset->MoveNext();
}
vSQL = _T("select cname from course");
pRset = m_pAdoConn->ExcuteSQL(vSQL);
while (!pRset->adoEOF)
{
str = GetCollect(pRset, _T("cname"));
m_combo_cname.AddString(str);
pRset->MoveNext();
}
}
void Cschool3Dlg::updateComboEnable()
{
switch (m_checked)
{
case 0:
m_combo_sname.EnableWindow(true);
m_combo_cname.EnableWindow(false);
break;
case 1:
m_combo_sname.EnableWindow(false);
m_combo_cname.EnableWindow(true);
break;
default:
break;
}
}
void Cschool3Dlg::updateComboEnable()
{
switch (m_checked)
{
case 0:
m_combo_sname.EnableWindow(true);
m_combo_cname.EnableWindow(false);
break;
case 1:
m_combo_sname.EnableWindow(false);
m_combo_cname.EnableWindow(true);
break;
default:
break;
}
}
void Cschool3Dlg::renewListInfo()
{
clearListInfo();
if (m_checked == 0)
{
m_listinfo.InsertColumn(0, _T("课程号"), 0, 60, -1);
m_listinfo.InsertColumn(1, _T("课程名"), 0, 60, -1);
CString str;
CString vSQL;
m_combo_sname.GetLBText(m_combo_sname.GetCurSel(), str);
vSQL.Format(_T("select c.cno, c.cname from student as s join elective as e on s.sno = e.sno join course as c on e.cno = c.cno where s.sname = '%s'"), str);
_RecordsetPtr pRset = m_pAdoConn->ExcuteSQL(vSQL);
int curItem = 0;
while (!pRset->adoEOF)
{
str = GetCollect(pRset, _T("cno"));
m_listinfo.InsertItem(curItem, str);
str = GetCollect(pRset, _T("cname"));
m_listinfo.SetItemText(curItem, 1, str);
pRset->MoveNext();
curItem++;
}
}
else if (m_checked == 1)
{
m_listinfo.InsertColumn(0, _T("学生姓名"), 0, 60, -1);
m_listinfo.InsertColumn(1, _T("分数"), 0, 60, -1);
CString str;
CString vSQL;
m_combo_cname.GetLBText(m_combo_cname.GetCurSel(), str);
vSQL.Format(_T("select s.sname, e.score from student as s join elective as e on s.sno = e.sno join course as c on e.cno = c.cno where c.cname = '%s ' order by score desc"), str);
_RecordsetPtr pRset = m_pAdoConn->ExcuteSQL(vSQL);
int curItem = 0;
while (!pRset->adoEOF)
{
str = GetCollect(pRset, _T("sname"));
m_listinfo.InsertItem(curItem, str);
str = GetCollect(pRset, _T("score"));
m_listinfo.SetItemText(curItem, 1, str);
pRset->MoveNext();
curItem++;
}
m_combo_cname.GetLBText(m_combo_cname.GetCurSel(), str);
vSQL.Format(_T("select avg(e.score) as avgscore from student as s join elective as e on s.sno = e.sno join course as c on e.cno = c.cno where c.cname = '%s'"), str);
pRset = m_pAdoConn->ExcuteSQL(vSQL);
if (!pRset->adoEOF)
{
str = GetCollect(pRset, _T("avgscore"));
m_static_avgscore.SetWindowTextW(str);
}
}
}
void Cschool3Dlg::clearListInfo()
{
m_listinfo.DeleteAllItems();
int n = m_listinfo.GetHeaderCtrl()->GetItemCount();
for (int i = 0; i < n; i++)
m_listinfo.DeleteColumn(0);
}
初始化内容
CoInitialize(NULL);
m_pAdoConn = new adoConn();
m_checked = 0;
m_radio_sname.SetCheck(true);
updateComboEnable();
updateCombo();
m_combo_sname.SetCurSel(0);
m_combo_cname.SetCurSel(0);