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

数据库系统

开发平台:

Visual C++

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