ScoreInputDlg.cpp
上传用户:rs600066
上传日期:2017-10-16
资源大小:4788k
文件大小:7k
源码类别:

数据库系统

开发平台:

Visual C++

  1. // ScoreInputDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "school.h"
  5. #include "ScoreInputDlg.h"
  6. #include "classset.h"
  7. #include "examtimeset.h"
  8. #include "examsubjectset.h"
  9. #include "examtypeset.h"
  10. #include "StudentSet.h"
  11. #include "scoredlg.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CScoreInputDlg dialog
  19. CScoreInputDlg::CScoreInputDlg(CWnd* pParent /*=NULL*/)
  20. : CDialog(CScoreInputDlg::IDD, pParent)
  21. {
  22. //{{AFX_DATA_INIT(CScoreInputDlg)
  23. m_strClass = _T("");
  24. m_strSubject = _T("");
  25. m_strTime = _T("");
  26. m_strType = _T("");
  27. //}}AFX_DATA_INIT
  28. }
  29. void CScoreInputDlg::DoDataExchange(CDataExchange* pDX)
  30. {
  31. CDialog::DoDataExchange(pDX);
  32. //{{AFX_DATA_MAP(CScoreInputDlg)
  33. DDX_Control(pDX, IDC_COMBO_TYPE, m_ctrType);
  34. DDX_Control(pDX, IDC_COMBO_TIME, m_ctrTime);
  35. DDX_Control(pDX, IDC_COMBO_SUBJECT, m_ctrSubject);
  36. DDX_Control(pDX, IDC_COMBO_CLASS, m_ctrClass);
  37. DDX_Control(pDX, IDC_LIST2, m_ctrList);
  38. DDX_CBString(pDX, IDC_COMBO_CLASS, m_strClass);
  39. DDX_CBString(pDX, IDC_COMBO_SUBJECT, m_strSubject);
  40. DDX_CBString(pDX, IDC_COMBO_TIME, m_strTime);
  41. DDX_CBString(pDX, IDC_COMBO_TYPE, m_strType);
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CScoreInputDlg, CDialog)
  45. //{{AFX_MSG_MAP(CScoreInputDlg)
  46. ON_BN_CLICKED(IDC_BUTTON_INPUT, OnButtonInput)
  47. ON_NOTIFY(NM_DBLCLK, IDC_LIST2, OnDblclkList2)
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CScoreInputDlg message handlers
  52. void CScoreInputDlg::OnButtonInput() 
  53. {
  54. // TODO: Add your control notification handler code here
  55. UpdateData();
  56. if(m_strClass.IsEmpty())
  57. {
  58. AfxMessageBox("请选择班级");
  59. return;
  60. }
  61. if(m_strTime.IsEmpty())
  62. {
  63. AfxMessageBox("请选择考试时间段");
  64. return;
  65. }
  66. if(m_strType.IsEmpty())
  67. {
  68. AfxMessageBox("请选择考试类型");
  69. return;
  70. }
  71. if(m_strSubject.IsEmpty())
  72. {
  73. AfxMessageBox("请选择考试科目");
  74. return;
  75. }
  76. CString strSQL;
  77. strSQL.Format("select * from score 
  78. where class = '%s' and time = '%s' 
  79. and type = '%s' and subject = '%s'"
  80. ,m_strClass,m_strTime,m_strType,m_strSubject);
  81. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  82. {
  83. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  84. return ;
  85. }
  86. if(m_recordset.GetRecordCount()==0)
  87. {
  88. m_recordset.Close();
  89. CreateScoreTable();
  90. }
  91. else
  92. {
  93. m_recordset.Close();
  94. }
  95. RefreshData(strSQL);
  96. }
  97. void CScoreInputDlg::CreateScoreTable()
  98. {
  99. CString strSQL;
  100. CStudentSet StudentSet;
  101. strSQL.Format("select * from student where class = '%s'",m_strClass);
  102. if(!StudentSet.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  103. {
  104. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  105. return ;
  106. }
  107. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE))
  108. {
  109. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  110. return ;
  111. }
  112. while(!StudentSet.IsEOF())
  113. {
  114. m_recordset.AddNew();
  115. m_recordset.m_code = StudentSet.m_code;
  116. m_recordset.m_class = m_strClass;
  117. m_recordset.m_name = StudentSet.m_name;
  118. m_recordset.m_subject = m_strSubject;
  119. m_recordset.m_time = m_strTime;
  120. m_recordset.m_type = m_strType;
  121. m_recordset.Update();
  122. StudentSet.MoveNext();
  123. }
  124. m_recordset.Close();
  125. StudentSet.Close();
  126. }
  127. void CScoreInputDlg::RefreshData(CString strSQL)
  128. {
  129. m_ctrList.DeleteAllItems();
  130. m_ctrList.SetRedraw(FALSE);
  131. UpdateData(TRUE);
  132. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  133. {
  134. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  135. return ;
  136. }
  137. int i=0;
  138. char buffer[20];
  139. while(!m_recordset.IsEOF())
  140. {
  141. _ltoa(m_recordset.m_ID,buffer,10);
  142. m_ctrList.InsertItem(i,buffer);
  143. m_ctrList.SetItemText(i,1,m_recordset.m_code);
  144. m_ctrList.SetItemText(i,2,m_recordset.m_name);
  145. m_ctrList.SetItemText(i,3,m_recordset.m_subject);
  146. _ltoa(m_recordset.m_score,buffer,10);
  147. m_ctrList.SetItemText(i,4,buffer);
  148. _ltoa(m_recordset.m_makeup_score,buffer,10);
  149. m_ctrList.SetItemText(i,5,buffer);
  150. m_ctrList.SetItemText(i,6,m_recordset.m_absent);
  151. i++;
  152. m_recordset.MoveNext();
  153. }
  154. m_recordset.Close();
  155. m_ctrList.SetRedraw(TRUE);
  156. }
  157. BOOL CScoreInputDlg::OnInitDialog() 
  158. {
  159. CDialog::OnInitDialog();
  160. CString strSQL;
  161. //初始化班级
  162. CClassSet ClassSet ;
  163. strSQL="select * from class";
  164. if(!ClassSet.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  165. {
  166. AfxMessageBox("打开数据库失败!");
  167. }
  168. while(!ClassSet.IsEOF())
  169. {
  170. m_ctrClass.AddString(ClassSet.m_code);
  171. ClassSet.MoveNext();
  172. }
  173. ClassSet.Close();
  174. //初始化时间段
  175. CExamtimeSet TimeSet ;
  176. strSQL="select * from examtime";
  177. if(!TimeSet.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  178. {
  179. AfxMessageBox("打开数据库失败!");
  180. }
  181. while(!TimeSet.IsEOF())
  182. {
  183. m_ctrTime.AddString(TimeSet.m_time);
  184. TimeSet.MoveNext();
  185. }
  186. TimeSet.Close();
  187. //初始化考试类型
  188. CExamtypeSet TypeSet ;
  189. strSQL="select * from examtype";
  190. if(!TypeSet.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  191. {
  192. AfxMessageBox("打开数据库失败!");
  193. }
  194. while(!TypeSet.IsEOF())
  195. {
  196. m_ctrType.AddString(TypeSet.m_name);
  197. TypeSet.MoveNext();
  198. }
  199. TypeSet.Close();
  200. //初始化考试科目
  201. CExamSubjectSet SubjectSet ;
  202. strSQL="select * from examsubject";
  203. if(!SubjectSet.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  204. {
  205. AfxMessageBox("打开数据库失败!");
  206. }
  207. while(!SubjectSet.IsEOF())
  208. {
  209. m_ctrSubject.AddString(SubjectSet.m_name);
  210. SubjectSet.MoveNext();
  211. }
  212. SubjectSet.Close();
  213. //初始化成绩列表
  214. m_ctrList.InsertColumn(0,"序号");
  215. m_ctrList.InsertColumn(1,"学号");
  216. m_ctrList.InsertColumn(2,"姓名");
  217. m_ctrList.InsertColumn(3,"科目");
  218. m_ctrList.InsertColumn(4,"成绩");
  219. m_ctrList.InsertColumn(5,"补考成绩");
  220. m_ctrList.InsertColumn(6,"缺考标志");
  221. m_ctrList.SetColumnWidth(0,60);
  222. m_ctrList.SetColumnWidth(1,80);
  223. m_ctrList.SetColumnWidth(2,80);
  224. m_ctrList.SetColumnWidth(3,100);
  225. m_ctrList.SetColumnWidth(4,80);
  226. m_ctrList.SetColumnWidth(5,80);
  227. m_ctrList.SetColumnWidth(6,60);
  228. m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  229. return TRUE;  // return TRUE unless you set the focus to a control
  230.               // EXCEPTION: OCX Property Pages should return FALSE
  231. }
  232. void CScoreInputDlg::OnDblclkList2(NMHDR* pNMHDR, LRESULT* pResult) 
  233. {
  234. // TODO: Add your control notification handler code here
  235. CString strSQL;
  236. long score=0,makeup=0;
  237. CScoreDlg Dlg;
  238. UpdateData(TRUE);
  239. int i = m_ctrList.GetSelectionMark();
  240. if(i<0) return;
  241. if(IDOK!=Dlg.DoModal())
  242. {
  243. return;
  244. }
  245. if(!Dlg.m_strScore.IsEmpty())
  246. score = atol(Dlg.m_strScore);
  247. if(!Dlg.m_strMakeup.IsEmpty())
  248. makeup = atol(Dlg.m_strMakeup);
  249. strSQL.Format("select * from score where ID=%s",m_ctrList.GetItemText(i,0));
  250. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  251. {
  252. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  253. return ;
  254. }
  255. m_recordset.Edit();
  256. if(Dlg.m_bAbsent)
  257. m_recordset.m_absent = "是";
  258. m_recordset.m_score = score;
  259. m_recordset.m_makeup_score = makeup;
  260. m_recordset.Update();
  261. char buffer[20];
  262. _ltoa(m_recordset.m_score,buffer,10);
  263. m_ctrList.SetItemText(i,4,buffer);
  264. _ltoa(m_recordset.m_makeup_score,buffer,10);
  265. m_ctrList.SetItemText(i,5,buffer);
  266. m_ctrList.SetItemText(i,6,m_recordset.m_absent);
  267. m_recordset.Close();
  268. *pResult = 0;
  269. }