AddrListDlg.cpp
上传用户:shcaka8
上传日期:2013-04-06
资源大小:103k
文件大小:6k
- // AddrListDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyAssistant.h"
- #include "AddrListDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAddrListDlg dialog
- CAddrListDlg::CAddrListDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CAddrListDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CAddrListDlg)
- m_strName = _T("");
- m_nAge = 0;
- m_strMobilPhone = _T("");
- m_strMemory = _T("");
- m_strTelePhone = _T("");
- //}}AFX_DATA_INIT
- }
- void CAddrListDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAddrListDlg)
- DDX_Control(pDX, IDC_RELATION, m_comboRelation);
- DDX_Control(pDX, IDC_SEX, m_comboSex);
- DDX_Text(pDX, IDC_NAME, m_strName);
- DDX_Text(pDX, IDC_AGE, m_nAge);
- DDX_Text(pDX, IDC_MOBILPHONE, m_strMobilPhone);
- DDX_Text(pDX, IDC_MEMORY, m_strMemory);
- DDX_Text(pDX, IDC_TELEPHONE, m_strTelePhone);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAddrListDlg, CDialog)
- //{{AFX_MSG_MAP(CAddrListDlg)
- ON_BN_CLICKED(IDC_QUERY, OnQuery)
- ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CAddrListDlg message handlers
- void CAddrListDlg::OnOK()
- {
- // TODO: Add extra validation here
-
- // CDialog::OnOK();
- }
- void CAddrListDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
-
- // CDialog::OnCancel();
- }
- BOOL CAddrListDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // TODO: Add extra initialization here
- m_nAge = 23;
- m_comboSex.AddString("男");
- m_comboSex.AddString("女");
- m_comboSex.SetCurSel(0);
- m_comboRelation.AddString("朋友");
- m_comboRelation.AddString("同学");
- m_comboRelation.AddString("同事");
- m_comboRelation.AddString("亲属");
- m_comboRelation.AddString("其它");
- m_comboRelation.SetCurSel(0);
- UpdateData(FALSE);
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CAddrListDlg::OnQuery()
- {
- UpdateData();
- m_strFilter = "Name = '" + m_strName + "'";
- QueryFromDb();
- }
- void CAddrListDlg::OnUpdate()
- {
- UpdateData();
- m_comboSex.GetWindowText(m_strSex);
- m_comboRelation.GetWindowText(m_strRelation);
-
- if(!SaveToDb()) {
- MessageBox("提交失败!");
- }
- else {
- MessageBox("提交成功!");
- }
- }
- BOOL CAddrListDlg::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 CAddrListRecord(p_Database);
- if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"AddrList"))
- {
- 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_Name = m_strName;
- p_Record->m_Sex = m_strSex;
- p_Record->m_Age = m_nAge;
- p_Record->m_Relation = m_strRelation;
- p_Record->m_TelePhone = m_strTelePhone;
- p_Record->m_MobilPhone = m_strMobilPhone;
- // p_Record->m_Address = m_strAddress;
- p_Record->m_Comments = m_strMemory;
- 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;
- }
- BOOL CAddrListDlg::QueryFromDb()
- {
- 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 CAddrListRecord(p_Database);
- p_Record->m_strFilter = m_strFilter;
- if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"AddrList"))
- {
- 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;
- }
- CString str;
- if(!p_Record->IsBOF())
- {
- m_strName = p_Record->m_Name;
- m_strSex = p_Record->m_Sex;
- m_nAge = p_Record->m_Age;
- m_strRelation = p_Record->m_Relation;
- m_strTelePhone = p_Record->m_TelePhone;
- m_strMobilPhone = p_Record->m_MobilPhone;
- // m_strAddress = p_Record->m_Address;
- m_strMemory = p_Record->m_Comments;
- UpdateData(FALSE);
- }
- //
- if(p_Record->IsOpen()) p_Record->Close();
- if(p_Database->IsOpen()) p_Database->Close();
- delete p_Record;
- delete p_Database;
- return TRUE;
- }