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

数据库系统

开发平台:

Visual C++

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