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

数据库系统

开发平台:

Visual C++

  1. // ExamSubjectDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "school.h"
  5. #include "ExamSubjectDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CExamSubjectDlg dialog
  13. CExamSubjectDlg::CExamSubjectDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CExamSubjectDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CExamSubjectDlg)
  17. m_strCode = _T("");
  18. m_strName = _T("");
  19. //}}AFX_DATA_INIT
  20. }
  21. void CExamSubjectDlg::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CDialog::DoDataExchange(pDX);
  24. //{{AFX_DATA_MAP(CExamSubjectDlg)
  25. DDX_Control(pDX, IDC_LIST1, m_ctrList);
  26. DDX_Control(pDX, IDC_BUTTON_SAVE, m_bntSave);
  27. DDX_Control(pDX, IDC_BUTTON_NEW, m_bntNew);
  28. DDX_Control(pDX, IDC_BUTTON_MODIFY, m_bntModify);
  29. DDX_Control(pDX, IDC_BUTTON_DELETE, m_bntDelete);
  30. DDX_Text(pDX, IDC_EDIT_CODE, m_strCode);
  31. DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
  32. //}}AFX_DATA_MAP
  33. }
  34. BEGIN_MESSAGE_MAP(CExamSubjectDlg, CDialog)
  35. //{{AFX_MSG_MAP(CExamSubjectDlg)
  36. ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
  37. ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
  38. ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
  39. ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
  40. ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CExamSubjectDlg message handlers
  45. BOOL CExamSubjectDlg::OnInitDialog() 
  46. {
  47. CDialog::OnInitDialog();
  48. m_ctrList.InsertColumn(0,"科目代码");
  49. m_ctrList.InsertColumn(1,"考试科目");
  50. m_ctrList.SetColumnWidth(0,80);
  51. m_ctrList.SetColumnWidth(1,160);
  52. m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  53. //设置按钮状态
  54. m_bntSave.EnableWindow(FALSE);
  55. m_bntNew.EnableWindow(FALSE);
  56. m_bntDelete.EnableWindow(FALSE);
  57. m_bntModify.EnableWindow(FALSE);
  58. RefreshData();
  59. return TRUE;  // return TRUE unless you set the focus to a control
  60.               // EXCEPTION: OCX Property Pages should return FALSE
  61. }
  62. void CExamSubjectDlg::RefreshData()
  63. {
  64. m_ctrList.DeleteAllItems();
  65. m_ctrList.SetRedraw(FALSE);
  66. UpdateData(TRUE);
  67. CString strSQL;
  68. strSQL.Format( "select * from examsubject ");
  69. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  70. {
  71. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  72. return ;
  73. }
  74. int i=0;
  75. while(!m_recordset.IsEOF())
  76. {
  77. m_ctrList.InsertItem(i,m_recordset.m_code);
  78. m_ctrList.SetItemText(i,1,m_recordset.m_name);
  79. i++;
  80. m_recordset.MoveNext();
  81. }
  82. m_recordset.Close();
  83. m_ctrList.SetRedraw(TRUE);
  84. //设置按钮状态
  85. m_bntSave.EnableWindow(FALSE);
  86. m_bntNew.EnableWindow();
  87. m_bntDelete.EnableWindow(FALSE);
  88. m_bntModify.EnableWindow(FALSE);
  89. }
  90. void CExamSubjectDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
  91. {
  92. CString strSQL;
  93. UpdateData(TRUE);
  94. int i = m_ctrList.GetSelectionMark();
  95. strSQL.Format("select * from examsubject where code='%s'",m_ctrList.GetItemText(i,0));
  96. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  97. {
  98. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  99. return ;
  100. }
  101. m_strName = m_recordset.m_name;
  102. m_strCode = m_recordset.m_code;
  103. m_recordset.Close();
  104. UpdateData(FALSE);
  105. m_bntSave.EnableWindow(FALSE);
  106. m_bntNew.EnableWindow();
  107. m_bntDelete.EnableWindow();
  108. m_bntModify.EnableWindow();
  109. *pResult = 0;
  110. }
  111. void CExamSubjectDlg::OnButtonNew() 
  112. {
  113. // TODO: Add your control notification handler code here
  114. m_strName = "";
  115. m_strCode = "";
  116. m_bntSave.EnableWindow();
  117. m_bntNew.EnableWindow(FALSE);
  118. m_bntDelete.EnableWindow(FALSE);
  119. m_bntModify.EnableWindow(FALSE);
  120. UpdateData(FALSE);
  121. }
  122. void CExamSubjectDlg::OnButtonSave() 
  123. {
  124. // TODO: Add your control notification handler code here
  125. UpdateData();
  126. if(m_strName=="")
  127. {
  128. AfxMessageBox("请输入考试科目!");
  129. return;
  130. }
  131. if(m_strCode=="")
  132. {
  133. AfxMessageBox("请输入科目代码!");
  134. return;
  135. }
  136. CString strSQL;
  137. strSQL.Format("select * from examsubject where code='%s'",m_strCode);
  138. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  139. {
  140. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  141. return ;
  142. }
  143. if( m_recordset.GetRecordCount()!=0)
  144. {
  145. AfxMessageBox("当前编码已经存在!请重新输入!");
  146. m_strCode = "";
  147. UpdateData(FALSE);
  148. m_recordset.Close();
  149. return;
  150. }
  151. m_recordset.AddNew();
  152. m_recordset.m_name = m_strName ;
  153. m_recordset.m_code = m_strCode ;
  154. m_recordset.Update();
  155. m_recordset.Close();
  156. RefreshData();
  157. }
  158. void CExamSubjectDlg::OnButtonModify() 
  159. {
  160. // TODO: Add your control notification handler code here
  161. UpdateData();
  162. int i = m_ctrList.GetSelectionMark();
  163. if(0>i)
  164. {
  165. MessageBox("请选择一条记录进行修改!");
  166. return;
  167. }
  168. if(m_strName=="")
  169. {
  170. AfxMessageBox("请输入考试科目!");
  171. return;
  172. }
  173. if(m_strCode=="")
  174. {
  175. AfxMessageBox("请输入科目代码!");
  176. return;
  177. }
  178. CString strSQL;
  179. strSQL.Format("select * from examsubject where code= '%s' ",m_ctrList.GetItemText(i,0));
  180. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  181. {
  182. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  183. return ;
  184. }
  185. m_recordset.Edit();
  186. m_recordset.m_name = m_strName ;
  187. m_recordset.m_code = m_strCode ;
  188. m_recordset.Update();
  189. m_recordset.Close();
  190. RefreshData();
  191. }
  192. void CExamSubjectDlg::OnButtonDelete() 
  193. {
  194. // TODO: Add your control notification handler code here
  195. int i = m_ctrList.GetSelectionMark();
  196. if(0>i)
  197. {
  198. MessageBox("请选择一条记录进行删除!");
  199. return;
  200. }
  201. CString strSQL;
  202. strSQL.Format("select * from examsubject where code= '%s' ",m_ctrList.GetItemText(i,0));
  203. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  204. {
  205. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  206. return ;
  207. }
  208. //删除该记录
  209. m_recordset.Delete();
  210. m_recordset.Close();
  211. //更新用户界面
  212. RefreshData();
  213. m_strName = "";
  214. m_strCode = "";
  215. UpdateData(FALSE);
  216. }