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

家庭/个人应用

开发平台:

Visual C++

  1. // AddrListDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyAssistant.h"
  5. #include "AddrListDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAddrListDlg dialog
  13. CAddrListDlg::CAddrListDlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CAddrListDlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CAddrListDlg)
  17. m_strName = _T("");
  18. m_nAge = 0;
  19. m_strMobilPhone = _T("");
  20. m_strMemory = _T("");
  21. m_strTelePhone = _T("");
  22. //}}AFX_DATA_INIT
  23. }
  24. void CAddrListDlg::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialog::DoDataExchange(pDX);
  27. //{{AFX_DATA_MAP(CAddrListDlg)
  28. DDX_Control(pDX, IDC_RELATION, m_comboRelation);
  29. DDX_Control(pDX, IDC_SEX, m_comboSex);
  30. DDX_Text(pDX, IDC_NAME, m_strName);
  31. DDX_Text(pDX, IDC_AGE, m_nAge);
  32. DDX_Text(pDX, IDC_MOBILPHONE, m_strMobilPhone);
  33. DDX_Text(pDX, IDC_MEMORY, m_strMemory);
  34. DDX_Text(pDX, IDC_TELEPHONE, m_strTelePhone);
  35. //}}AFX_DATA_MAP
  36. }
  37. BEGIN_MESSAGE_MAP(CAddrListDlg, CDialog)
  38. //{{AFX_MSG_MAP(CAddrListDlg)
  39. ON_BN_CLICKED(IDC_QUERY, OnQuery)
  40. ON_BN_CLICKED(IDC_UPDATE, OnUpdate)
  41. //}}AFX_MSG_MAP
  42. END_MESSAGE_MAP()
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CAddrListDlg message handlers
  45. void CAddrListDlg::OnOK() 
  46. {
  47. // TODO: Add extra validation here
  48. // CDialog::OnOK();
  49. }
  50. void CAddrListDlg::OnCancel() 
  51. {
  52. // TODO: Add extra cleanup here
  53. // CDialog::OnCancel();
  54. }
  55. BOOL CAddrListDlg::OnInitDialog() 
  56. {
  57. CDialog::OnInitDialog();
  58. // TODO: Add extra initialization here
  59. m_nAge = 23;
  60. m_comboSex.AddString("男");
  61. m_comboSex.AddString("女");
  62. m_comboSex.SetCurSel(0);
  63. m_comboRelation.AddString("朋友");
  64. m_comboRelation.AddString("同学");
  65. m_comboRelation.AddString("同事");
  66. m_comboRelation.AddString("亲属");
  67. m_comboRelation.AddString("其它");
  68. m_comboRelation.SetCurSel(0);
  69. UpdateData(FALSE);
  70. return TRUE;  // return TRUE unless you set the focus to a control
  71.               // EXCEPTION: OCX Property Pages should return FALSE
  72. }
  73. void CAddrListDlg::OnQuery() 
  74. {
  75. UpdateData();
  76. m_strFilter = "Name = '" + m_strName + "'";
  77. QueryFromDb();
  78. }
  79. void CAddrListDlg::OnUpdate() 
  80. {
  81. UpdateData();
  82. m_comboSex.GetWindowText(m_strSex);
  83. m_comboRelation.GetWindowText(m_strRelation);
  84. if(!SaveToDb()) {
  85. MessageBox("提交失败!");
  86. }
  87. else {
  88. MessageBox("提交成功!");
  89. }
  90. }
  91. BOOL CAddrListDlg::SaveToDb()
  92. {
  93. p_Database = new CDatabase;
  94. p_Database->SetLoginTimeout(5);
  95. BOOL p_Status = FALSE;
  96. try
  97. {
  98. p_Status = p_Database->Open("Assistant");
  99. if(p_Status)
  100. TRACE("nDB opened successfullyn");
  101. else
  102. TRACE("nOpen DSN failed.n");
  103. }
  104. catch(CMemoryException* pEx)
  105. {
  106. pEx->ReportError();
  107. }
  108. catch(CDBException* pDBEx)
  109. {
  110. pDBEx->ReportError();
  111. TRACE("RetCOde:%d strError:[%d] strState: [%d]n",
  112. pDBEx->m_nRetCode,
  113. pDBEx->m_strError,
  114. pDBEx->m_strStateNativeOrigin);
  115. }
  116. if(!p_Status)
  117. {
  118. MessageBox("Open DSN Failed.");
  119. if(p_Database->IsOpen()) p_Database->Close();
  120. if(p_Database != NULL) delete p_Database;
  121. return FALSE;
  122. }
  123. p_Record = new CAddrListRecord(p_Database);
  124. if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"AddrList"))
  125. {
  126. MessageBox("Open Table SiteData Failed.");
  127. if(p_Record->IsOpen()) p_Record->Close();
  128. if(p_Database->IsOpen()) p_Database->Close();
  129. delete p_Record;
  130. delete p_Database;
  131. return FALSE;
  132. }
  133. p_Record->AddNew();
  134. p_Record->m_Name = m_strName;
  135. p_Record->m_Sex = m_strSex;
  136. p_Record->m_Age = m_nAge;
  137. p_Record->m_Relation = m_strRelation;
  138. p_Record->m_TelePhone = m_strTelePhone;
  139. p_Record->m_MobilPhone = m_strMobilPhone;
  140. // p_Record->m_Address = m_strAddress;
  141. p_Record->m_Comments = m_strMemory;
  142. p_Record->Update();
  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 TRUE;
  148. }
  149. BOOL CAddrListDlg::QueryFromDb()
  150. {
  151. p_Database = new CDatabase;
  152. p_Database->SetLoginTimeout(5);
  153. BOOL p_Status = FALSE;
  154. try
  155. {
  156. p_Status = p_Database->Open("Assistant");
  157. if(p_Status)
  158. TRACE("nDB opened successfullyn");
  159. else
  160. TRACE("nOpen DSN failed.n");
  161. }
  162. catch(CMemoryException* pEx)
  163. {
  164. pEx->ReportError();
  165. }
  166. catch(CDBException* pDBEx)
  167. {
  168. pDBEx->ReportError();
  169. TRACE("RetCOde:%d strError:[%d] strState: [%d]n",
  170. pDBEx->m_nRetCode,
  171. pDBEx->m_strError,
  172. pDBEx->m_strStateNativeOrigin);
  173. }
  174. if(!p_Status)
  175. {
  176. MessageBox("Open DSN Failed.");
  177. if(p_Database->IsOpen()) p_Database->Close();
  178. if(p_Database != NULL) delete p_Database;
  179. return FALSE;
  180. }
  181. p_Record = new CAddrListRecord(p_Database);
  182. p_Record->m_strFilter = m_strFilter;
  183. if(!p_Record->Open(AFX_DB_USE_DEFAULT_TYPE,(LPCTSTR)"AddrList"))
  184. {
  185. MessageBox("Open Table SiteData Failed.");
  186. if(p_Record->IsOpen()) p_Record->Close();
  187. if(p_Database->IsOpen()) p_Database->Close();
  188. delete p_Record;
  189. delete p_Database;
  190. return FALSE;
  191. }
  192. CString str;
  193. if(!p_Record->IsBOF())
  194. {
  195. m_strName = p_Record->m_Name;
  196. m_strSex = p_Record->m_Sex;
  197. m_nAge = p_Record->m_Age;
  198. m_strRelation = p_Record->m_Relation;
  199. m_strTelePhone = p_Record->m_TelePhone;
  200. m_strMobilPhone = p_Record->m_MobilPhone;
  201. // m_strAddress = p_Record->m_Address;
  202. m_strMemory = p_Record->m_Comments;
  203. UpdateData(FALSE);
  204. }
  205. //
  206. if(p_Record->IsOpen()) p_Record->Close();
  207. if(p_Database->IsOpen()) p_Database->Close();
  208. delete p_Record;
  209. delete p_Database;
  210. return TRUE;
  211. }