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

书籍源码

开发平台:

Visual C++

  1. // ODBCDepartmentView.cpp : implementation of the CODBCDepartmentView class
  2. //
  3. #include "stdafx.h"
  4. #include "ODBCDepartment.h"
  5. #include "ODBCDepartmentSet.h"
  6. #include "ODBCDepartmentDoc.h"
  7. #include "ODBCDepartmentView.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CODBCDepartmentView
  15. IMPLEMENT_DYNCREATE(CODBCDepartmentView, CRecordView)
  16. BEGIN_MESSAGE_MAP(CODBCDepartmentView, CRecordView)
  17. //{{AFX_MSG_MAP(CODBCDepartmentView)
  18. ON_COMMAND(ID_RECORD_ADDRECORD, OnRecordAddrecord)
  19. ON_COMMAND(ID_RECORD_DELETERECORD, OnRecordDeleterecord)
  20. ON_COMMAND(ID_RECORD_QUERYRECORD, OnRecordQueryrecord)
  21. ON_UPDATE_COMMAND_UI(ID_RECORD_DELETERECORD, OnUpdateRecordDeleterecord)
  22. //}}AFX_MSG_MAP
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CODBCDepartmentView construction/destruction
  26. CODBCDepartmentView::CODBCDepartmentView()
  27. : CRecordView(CODBCDepartmentView::IDD)
  28. {
  29. //{{AFX_DATA_INIT(CODBCDepartmentView)
  30. m_pSet = NULL;
  31. m_FindDeptCode = _T("");
  32. //}}AFX_DATA_INIT
  33. m_bAddingRecord = FALSE;
  34. }
  35. CODBCDepartmentView::~CODBCDepartmentView()
  36. {
  37. }
  38. void CODBCDepartmentView::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CRecordView::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CODBCDepartmentView)
  42. DDX_FieldText(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, m_pSet);
  43. DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
  44. DDX_FieldText(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, m_pSet);
  45. DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 50);
  46. DDX_Text(pDX, IDC_FINDCODE, m_FindDeptCode);
  47. DDV_MaxChars(pDX, m_FindDeptCode, 4);
  48. //}}AFX_DATA_MAP
  49. }
  50. BOOL CODBCDepartmentView::PreCreateWindow(CREATESTRUCT& cs)
  51. {
  52. return CRecordView::PreCreateWindow(cs);
  53. }
  54. void CODBCDepartmentView::OnInitialUpdate()
  55. {
  56. m_pSet = &GetDocument()->m_oDBCDepartmentSet;
  57. CRecordView::OnInitialUpdate();
  58. GetParentFrame()->RecalcLayout();
  59. ResizeParentToFit();
  60. }
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CODBCDepartmentView diagnostics
  63. #ifdef _DEBUG
  64. void CODBCDepartmentView::AssertValid() const
  65. {
  66. CRecordView::AssertValid();
  67. }
  68. void CODBCDepartmentView::Dump(CDumpContext& dc) const
  69. {
  70. CRecordView::Dump(dc);
  71. }
  72. CODBCDepartmentDoc* CODBCDepartmentView::GetDocument() // non-debug version is inline
  73. {
  74. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CODBCDepartmentDoc)));
  75. return (CODBCDepartmentDoc*)m_pDocument;
  76. }
  77. #endif //_DEBUG
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CODBCDepartmentView database support
  80. CRecordset* CODBCDepartmentView::OnGetRecordset()
  81. {
  82. return m_pSet;
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CODBCDepartmentView message handlers
  86. BOOL CODBCDepartmentView::OnMove(UINT nIDMoveCommand) 
  87. {
  88. if (m_bAddingRecord) { //Currently adding?
  89. //If so, Update the record before the move
  90. UpdateData(TRUE); //Get data from dialog box
  91. m_pSet->Update(); //Update data if needed
  92. m_pSet->MoveLast(); //Go to the added record
  93. m_bAddingRecord = FALSE; //Reset flag
  94. }
  95. //Continue with normal processing
  96. return CRecordView::OnMove(nIDMoveCommand);
  97. }
  98. void CODBCDepartmentView::OnRecordAddrecord() 
  99. {
  100. CRecordsetStatus rStatus; //Status variable
  101. m_pSet->GetStatus(rStatus); //Get CRecordset status
  102. if (rStatus.m_lCurrentRecord >= 0) { //Records Exist?
  103. UpdateData(TRUE); //Get data from dialog box
  104. if (!m_bAddingRecord) { //Currently adding?
  105. //If not, set CRecordset in edit mode for updating
  106. m_pSet->Edit();
  107. }
  108. m_pSet->Update(); //Update data if needed
  109. m_pSet->MoveLast(); //Get off record 1
  110. }
  111. m_bAddingRecord = TRUE; //Set flag
  112. m_pSet->SetFieldNull(NULL); //Clear all fields
  113. m_pSet->AddNew(); //Set database in AddNew mode
  114. UpdateData(FALSE); //Update dialog box fields
  115. }
  116. void CODBCDepartmentView::OnRecordDeleterecord() 
  117. {
  118. if (AfxMessageBox( //Be sure to verify your deletes
  119. "Are you sure you want to delete?",
  120. MB_YESNO)
  121. != IDYES) {
  122. return;
  123. }
  124. if (m_bAddingRecord) { //Currently adding?
  125. //Don't delete, just cancel add.
  126. m_pSet->CancelUpdate();
  127. m_bAddingRecord = FALSE;
  128. m_pSet->MovePrev();
  129. return;
  130. }
  131. try {
  132. m_pSet->Delete(); //Delete record
  133. }
  134. catch(CDBException* e1) { //Failed
  135. AfxMessageBox("Delete Failed:n" +
  136. e1->m_strError,
  137. MB_ICONEXCLAMATION);
  138. m_pSet->MoveFirst(); //We lost our place.
  139. e1->Delete(); //Delete Error Message
  140. UpdateData(FALSE); //Update dialog box fields
  141. return;
  142. }
  143. CRecordsetStatus rStatus; //Status variable
  144. m_pSet->GetStatus(rStatus); //Get CRecordset status
  145. try {
  146. m_pSet->MoveNext(); //Go to next record
  147. if (m_pSet->IsDeleted()) { //Was there a next record?
  148. m_pSet->MoveFirst(); //Deleted last record
  149. }
  150. if (m_pSet->IsDeleted()) { //Can't find a record
  151. AfxThrowDBException(SQL_ERROR, 
  152. m_pSet->m_pDatabase,
  153. m_pSet->m_hstmt);   
  154. }
  155. UpdateData(FALSE); //Update dialog box fields
  156. }
  157. catch(CDBException* e2) { //No records exist
  158. AfxMessageBox("No more records",
  159. MB_ICONEXCLAMATION);
  160. e2->Delete(); //Delete Error Message
  161. //Close and Open to get rid of the Deleted record
  162. m_pSet->Close();
  163. m_pSet->Open();
  164. //No records, so set up an add record
  165. OnRecordAddrecord();
  166. }
  167. }
  168. void CODBCDepartmentView::OnUpdateRecordDeleterecord(CCmdUI* pCmdUI) 
  169. {
  170. //Disable delete functionality if no record is found
  171. pCmdUI->Enable( //Enable delete if there's a record
  172. !m_pSet->IsBOF() &&
  173. !m_pSet->IsDeleted() &&
  174. !m_pSet->IsEOF());
  175. }
  176. void CODBCDepartmentView::OnRecordQueryrecord() 
  177. {
  178. CString newFilter = ""; //Default is no filter
  179. UpdateData(TRUE); //Get data from dialog box
  180. if (!m_bAddingRecord) { //Currently adding?
  181. //Set to update
  182. m_pSet->Edit();
  183. }
  184. m_pSet->Update(); //Update data if needed
  185. m_bAddingRecord = FALSE; //Reset flag
  186. if (m_FindDeptCode != "") {
  187. //Setup new filter
  188. newFilter = "DepartmentCode = '" + m_FindDeptCode + "'";
  189. }
  190. if (newFilter != m_pSet->m_strFilter) { //Filter has changed
  191. m_pSet->m_strFilter = newFilter; //Assign new filter
  192. if (!m_pSet->Requery()) { //Requery
  193. AfxMessageBox("Requery has failed"); //Error occurred
  194. m_pSet->m_strFilter = ""; //Try to get back
  195. m_pSet->Requery(); //Requery again
  196. }
  197. try {
  198. //Go to the first record of the new filtered recordset
  199. m_pSet->MoveFirst();
  200. }
  201. catch(CDBException* e)    {
  202. //Move failed because there are no records
  203. AfxMessageBox("No records were found", MB_ICONEXCLAMATION );
  204. e->Delete(); //Delete Error Message
  205. //No records, so set up an add record
  206. OnRecordAddrecord();
  207. }
  208. }
  209. UpdateData(FALSE); //Update dialog box fields
  210. }