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

数据库系统

开发平台:

Visual C++

  1. // ExamtypeDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "school.h"
  5. #include "ExamtypeDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CExamtypeDlg dialog
  13. CExamtypeDlg::CExamtypeDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CExamtypeDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CExamtypeDlg)
  17. m_strCode = _T("");
  18. m_strName = _T("");
  19. //}}AFX_DATA_INIT
  20. }
  21. void CExamtypeDlg::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CDialog::DoDataExchange(pDX);
  24. //{{AFX_DATA_MAP(CExamtypeDlg)
  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(CExamtypeDlg, CDialog)
  35. //{{AFX_MSG_MAP(CExamtypeDlg)
  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. // CExamtypeDlg message handlers
  45. BOOL CExamtypeDlg::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 CExamtypeDlg::RefreshData()
  63. {
  64. m_ctrList.DeleteAllItems();
  65. m_ctrList.SetRedraw(FALSE);
  66. UpdateData(TRUE);
  67. CString strSQL;
  68. strSQL.Format( "select * from examtype ");
  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 CExamtypeDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) 
  91. {
  92. // TODO: Add your control notification handler code here
  93. CString strSQL;
  94. UpdateData(TRUE);
  95. int i = m_ctrList.GetSelectionMark();
  96. strSQL.Format("select * from examtype where code='%s'",m_ctrList.GetItemText(i,0));
  97. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  98. {
  99. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  100. return ;
  101. }
  102. m_strName = m_recordset.m_name;
  103. m_strCode = m_recordset.m_code;
  104. m_recordset.Close();
  105. UpdateData(FALSE);
  106. m_bntSave.EnableWindow(FALSE);
  107. m_bntNew.EnableWindow();
  108. m_bntDelete.EnableWindow();
  109. m_bntModify.EnableWindow();
  110. *pResult = 0;
  111. }
  112. void CExamtypeDlg::OnButtonNew() 
  113. {
  114. // TODO: Add your control notification handler code here
  115. m_strName = "";
  116. m_strCode = "";
  117. m_bntSave.EnableWindow();
  118. m_bntNew.EnableWindow(FALSE);
  119. m_bntDelete.EnableWindow(FALSE);
  120. m_bntModify.EnableWindow(FALSE);
  121. UpdateData(FALSE);
  122. }
  123. void CExamtypeDlg::OnButtonSave() 
  124. {
  125. // TODO: Add your control notification handler code here
  126. UpdateData();
  127. if(m_strName=="")
  128. {
  129. AfxMessageBox("请输入类别名!");
  130. return;
  131. }
  132. if(m_strCode=="")
  133. {
  134. AfxMessageBox("请输入类别代码!");
  135. return;
  136. }
  137. CString strSQL;
  138. strSQL.Format("select * from examtype where code='%s'",m_strCode);
  139. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  140. {
  141. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  142. return ;
  143. }
  144. if( m_recordset.GetRecordCount()!=0)
  145. {
  146. AfxMessageBox("当前编码已经存在!请重新输入!");
  147. m_strCode = "";
  148. UpdateData(FALSE);
  149. m_recordset.Close();
  150. return;
  151. }
  152. m_recordset.AddNew();
  153. m_recordset.m_name = m_strName ;
  154. m_recordset.m_code = m_strCode ;
  155. m_recordset.Update();
  156. m_recordset.Close();
  157. RefreshData();
  158. }
  159. void CExamtypeDlg::OnButtonModify() 
  160. {
  161. // TODO: Add your control notification handler code here
  162. UpdateData();
  163. int i = m_ctrList.GetSelectionMark();
  164. if(0>i)
  165. {
  166. MessageBox("请选择一条记录进行修改!");
  167. return;
  168. }
  169. if(m_strName=="")
  170. {
  171. AfxMessageBox("请输入类别名!");
  172. return;
  173. }
  174. if(m_strCode=="")
  175. {
  176. AfxMessageBox("请输入类别代码!");
  177. return;
  178. }
  179. CString strSQL;
  180. strSQL.Format("select * from examtype where code= '%s' ",m_ctrList.GetItemText(i,0));
  181. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  182. {
  183. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  184. return ;
  185. }
  186. m_recordset.Edit();
  187. m_recordset.m_name = m_strName ;
  188. m_recordset.m_code = m_strCode ;
  189. m_recordset.Update();
  190. m_recordset.Close();
  191. RefreshData();
  192. }
  193. void CExamtypeDlg::OnButtonDelete() 
  194. {
  195. // TODO: Add your control notification handler code here
  196. int i = m_ctrList.GetSelectionMark();
  197. if(0>i)
  198. {
  199. MessageBox("请选择一条记录进行删除!");
  200. return;
  201. }
  202. CString strSQL;
  203. strSQL.Format("select * from examtype where code= '%s' ",m_ctrList.GetItemText(i,0));
  204. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  205. {
  206. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  207. return ;
  208. }
  209. //删除该记录
  210. m_recordset.Delete();
  211. m_recordset.Close();
  212. //更新用户界面
  213. RefreshData();
  214. m_strName = "";
  215. m_strCode = "";
  216. UpdateData(FALSE);
  217. }