MoneyDlg.cpp
上传用户:shcaka8
上传日期:2013-04-06
资源大小:103k
文件大小:4k
源码类别:

家庭/个人应用

开发平台:

Visual C++

  1. // MoneyDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyAssistant.h"
  5. #include "MoneyDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMoneyEditDlg dialog
  13. CMoneyEditDlg::CMoneyEditDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CMoneyEditDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CMoneyEditDlg)
  17. m_strID = _T("");
  18. m_nSum = 0;
  19. m_nDay = 0;
  20. m_nMonth = 0;
  21. m_nYear = 0;
  22. m_strComments = _T("");
  23. //}}AFX_DATA_INIT
  24. }
  25. void CMoneyEditDlg::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. //{{AFX_DATA_MAP(CMoneyEditDlg)
  29. DDX_Control(pDX, IDC_UPDATE, m_btnUpdate);
  30. DDX_Control(pDX, IDC_TYPE, m_comboType);
  31. DDX_Text(pDX, IDC_ID, m_strID);
  32. DDX_Text(pDX, IDC_SUM, m_nSum);
  33. DDX_Text(pDX, IDC_DAY, m_nDay);
  34. DDX_Text(pDX, IDC_MONTH, m_nMonth);
  35. DDX_Text(pDX, IDC_YEAR, m_nYear);
  36. DDX_Text(pDX, IDC_COMMENTS, m_strComments);
  37. //}}AFX_DATA_MAP
  38. }
  39. BEGIN_MESSAGE_MAP(CMoneyEditDlg, CDialog)
  40. //{{AFX_MSG_MAP(CMoneyEditDlg)
  41. ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CMoneyEditDlg message handlers
  46. void CMoneyEditDlg::OnOK() 
  47. {
  48. // TODO: Add extra validation here
  49. // CDialog::OnOK();
  50. }
  51. void CMoneyEditDlg::OnCancel() 
  52. {
  53. // TODO: Add extra cleanup here
  54. // CDialog::OnCancel();
  55. }
  56. BOOL CMoneyEditDlg::OnInitDialog() 
  57. {
  58. CDialog::OnInitDialog();
  59. // TODO: Add extra initialization here
  60. m_strID = GenerateID();
  61. m_comboType.AddString("饮食");
  62. m_comboType.AddString("住宿");
  63. m_comboType.AddString("衣服");
  64. m_comboType.AddString("交通");
  65. m_comboType.AddString("通信");
  66. m_comboType.AddString("书籍");
  67. m_comboType.AddString("培训");
  68. m_comboType.AddString("其它");
  69. m_comboType.SetCurSel(0);
  70. m_nSum = 0;
  71. CString strYear,strMonth,strDay;
  72. m_nYear = 2004;
  73. m_nMonth = CTime::GetCurrentTime().GetMonth();
  74. m_nDay = CTime::GetCurrentTime().GetDay();
  75. UpdateData(FALSE);
  76. return TRUE;  // return TRUE unless you set the focus to a control
  77.               // EXCEPTION: OCX Property Pages should return FALSE
  78. }
  79. CString CMoneyEditDlg::GenerateID()
  80. {
  81. CString strID = "";
  82. CTime NOW = CTime::GetCurrentTime();
  83. int year = NOW.GetYear();
  84. int month = NOW.GetMonth();
  85. int day = NOW.GetDay();
  86. int hour = NOW.GetHour();
  87. int minute = NOW.GetMinute();
  88. int second = NOW.GetSecond();
  89. int date = year*100*100 + month*100  + day;
  90. int time = hour*100*100 + minute*100 + second;
  91. strID.Format("%d-%d",date,time);
  92. return strID;
  93. }
  94. void CMoneyEditDlg::OnUpdate() 
  95. {
  96. UpdateData(TRUE);
  97. m_comboType.GetWindowText(m_strType);
  98. CTime p_Date(m_nYear,m_nMonth,m_nDay,0,0,0);
  99. m_Date = p_Date;
  100. if(!SaveToDb()) {
  101. MessageBox("提交失败!");
  102. }
  103. else {
  104. MessageBox("提交成功!");
  105. }
  106. }
  107. BOOL CMoneyEditDlg::SaveToDb()
  108. {
  109. p_Database = new CDatabase;
  110. p_Database->SetLoginTimeout(5);
  111. BOOL p_Status = FALSE;
  112. try
  113. {
  114. p_Status = p_Database->Open("Assistant");
  115. if(p_Status)
  116. TRACE("nDB opened successfullyn");
  117. else
  118. TRACE("nOpen DSN failed.n");
  119. }
  120. catch(CMemoryException* pEx)
  121. {
  122. pEx->ReportError();
  123. }
  124. catch(CDBException* pDBEx)
  125. {
  126. pDBEx->ReportError();
  127. TRACE("RetCOde:%d strError:[%d] strState: [%d]n",
  128. pDBEx->m_nRetCode,
  129. pDBEx->m_strError,
  130. pDBEx->m_strStateNativeOrigin);
  131. }
  132. if(!p_Status)
  133. {
  134. MessageBox("Open DSN Failed.");
  135. if(p_Database->IsOpen()) p_Database->Close();
  136. if(p_Database != NULL) delete p_Database;
  137. return FALSE;
  138. }
  139. p_Record = new CMoneyRecord(p_Database);
  140. if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"Account"))
  141. {
  142. MessageBox("Open Table SiteData Failed.");
  143. if(p_Record->IsOpen()) p_Record->Close();
  144. if(p_Database->IsOpen()) p_Database->Close();
  145. delete p_Record;
  146. delete p_Database;
  147. return FALSE;
  148. }
  149. p_Record->AddNew();
  150. p_Record->m_ID = GenerateID();
  151. p_Record->m_Type = m_strType;
  152. p_Record->m_Sum = m_nSum;
  153. p_Record->m_Date = m_Date;
  154. p_Record->m_Comments = m_strComments;
  155. p_Record->Update();
  156. if(p_Record->IsOpen()) p_Record->Close();
  157. if(p_Database->IsOpen()) p_Database->Close();
  158. delete p_Record;
  159. delete p_Database;
  160. return TRUE;
  161. }