MoneyDlg.cpp
上传用户:shcaka8
上传日期:2013-04-06
资源大小:103k
文件大小:4k
- // MoneyDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyAssistant.h"
- #include "MoneyDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMoneyEditDlg dialog
- CMoneyEditDlg::CMoneyEditDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMoneyEditDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMoneyEditDlg)
- m_strID = _T("");
- m_nSum = 0;
- m_nDay = 0;
- m_nMonth = 0;
- m_nYear = 0;
- m_strComments = _T("");
- //}}AFX_DATA_INIT
- }
- void CMoneyEditDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMoneyEditDlg)
- DDX_Control(pDX, IDC_UPDATE, m_btnUpdate);
- DDX_Control(pDX, IDC_TYPE, m_comboType);
- DDX_Text(pDX, IDC_ID, m_strID);
- DDX_Text(pDX, IDC_SUM, m_nSum);
- DDX_Text(pDX, IDC_DAY, m_nDay);
- DDX_Text(pDX, IDC_MONTH, m_nMonth);
- DDX_Text(pDX, IDC_YEAR, m_nYear);
- DDX_Text(pDX, IDC_COMMENTS, m_strComments);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMoneyEditDlg, CDialog)
- //{{AFX_MSG_MAP(CMoneyEditDlg)
- ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMoneyEditDlg message handlers
- void CMoneyEditDlg::OnOK()
- {
- // TODO: Add extra validation here
-
- // CDialog::OnOK();
- }
- void CMoneyEditDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
-
- // CDialog::OnCancel();
- }
- BOOL CMoneyEditDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // TODO: Add extra initialization here
- m_strID = GenerateID();
- m_comboType.AddString("饮食");
- m_comboType.AddString("住宿");
- m_comboType.AddString("衣服");
- m_comboType.AddString("交通");
- m_comboType.AddString("通信");
- m_comboType.AddString("书籍");
- m_comboType.AddString("培训");
- m_comboType.AddString("其它");
- m_comboType.SetCurSel(0);
- m_nSum = 0;
- CString strYear,strMonth,strDay;
- m_nYear = 2004;
- m_nMonth = CTime::GetCurrentTime().GetMonth();
- m_nDay = CTime::GetCurrentTime().GetDay();
- UpdateData(FALSE);
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- CString CMoneyEditDlg::GenerateID()
- {
- CString strID = "";
- CTime NOW = CTime::GetCurrentTime();
- int year = NOW.GetYear();
- int month = NOW.GetMonth();
- int day = NOW.GetDay();
- int hour = NOW.GetHour();
- int minute = NOW.GetMinute();
- int second = NOW.GetSecond();
- int date = year*100*100 + month*100 + day;
- int time = hour*100*100 + minute*100 + second;
- strID.Format("%d-%d",date,time);
- return strID;
- }
- void CMoneyEditDlg::OnUpdate()
- {
- UpdateData(TRUE);
- m_comboType.GetWindowText(m_strType);
- CTime p_Date(m_nYear,m_nMonth,m_nDay,0,0,0);
- m_Date = p_Date;
- if(!SaveToDb()) {
- MessageBox("提交失败!");
- }
- else {
- MessageBox("提交成功!");
- }
- }
- BOOL CMoneyEditDlg::SaveToDb()
- {
- p_Database = new CDatabase;
- p_Database->SetLoginTimeout(5);
- BOOL p_Status = FALSE;
- try
- {
- p_Status = p_Database->Open("Assistant");
- if(p_Status)
- TRACE("nDB opened successfullyn");
- else
- TRACE("nOpen DSN failed.n");
- }
- catch(CMemoryException* pEx)
- {
- pEx->ReportError();
- }
- catch(CDBException* pDBEx)
- {
- pDBEx->ReportError();
- TRACE("RetCOde:%d strError:[%d] strState: [%d]n",
- pDBEx->m_nRetCode,
- pDBEx->m_strError,
- pDBEx->m_strStateNativeOrigin);
- }
- if(!p_Status)
- {
- MessageBox("Open DSN Failed.");
- if(p_Database->IsOpen()) p_Database->Close();
- if(p_Database != NULL) delete p_Database;
- return FALSE;
- }
- p_Record = new CMoneyRecord(p_Database);
- if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"Account"))
- {
- MessageBox("Open Table SiteData Failed.");
- if(p_Record->IsOpen()) p_Record->Close();
- if(p_Database->IsOpen()) p_Database->Close();
- delete p_Record;
- delete p_Database;
- return FALSE;
- }
- p_Record->AddNew();
- p_Record->m_ID = GenerateID();
- p_Record->m_Type = m_strType;
- p_Record->m_Sum = m_nSum;
- p_Record->m_Date = m_Date;
- p_Record->m_Comments = m_strComments;
- p_Record->Update();
- if(p_Record->IsOpen()) p_Record->Close();
- if(p_Database->IsOpen()) p_Database->Close();
- delete p_Record;
- delete p_Database;
- return TRUE;
- }