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

数据库系统

开发平台:

Visual C++

  1. // ScholarshipDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "school.h"
  5. #include "ScholarshipDlg.h"
  6. #include "scholarshipinfodlg.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CScholarshipDlg dialog
  14. CScholarshipDlg::CScholarshipDlg(CWnd* pParent /*=NULL*/)
  15. : CDialog(CScholarshipDlg::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CScholarshipDlg)
  18. m_nCondition = -1;
  19. m_strContent = _T("");
  20. //}}AFX_DATA_INIT
  21. }
  22. void CScholarshipDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CScholarshipDlg)
  26. DDX_Control(pDX, IDC_LIST1, m_ctrList);
  27. DDX_CBIndex(pDX, IDC_COMBO_CONDITION, m_nCondition);
  28. DDX_CBString(pDX, IDC_COMBO_CONTENT, m_strContent);
  29. //}}AFX_DATA_MAP
  30. }
  31. BEGIN_MESSAGE_MAP(CScholarshipDlg, CDialog)
  32. //{{AFX_MSG_MAP(CScholarshipDlg)
  33. ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
  34. ON_BN_CLICKED(IDC_BUTTON_SEARCH, OnButtonSearch)
  35. ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
  36. ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
  37. ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CScholarshipDlg message handlers
  42. BOOL CScholarshipDlg::OnInitDialog() 
  43. {
  44. CDialog::OnInitDialog();
  45. m_ctrList.InsertColumn(0,"序号");
  46. m_ctrList.InsertColumn(1,"奖学金名称");
  47. m_ctrList.InsertColumn(2,"奖学金类型");
  48. m_ctrList.InsertColumn(3,"奖学金等级");
  49. m_ctrList.InsertColumn(4,"获奖人");
  50. m_ctrList.InsertColumn(5,"说明");
  51. m_ctrList.InsertColumn(6,"获奖日期");
  52. m_ctrList.SetColumnWidth(0,60);
  53. m_ctrList.SetColumnWidth(1,100);
  54. m_ctrList.SetColumnWidth(2,100);
  55. m_ctrList.SetColumnWidth(3,100);
  56. m_ctrList.SetColumnWidth(4,100);
  57. m_ctrList.SetColumnWidth(5,100);
  58. m_ctrList.SetColumnWidth(6,100);
  59. m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  60. RefreshData("select * from scholarship");
  61. return TRUE;  // return TRUE unless you set the focus to a control
  62.               // EXCEPTION: OCX Property Pages should return FALSE
  63. }
  64. void CScholarshipDlg::RefreshData(CString strSQL)
  65. {
  66. m_ctrList.DeleteAllItems();
  67. m_ctrList.SetRedraw(FALSE);
  68. UpdateData(TRUE);
  69. // strSQL="select * from dept";
  70. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  71. {
  72. MessageBox("打开数据库失败!","数据库错误",MB_OK);
  73. return ;
  74. }
  75. int i=0;
  76. CString strTime;
  77. char buffer[20];
  78. while(!m_recordset.IsEOF())
  79. {
  80. _ltoa(m_recordset.m_ID,buffer,10);
  81. m_ctrList.InsertItem(i,buffer);
  82. m_ctrList.SetItemText(i,1,m_recordset.m_name);
  83. m_ctrList.SetItemText(i,2,m_recordset.m_type);
  84. m_ctrList.SetItemText(i,3,m_recordset.m_level);
  85. m_ctrList.SetItemText(i,4,m_recordset.m_person);
  86. m_ctrList.SetItemText(i,5,m_recordset.m_brief);
  87. strTime.Format("%d-%d-%d",m_recordset.m_date.GetYear(),m_recordset.m_date.GetMonth(),m_recordset.m_date.GetDay());
  88. m_ctrList.SetItemText(i,6,strTime);
  89. i++;
  90. m_recordset.MoveNext();
  91. }
  92. m_recordset.Close();
  93. m_ctrList.SetRedraw(TRUE);
  94. }
  95. void CScholarshipDlg::OnButtonNew() 
  96. {
  97. // TODO: Add your control notification handler code here
  98. CScholarshipInfoDlg   Dlg;
  99. if(IDOK==Dlg.DoModal())
  100. {
  101. //添加新记录
  102. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE))
  103. {
  104. AfxMessageBox("打开数据库失败!");
  105. return ;
  106. }
  107. m_recordset.AddNew();
  108. m_recordset.m_name = Dlg.m_strName ;
  109. m_recordset.m_brief = Dlg.m_strBrief ;
  110. m_recordset.m_date =   Dlg.m_tmDate ;
  111. m_recordset.m_level = Dlg.m_strLevel ;
  112. m_recordset.m_person = Dlg.m_strPerson ;
  113. m_recordset.m_type = Dlg.m_strType ;
  114. m_recordset.Update();
  115. m_recordset.Close();
  116. CString strSQL = "select * from scholarship ";
  117. RefreshData(strSQL);
  118. }
  119. }
  120. void CScholarshipDlg::OnButtonSearch() 
  121. {
  122. // TODO: Add your control notification handler code here
  123. UpdateData();
  124. CString strSQL;
  125. if(0==m_nCondition)
  126. {//奖学金名称
  127. strSQL.Format("select * from scholarship where name = '%s'",m_strContent);
  128. }else if(1==m_nCondition)
  129. {//奖学金类型
  130. strSQL.Format("select * from scholarship where type = '%s'",m_strContent);
  131. }else if(2==m_nCondition)
  132. {//奖学金等级
  133. strSQL.Format("select * from scholarship where level = '%s'",m_strContent);
  134. }else if(3==m_nCondition)
  135. {//获奖人
  136. strSQL.Format("select * from scholarship where person = '%s'",m_strContent);
  137. }else if(4==m_nCondition)
  138. {//全部
  139. strSQL.Format("select * from scholarship");
  140. }else
  141. {
  142. strSQL.Format("select * from scholarship ");
  143. }
  144. RefreshData(strSQL);
  145. }
  146. void CScholarshipDlg::OnButtonModify() 
  147. {
  148. // TODO: Add your control notification handler code here
  149. CScholarshipInfoDlg Dlg;
  150. UpdateData();
  151. int i = m_ctrList.GetSelectionMark();
  152. if(0>i)
  153. {
  154. AfxMessageBox("请选择一条记录进行修改!");
  155. return;
  156. }
  157. CString strSQL;
  158. strSQL.Format("select * from scholarship where ID = %s",m_ctrList.GetItemText(i,0));
  159. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  160. {
  161. AfxMessageBox("打开数据库失败!");
  162. return ;
  163. }
  164. Dlg.m_strName = m_recordset.m_name ;
  165. Dlg.m_strBrief = m_recordset.m_brief ;
  166. Dlg.m_tmDate = m_recordset.m_date     ;
  167. Dlg.m_strLevel = m_recordset.m_level ;
  168. Dlg.m_strPerson = m_recordset.m_person ;
  169. Dlg.m_strType = m_recordset.m_type ;
  170. m_recordset.Close();
  171. if(IDOK==Dlg.DoModal())
  172. {//修改记录
  173. strSQL.Format("select * from scholarship where ID = %s",m_ctrList.GetItemText(i,0));
  174. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  175. {
  176. AfxMessageBox("打开数据库失败!");
  177. return ;
  178. }
  179. m_recordset.Edit();
  180. m_recordset.m_name = Dlg.m_strName ;
  181. m_recordset.m_brief = Dlg.m_strBrief ;
  182. m_recordset.m_date =   Dlg.m_tmDate ;
  183. m_recordset.m_level = Dlg.m_strLevel ;
  184. m_recordset.m_person = Dlg.m_strPerson ;
  185. m_recordset.m_type = Dlg.m_strType ;
  186. m_recordset.Update();
  187. m_recordset.Close();
  188. strSQL = "select * from scholarship ";
  189. RefreshData(strSQL);
  190. }
  191. }
  192. void CScholarshipDlg::OnButtonDelete() 
  193. {
  194. // TODO: Add your control notification handler code here
  195. int i = m_ctrList.GetSelectionMark();
  196. if(0>i)
  197. {
  198. AfxMessageBox("请选择一条记录进行查看!");
  199. return;
  200. }
  201. CString strSQL;
  202. strSQL.Format("select * from scholarship where ID = %s ",m_ctrList.GetItemText(i,0));
  203. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  204. {
  205. AfxMessageBox("打开数据库失败!");
  206. return ;
  207. }
  208. m_recordset.Delete();
  209. m_recordset.Close();
  210. strSQL = "select * from scholarship";
  211. RefreshData(strSQL);
  212. }
  213. void CScholarshipDlg::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
  214. {
  215. // TODO: Add your control notification handler code here
  216. CScholarshipInfoDlg Dlg;
  217. UpdateData();
  218. int i = m_ctrList.GetSelectionMark();
  219. if(0>i)
  220. {
  221. AfxMessageBox("请选择一条记录进行修改!");
  222. return;
  223. }
  224. CString strSQL;
  225. strSQL.Format("select * from scholarship where ID = %s",m_ctrList.GetItemText(i,0));
  226. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  227. {
  228. AfxMessageBox("打开数据库失败!");
  229. return ;
  230. }
  231. Dlg.m_strName = m_recordset.m_name ;
  232. Dlg.m_strBrief = m_recordset.m_brief ;
  233. Dlg.m_tmDate = m_recordset.m_date     ;
  234. Dlg.m_strLevel = m_recordset.m_level ;
  235. Dlg.m_strPerson = m_recordset.m_person ;
  236. Dlg.m_strType = m_recordset.m_type ;
  237. m_recordset.Close();
  238. if(IDOK==Dlg.DoModal())
  239. {//修改记录
  240. strSQL.Format("select * from scholarship where ID = %s",m_ctrList.GetItemText(i,0));
  241. if(!m_recordset.Open(AFX_DB_USE_DEFAULT_TYPE,strSQL))
  242. {
  243. AfxMessageBox("打开数据库失败!");
  244. return ;
  245. }
  246. m_recordset.Edit();
  247. m_recordset.m_name = Dlg.m_strName ;
  248. m_recordset.m_brief = Dlg.m_strBrief ;
  249. m_recordset.m_date =   Dlg.m_tmDate ;
  250. m_recordset.m_level = Dlg.m_strLevel ;
  251. m_recordset.m_person = Dlg.m_strPerson ;
  252. m_recordset.m_type = Dlg.m_strType ;
  253. m_recordset.Update();
  254. m_recordset.Close();
  255. strSQL = "select * from scholarship ";
  256. RefreshData(strSQL);
  257. }
  258. *pResult = 0;
  259. }