InhabitantsDoc.cpp
上传用户:yuxuan88
上传日期:2022-05-09
资源大小:2290k
文件大小:4k
源码类别:

行业应用

开发平台:

Visual C++

  1. // InhabitantsDoc.cpp : implementation of the CInhabitantsDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "Inhabitants.h"
  5. #include "InhabitantsDoc.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CInhabitantsDoc
  13. IMPLEMENT_DYNCREATE(CInhabitantsDoc, CDocument)
  14. BEGIN_MESSAGE_MAP(CInhabitantsDoc, CDocument)
  15. //{{AFX_MSG_MAP(CInhabitantsDoc)
  16. // NOTE - the ClassWizard will add and remove mapping macros here.
  17. //    DO NOT EDIT what you see in these blocks of generated code!
  18. //}}AFX_MSG_MAP
  19. END_MESSAGE_MAP()
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CInhabitantsDoc construction/destruction
  22. CInhabitantsDoc::CInhabitantsDoc()
  23. {
  24. // TODO: add one-time construction code here
  25. }
  26. CInhabitantsDoc::~CInhabitantsDoc()
  27. {
  28. }
  29. BOOL CInhabitantsDoc::OnNewDocument()
  30. {
  31. if (!CDocument::OnNewDocument())
  32. return FALSE;
  33. //打开数据库
  34. HRESULT result = m_dbHouse.Open();
  35. if(FAILED(result))
  36. AfxMessageBox("open database failed!");
  37. //设置程序标题
  38. SetTitle("小区居民管理系统");
  39. return TRUE;
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CInhabitantsDoc serialization
  43. void CInhabitantsDoc::Serialize(CArchive& ar)
  44. {
  45. if (ar.IsStoring())
  46. {
  47. // TODO: add storing code here
  48. }
  49. else
  50. {
  51. // TODO: add loading code here
  52. }
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CInhabitantsDoc diagnostics
  56. #ifdef _DEBUG
  57. void CInhabitantsDoc::AssertValid() const
  58. {
  59. CDocument::AssertValid();
  60. }
  61. void CInhabitantsDoc::Dump(CDumpContext& dc) const
  62. {
  63. CDocument::Dump(dc);
  64. }
  65. #endif //_DEBUG
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CInhabitantsDoc commands
  68. int CInhabitantsDoc::AddUser(USER &user)
  69. { /*返回值: 0 已经存在
  70. -1 添加失败
  71. 1 添加成功
  72. */
  73. //判断是否存在该住户了
  74. CString strSql;
  75. strSql.Format("select * from house where sectionname = '%s' and buildingnum = %d and cellnum = %d and roomnum = %d",
  76. user.strSectionname,user.nBuildingnum,user.nCellnum,user.nRoomnum);
  77. CCommand<CAccessor<CHouseAccessor> > dbHouse;
  78. long* pCount = new long;
  79. if(dbHouse.Open(m_dbHouse.m_session,strSql,NULL,pCount) != S_OK)
  80. {
  81. AfxMessageBox("error");
  82. delete pCount;
  83. return -1;
  84. }
  85. if(dbHouse.MoveFirst() == S_OK)
  86. {
  87. delete pCount;
  88. dbHouse.Close();
  89. return 0;
  90. }
  91. delete pCount;
  92. dbHouse.Close();
  93. //增加住户到数据库中
  94. m_dbHouse.MoveLast();
  95. _tcscpy( m_dbHouse.m_sectionname,user.strSectionname );
  96. m_dbHouse.m_buildingnum = user.nBuildingnum;
  97. m_dbHouse.m_cellnum = user.nCellnum;
  98. m_dbHouse.m_roomnum = user.nRoomnum;
  99. _tcscpy(m_dbHouse.m_housemaster,user.strName);
  100. _tcscpy(m_dbHouse.m_beeppager,user.strBeeppager);
  101. _tcscpy(m_dbHouse.m_email,user.strEmail);
  102. _tcscpy(m_dbHouse.m_housetel,user.strHouseTel);
  103. _tcscpy(m_dbHouse.m_mobile,user.strMobile);
  104. _tcscpy(m_dbHouse.m_office,user.strOffice);
  105. _tcscpy(m_dbHouse.m_officetel,user.strOfficeTel);
  106. HRESULT hResult = m_dbHouse.Insert(); 
  107. if( FAILED( hResult ) )
  108. {
  109. AfxMessageBox( _T( "Error inserting the current record" ) );
  110. return -1;
  111. }
  112. return 1;
  113. }
  114. //删除符合条件的住户
  115. BOOL CInhabitantsDoc::DeleteUser(CString strSql)
  116. {
  117. CCommand<CAccessor<CHouseAccessor> > dbHouse;
  118. long* pCount = new long;
  119. if(dbHouse.Open(m_dbHouse.m_session,strSql,
  120. NULL,pCount,DBGUID_DEFAULT,FALSE) != S_OK)
  121. {
  122. AfxMessageBox("error");
  123. delete pCount;
  124. return FALSE;
  125. }
  126. delete pCount;
  127. dbHouse.Close();
  128. return TRUE;
  129. }
  130. void CInhabitantsDoc::DeleteContents() 
  131. {
  132. //关闭数据库
  133. m_dbHouse.Close();
  134. m_dbHouse.m_session.Close();
  135. CDocument::DeleteContents();
  136. }