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

书籍源码

开发平台:

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