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

数据库系统

开发平台:

Visual C++

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