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