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

数据库系统

开发平台:

Visual C++

  1. // HonorDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "school.h"
  5. #include "HonorDlg.h"
  6. #include "HonorInfoDlg.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CHonorDlg dialog
  14. CHonorDlg::CHonorDlg(CWnd* pParent /*=NULL*/)
  15. : CDialog(CHonorDlg::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CHonorDlg)
  18. // NOTE: the ClassWizard will add member initialization here
  19. //}}AFX_DATA_INIT
  20. }
  21. void CHonorDlg::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CDialog::DoDataExchange(pDX);
  24. //{{AFX_DATA_MAP(CHonorDlg)
  25. DDX_Control(pDX, IDC_LIST1, m_ctrList);
  26. //}}AFX_DATA_MAP
  27. }
  28. BEGIN_MESSAGE_MAP(CHonorDlg, CDialog)
  29. //{{AFX_MSG_MAP(CHonorDlg)
  30. ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
  31. ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
  32. ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
  33. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  34. //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CHonorDlg message handlers
  38. BOOL CHonorDlg::OnInitDialog() 
  39. {
  40. CDialog::OnInitDialog();
  41. m_ctrList.InsertColumn(0,"序号");
  42. m_ctrList.InsertColumn(1,"姓名");
  43. m_ctrList.InsertColumn(2,"学号");
  44. m_ctrList.InsertColumn(3,"班级");
  45. m_ctrList.InsertColumn(4,"奖惩日期");
  46. m_ctrList.InsertColumn(5,"奖惩内容");
  47. m_ctrList.InsertColumn(6,"备注");
  48. m_ctrList.SetColumnWidth(0,60);
  49. m_ctrList.SetColumnWidth(1,80);
  50. m_ctrList.SetColumnWidth(2,80);
  51. m_ctrList.SetColumnWidth(3,80);
  52. m_ctrList.SetColumnWidth(4,100);
  53. m_ctrList.SetColumnWidth(5,160);
  54. m_ctrList.SetColumnWidth(6,160);
  55. m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  56. RefreshData("select * from honor");
  57. return TRUE;  // return TRUE unless you set the focus to a control
  58.               // EXCEPTION: OCX Property Pages should return FALSE
  59. }
  60. void CHonorDlg::RefreshData(CString strSQL)
  61. {
  62. m_ctrList.DeleteAllItems();
  63. m_ctrList.SetRedraw(FALSE);
  64. UpdateData(TRUE);
  65. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  66. {
  67. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  68. return ;
  69. }
  70. int i=0;
  71. char buffer[20];
  72. while(!m_recordset.IsEOF())
  73. {
  74. _ltoa(m_recordset.m_ID,buffer,10);
  75. m_ctrList.InsertItem(i,buffer);
  76. m_ctrList.SetItemText(i,1,m_recordset.m_name);
  77. m_ctrList.SetItemText(i,2,m_recordset.m_code);
  78. m_ctrList.SetItemText(i,3,m_recordset.m_class);
  79. m_ctrList.SetItemText(i,4,m_recordset.m_date);
  80. m_ctrList.SetItemText(i,5,m_recordset.m_brief);
  81. m_ctrList.SetItemText(i,6,m_recordset.m_content);
  82. i++;
  83. m_recordset.MoveNext();
  84. }
  85. m_recordset.Close();
  86. m_ctrList.SetRedraw(TRUE);
  87. }
  88. void CHonorDlg::OnButtonNew() 
  89. {
  90. // TODO: Add your control notification handler code here
  91. CHonorInfoDlg   Dlg;
  92. if(IDOK==Dlg.DoModal())
  93. {
  94. //添加新记录
  95. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE))
  96. {
  97. AfxMessageBox("打开数据库失败!");
  98. return ;
  99. }
  100. m_recordset.AddNew();
  101. m_recordset.m_name = Dlg.m_strName ;
  102. m_recordset.m_brief = Dlg.m_strBrief ;
  103. m_recordset.m_date =   Dlg.m_strDate ;
  104. m_recordset.m_class = Dlg.m_strClass ;
  105. m_recordset.m_code = Dlg.m_strCode ;
  106. m_recordset.m_content = Dlg.m_strContent ;
  107. m_recordset.Update();
  108. m_recordset.Close();
  109. RefreshData("select * from honor ");
  110. }
  111. }
  112. void CHonorDlg::OnButtonModify() 
  113. {
  114. // TODO: Add your control notification handler code here
  115. CHonorInfoDlg Dlg;
  116. UpdateData();
  117. int i = m_ctrList.GetSelectionMark();
  118. if(0>i)
  119. {
  120. AfxMessageBox("请选择一条记录进行修改!");
  121. return;
  122. }
  123. CString strSQL;
  124. strSQL.Format("select * from honor where ID = %s",m_ctrList.GetItemText(i,0));
  125. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  126. {
  127. AfxMessageBox("打开数据库失败!");
  128. return ;
  129. }
  130. Dlg.m_strName = m_recordset.m_name ;
  131. Dlg.m_strBrief = m_recordset.m_brief ;
  132. Dlg.m_strDate = m_recordset.m_date ;
  133. Dlg.m_strClass = m_recordset.m_class ;
  134. Dlg.m_strCode = m_recordset.m_code ;
  135. Dlg.m_strContent = m_recordset.m_content ;
  136. m_recordset.Close();
  137. if(IDOK==Dlg.DoModal())
  138. {//修改记录
  139. strSQL.Format("select * from honor where ID = %s",m_ctrList.GetItemText(i,0));
  140. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  141. {
  142. AfxMessageBox("打开数据库失败!");
  143. return ;
  144. }
  145. m_recordset.Edit();
  146. m_recordset.m_name = Dlg.m_strName ;
  147. m_recordset.m_brief = Dlg.m_strBrief ;
  148. m_recordset.m_date =   Dlg.m_strDate ;
  149. m_recordset.m_class = Dlg.m_strClass ;
  150. m_recordset.m_code = Dlg.m_strCode ;
  151. m_recordset.m_content = Dlg.m_strContent ;
  152. m_recordset.Update();
  153. m_recordset.Close();
  154. RefreshData("select * from honor ");
  155. }
  156. }
  157. void CHonorDlg::OnButtonDelete() 
  158. {
  159. // TODO: Add your control notification handler code here
  160. int i = m_ctrList.GetSelectionMark();
  161. if(0>i)
  162. {
  163. AfxMessageBox("请选择一条记录进行查看!");
  164. return;
  165. }
  166. CString strSQL;
  167. strSQL.Format("select * from honor where ID = %s ",m_ctrList.GetItemText(i,0));
  168. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  169. {
  170. AfxMessageBox("打开数据库失败!");
  171. return ;
  172. }
  173. m_recordset.Delete();
  174. m_recordset.Close();
  175. RefreshData("select * from honor ");
  176. }
  177. void CHonorDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
  178. {
  179. // TODO: Add your control notification handler code here
  180. CHonorInfoDlg Dlg;
  181. UpdateData();
  182. int i = m_ctrList.GetSelectionMark();
  183. if(0>i)
  184. {
  185. AfxMessageBox("请选择一条记录进行修改!");
  186. return;
  187. }
  188. CString strSQL;
  189. strSQL.Format("select * from honor where ID = %s",m_ctrList.GetItemText(i,0));
  190. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  191. {
  192. AfxMessageBox("打开数据库失败!");
  193. return ;
  194. }
  195. Dlg.m_strName = m_recordset.m_name ;
  196. Dlg.m_strBrief = m_recordset.m_brief ;
  197. Dlg.m_strDate = m_recordset.m_date ;
  198. Dlg.m_strClass = m_recordset.m_class ;
  199. Dlg.m_strCode = m_recordset.m_code ;
  200. Dlg.m_strContent = m_recordset.m_content ;
  201. m_recordset.Close();
  202. if(IDOK==Dlg.DoModal())
  203. {//修改记录
  204. strSQL.Format("select * from honor where ID = %s",m_ctrList.GetItemText(i,0));
  205. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  206. {
  207. AfxMessageBox("打开数据库失败!");
  208. return ;
  209. }
  210. m_recordset.Edit();
  211. m_recordset.m_name = Dlg.m_strName ;
  212. m_recordset.m_brief = Dlg.m_strBrief ;
  213. m_recordset.m_date =   Dlg.m_strDate ;
  214. m_recordset.m_class = Dlg.m_strClass ;
  215. m_recordset.m_code = Dlg.m_strCode ;
  216. m_recordset.m_content = Dlg.m_strContent ;
  217. m_recordset.Update();
  218. m_recordset.Close();
  219. RefreshData("select * from honor ");
  220. }
  221. *pResult = 0;
  222. }