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

数据库系统

开发平台:

Visual C++

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