OLEDBDepartmentMFCView.cpp
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:6k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // OLEDBDepartmentMFCView.cpp : implementation of the COLEDBDepartmentMFCView class
  2. //
  3. #include "stdafx.h"
  4. #include "OLEDBDepartmentMFC.h"
  5. #include "OLEDBDepartmentMFCSet.h"
  6. #include "OLEDBDepartmentMFCDoc.h"
  7. #include "OLEDBDepartmentMFCView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. //Added by Chuck Wood for query dialog support
  14. #include "OpenDepartment.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. // COLEDBDepartmentMFCView
  17. IMPLEMENT_DYNCREATE(COLEDBDepartmentMFCView, COleDBRecordView)
  18. BEGIN_MESSAGE_MAP(COLEDBDepartmentMFCView, COleDBRecordView)
  19. //{{AFX_MSG_MAP(COLEDBDepartmentMFCView)
  20. ON_WM_DESTROY()
  21. ON_COMMAND(ID_EDIT_DELETETCTRLDEL, OnEditDeletetctrldel)
  22. ON_COMMAND(ID_FILE_NEW, OnFileNew)
  23. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // COLEDBDepartmentMFCView construction/destruction
  28. COLEDBDepartmentMFCView::COLEDBDepartmentMFCView()
  29. : COleDBRecordView(COLEDBDepartmentMFCView::IDD)
  30. {
  31. //{{AFX_DATA_INIT(COLEDBDepartmentMFCView)
  32. // NOTE: the ClassWizard will add member initialization here
  33. m_pSet = NULL;
  34. //}}AFX_DATA_INIT
  35. m_bAddingRecord = FALSE;
  36. }
  37. COLEDBDepartmentMFCView::~COLEDBDepartmentMFCView()
  38. {
  39. }
  40. void COLEDBDepartmentMFCView::DoDataExchange(CDataExchange* pDX)
  41. {
  42. COleDBRecordView::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(COLEDBDepartmentMFCView)
  44. // NOTE: the ClassWizard will add DDX and DDV calls here
  45. //}}AFX_DATA_MAP
  46. DDX_Text(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, 5);
  47. DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
  48. DDX_Text(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, 51);
  49. DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 51);
  50. }
  51. BOOL COLEDBDepartmentMFCView::PreCreateWindow(CREATESTRUCT& cs)
  52. {
  53. return COleDBRecordView::PreCreateWindow(cs);
  54. }
  55. void COLEDBDepartmentMFCView::OnInitialUpdate()
  56. {
  57. m_pSet = &GetDocument()->m_oLEDBDepartmentMFCSet;
  58. {
  59. CWaitCursor wait;
  60. HRESULT hr = m_pSet->Open();
  61. if (hr != S_OK)
  62. {
  63. AfxMessageBox(_T("Record set failed to open."), MB_OK);
  64. // Disable the Next and Previous record commands,
  65. // since attempting to change the current record without an
  66. // open RecordSet will cause a crash.
  67. m_bOnFirstRecord = TRUE;
  68. m_bOnLastRecord = TRUE;
  69. }
  70. }
  71. COleDBRecordView::OnInitialUpdate();
  72. }
  73. /////////////////////////////////////////////////////////////////////////////
  74. // COLEDBDepartmentMFCView diagnostics
  75. #ifdef _DEBUG
  76. void COLEDBDepartmentMFCView::AssertValid() const
  77. {
  78. COleDBRecordView::AssertValid();
  79. }
  80. void COLEDBDepartmentMFCView::Dump(CDumpContext& dc) const
  81. {
  82. COleDBRecordView::Dump(dc);
  83. }
  84. COLEDBDepartmentMFCDoc* COLEDBDepartmentMFCView::GetDocument() // non-debug version is inline
  85. {
  86. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COLEDBDepartmentMFCDoc)));
  87. return (COLEDBDepartmentMFCDoc*)m_pDocument;
  88. }
  89. #endif //_DEBUG
  90. /////////////////////////////////////////////////////////////////////////////
  91. // COLEDBDepartmentMFCView database support
  92. CRowset* COLEDBDepartmentMFCView::OnGetRowset()
  93. {
  94. return m_pSet;
  95. }
  96. /////////////////////////////////////////////////////////////////////////////
  97. // COLEDBDepartmentMFCView message handlers
  98. BOOL COLEDBDepartmentMFCView::OnMove(UINT nIDMoveCommand) 
  99. {
  100. SaveRecord();
  101. return COleDBRecordView::OnMove(nIDMoveCommand);
  102. }
  103. void COLEDBDepartmentMFCView::OnDestroy() 
  104. {
  105. SaveRecord();
  106. COleDBRecordView::OnDestroy();
  107. }
  108. void COLEDBDepartmentMFCView::OnEditDeletetctrldel() 
  109. {
  110. if (AfxMessageBox( //Be sure to verify your deletes
  111. "Are you sure you want to delete?",
  112. MB_YESNO)
  113. != IDYES) {
  114. return;
  115. }
  116. //Delete record and test
  117. if (m_bAddingRecord) {
  118. //Just abort the add
  119. m_bAddingRecord = FALSE;
  120. }
  121. else if (FAILED(m_pSet->Delete())) {
  122. AfxMessageBox("Delete Failed:n", MB_ICONEXCLAMATION);
  123. }
  124. ResetRowSet();
  125. }
  126. void COLEDBDepartmentMFCView::OnFileNew() 
  127. {
  128. SaveRecord();
  129. AddRecord();
  130. }
  131. void COLEDBDepartmentMFCView::OnFileOpen() 
  132. {
  133. COpenDepartment od; //Query dialog box declaration
  134. SaveRecord(); //Save any changes first
  135. if (od.DoModal() == IDOK) { //OK was clicked?
  136. CString newFilter; //Define a new filter
  137. if (od.m_strDepartment > "") { //Anything entered?
  138. //Fill the new filter with a WHERE clause
  139. newFilter =
  140. " WHERE Department.DepartmentCode = '" +
  141. od.m_strDepartment +"'";
  142. }
  143. else {
  144. //Empty out the filter if nothing entered
  145. newFilter = "";
  146. }
  147. if (m_pSet->m_strFilter.CompareNoCase(newFilter)) {
  148. //Strings are different
  149. m_pSet->m_strFilter = newFilter;
  150. //Close and open to requery
  151. m_pSet->Close();
  152. m_pSet->Open();
  153. ResetRowSet(); //Display results
  154. }
  155. }
  156. }
  157. void COLEDBDepartmentMFCView::AddRecord()
  158. {
  159. m_bAddingRecord = TRUE;
  160. m_pSet->ClearFields();
  161. UpdateData(FALSE); // Show cleared values on window
  162. }
  163. BOOL COLEDBDepartmentMFCView::SaveRecord()
  164. {
  165. if (!UpdateData(TRUE)) { //Save changes from the screen
  166. return FALSE;
  167. }
  168. if (m_bAddingRecord) {
  169. m_bAddingRecord = FALSE;
  170. if (FAILED(m_pSet->Insert())) {
  171. AfxMessageBox("Insert failed",
  172. MB_ICONEXCLAMATION);
  173. m_pSet->MoveFirst();
  174. UpdateData(FALSE); // Show cleared values on window
  175. return FALSE;
  176. }
  177. }
  178. else if (FAILED(m_pSet->SetData(0))) { //Save changes from this accessor
  179. AfxMessageBox("Update via SetData failed",
  180. MB_ICONEXCLAMATION);
  181. UpdateData(FALSE); // Show cleared values on window
  182. return FALSE;
  183. }
  184. return TRUE;
  185. }
  186. void COLEDBDepartmentMFCView::ResetRowSet()
  187. {
  188. if (m_pSet->MoveNext() != S_OK) { //Go to next record
  189. if (m_pSet->MoveFirst() != S_OK) {
  190. AfxMessageBox("No more records",
  191. MB_ICONEXCLAMATION);
  192. //No records, so set up an add record
  193. AddRecord();
  194. }
  195. }
  196. UpdateData(FALSE); //Write changes to window
  197. }