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

书籍源码

开发平台:

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_DELETERECORD, OnRecordDeleterecord)
  19. ON_UPDATE_COMMAND_UI(ID_RECORD_DELETERECORD, OnUpdateRecordDeleterecord)
  20. ON_COMMAND(ID_RECORD_QUERYRECORD, OnRecordQueryrecord)
  21. ON_COMMAND(ID_FILE_NEW, OnFileNew)
  22. ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  23. ON_WM_DESTROY()
  24. ON_EN_CHANGE(IDC_DEPARTMENTCODE, OnChangeDepartmentcode)
  25. ON_EN_CHANGE(IDC_DEPARTMENTNAME, OnChangeDepartmentname)
  26. ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, OnUpdateFileSave)
  27. //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CODBCDepartmentView construction/destruction
  31. CODBCDepartmentView::CODBCDepartmentView()
  32. : CRecordView(CODBCDepartmentView::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CODBCDepartmentView)
  35. m_pSet = NULL;
  36. m_FindDeptCode = _T("");
  37. //}}AFX_DATA_INIT
  38. m_bAddingRecord = FALSE;
  39. m_bFieldsChanged = FALSE;
  40. }
  41. CODBCDepartmentView::~CODBCDepartmentView()
  42. {
  43. }
  44. void CODBCDepartmentView::DoDataExchange(CDataExchange* pDX)
  45. {
  46. CRecordView::DoDataExchange(pDX);
  47. //{{AFX_DATA_MAP(CODBCDepartmentView)
  48. DDX_FieldText(pDX, IDC_DEPARTMENTCODE, m_pSet->m_DepartmentCode, m_pSet);
  49. DDV_MaxChars(pDX, m_pSet->m_DepartmentCode, 4);
  50. DDX_FieldText(pDX, IDC_DEPARTMENTNAME, m_pSet->m_DepartmentName, m_pSet);
  51. DDV_MaxChars(pDX, m_pSet->m_DepartmentName, 50);
  52. DDX_Text(pDX, IDC_FINDCODE, m_FindDeptCode);
  53. DDV_MaxChars(pDX, m_FindDeptCode, 4);
  54. //}}AFX_DATA_MAP
  55. }
  56. BOOL CODBCDepartmentView::PreCreateWindow(CREATESTRUCT& cs)
  57. {
  58. return CRecordView::PreCreateWindow(cs);
  59. }
  60. void CODBCDepartmentView::OnInitialUpdate()
  61. {
  62. m_pSet = &GetDocument()->m_oDBCDepartmentSet;
  63. CRecordView::OnInitialUpdate();
  64. GetParentFrame()->RecalcLayout();
  65. ResizeParentToFit();
  66. m_pSet->Close(); //Close to start transaction
  67. if (m_pSet->m_pDatabase->CanTransact()) {
  68. m_pSet->m_pDatabase->BeginTrans();
  69. }
  70. m_pSet->Open(); //Open after starting transaction
  71. }
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CODBCDepartmentView diagnostics
  74. #ifdef _DEBUG
  75. void CODBCDepartmentView::AssertValid() const
  76. {
  77. CRecordView::AssertValid();
  78. }
  79. void CODBCDepartmentView::Dump(CDumpContext& dc) const
  80. {
  81. CRecordView::Dump(dc);
  82. }
  83. CODBCDepartmentDoc* CODBCDepartmentView::GetDocument() // non-debug version is inline
  84. {
  85. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CODBCDepartmentDoc)));
  86. return (CODBCDepartmentDoc*)m_pDocument;
  87. }
  88. #endif //_DEBUG
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CODBCDepartmentView database support
  91. CRecordset* CODBCDepartmentView::OnGetRecordset()
  92. {
  93. return m_pSet;
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CODBCDepartmentView message handlers
  97. BOOL CODBCDepartmentView::OnMove(UINT nIDMoveCommand) 
  98. {
  99. if (m_bAddingRecord) { //Currently adding?
  100. //If so, Update the record before the move
  101. UpdateData(TRUE); //Get data from dialog box
  102. m_pSet->Update(); //Update data if needed
  103. m_pSet->MoveLast(); //Go to the added record
  104. m_bAddingRecord = FALSE; //Reset flag
  105. }
  106. //Continue with normal processing
  107. return CRecordView::OnMove(nIDMoveCommand);
  108. }
  109. void CODBCDepartmentView::OnRecordDeleterecord() 
  110. {
  111. if (AfxMessageBox( //Be sure to verify your deletes
  112. "Are you sure you want to delete?",
  113. MB_YESNO)
  114. != IDYES) {
  115. return;
  116. }
  117. if (m_bAddingRecord) { //Currently adding?
  118. //Don't delete, just cancel add.
  119. m_pSet->CancelUpdate();
  120. m_bAddingRecord = FALSE;
  121. m_pSet->MovePrev();
  122. return;
  123. }
  124. try {
  125. m_pSet->Delete(); //Delete record
  126. }
  127. catch(CDBException* e1) { //Failed
  128. AfxMessageBox("Delete Failed:n" +
  129. e1->m_strError,
  130. MB_ICONEXCLAMATION);
  131. m_pSet->MoveFirst(); //We lost our place.
  132. e1->Delete(); //Delete Error Message
  133. UpdateData(FALSE); //Update dialog box fields
  134. return;
  135. }
  136. CRecordsetStatus rStatus; //Status variable
  137. m_pSet->GetStatus(rStatus); //Get CRecordset status
  138. try {
  139. m_pSet->MoveNext(); //Go to next record
  140. if (m_pSet->IsDeleted()) { //Was there a next record?
  141. m_pSet->MoveFirst(); //Deleted last record
  142. }
  143. if (m_pSet->IsDeleted()) { //Can't find a record
  144. AfxThrowDBException(SQL_ERROR, 
  145. m_pSet->m_pDatabase,
  146. m_pSet->m_hstmt);   
  147. }
  148. UpdateData(FALSE); //Update dialog box fields
  149. }
  150. catch(CDBException* e2) { //No records exist
  151. AfxMessageBox("No more records",
  152. MB_ICONEXCLAMATION);
  153. e2->Delete(); //Delete Error Message
  154. //Close and Open to get rid of the Deleted record
  155. m_pSet->Close();
  156. m_pSet->Open();
  157. //No records, so set up an add record
  158. OnFileNew();
  159. }
  160. }
  161. void CODBCDepartmentView::OnUpdateRecordDeleterecord(CCmdUI* pCmdUI) 
  162. {
  163. //Disable delete functionality if no record is found
  164. pCmdUI->Enable( //Enable delete if there's a record
  165. !m_pSet->IsBOF() &&
  166. !m_pSet->IsDeleted() &&
  167. !m_pSet->IsEOF());
  168. }
  169. void CODBCDepartmentView::OnRecordQueryrecord() 
  170. {
  171. CString newFilter = ""; //Default is no filter
  172. UpdateData(TRUE); //Get data from dialog box
  173. if (!m_bAddingRecord) { //Currently adding?
  174. //Set to update
  175. m_pSet->Edit();
  176. }
  177. m_pSet->Update(); //Update data if needed
  178. m_bAddingRecord = FALSE; //Reset flag
  179. if (m_FindDeptCode != "") {
  180. //Setup new filter
  181. newFilter = "DepartmentCode = '" + m_FindDeptCode + "'";
  182. }
  183. if (newFilter != m_pSet->m_strFilter) { //Filter has changed
  184. m_pSet->m_strFilter = newFilter; //Assign new filter
  185. if (!m_pSet->Requery()) { //Requery
  186. AfxMessageBox("Requery has failed"); //Error occurred
  187. m_pSet->m_strFilter = ""; //Try to get back
  188. m_pSet->Requery(); //Requery again
  189. }
  190. try {
  191. //Go to the first record of the new filtered recordset
  192. m_pSet->MoveFirst();
  193. }
  194. catch(CDBException* e)    {
  195. //Move failed because there are no records
  196. AfxMessageBox("No records were found", MB_ICONEXCLAMATION );
  197. e->Delete(); //Delete Error Message
  198. //No records, so set up an add record
  199. OnFileNew();
  200. }
  201. }
  202. UpdateData(FALSE); //Update dialog box fields
  203. }
  204. void CODBCDepartmentView::OnFileNew() 
  205. {
  206. CRecordsetStatus rStatus; //Status variable
  207. m_pSet->GetStatus(rStatus); //Get CRecordset status
  208. if (rStatus.m_lCurrentRecord >= 0) { //Records Exist?
  209. UpdateData(TRUE); //Get data from dialog box
  210. if (!m_bAddingRecord) { //Currently adding?
  211. //If not, set CRecordset in edit mode for updating
  212. m_pSet->Edit();
  213. }
  214. m_pSet->Update(); //Update data if needed
  215. m_pSet->MoveLast(); //Get off record 1
  216. }
  217. m_bAddingRecord = TRUE; //Set flag
  218. m_pSet->SetFieldNull(NULL); //Clear all fields
  219. m_pSet->AddNew(); //Set database in AddNew mode
  220. UpdateData(FALSE); //Update dialog box fields
  221. }
  222. void CODBCDepartmentView::OnFileSave() 
  223. {
  224. if (m_bFieldsChanged && m_pSet->CanTransact()) {
  225. if (!m_bAddingRecord) { //Currently adding?
  226. m_pSet->Edit(); //If not, set edit mode
  227. }
  228. UpdateData(TRUE); //Get data from dialog box
  229. m_pSet->Update(); //Update data if needed
  230. m_bAddingRecord = FALSE; //Reset flag
  231. m_pSet->m_pDatabase->CommitTrans(); //Commit changes
  232. CRecordsetStatus rStatus; //Status variable
  233. m_pSet->GetStatus(rStatus); //Get CRecordset status
  234. m_pSet->Close(); //Close to start transaction
  235. m_pSet->m_pDatabase->BeginTrans(); //Start Transaction
  236. m_pSet->Open(); //Reopen CRecordset
  237. //Restore record position
  238. if (rStatus.m_lCurrentRecord >= 0) {
  239. m_pSet->SetAbsolutePosition(rStatus.m_lCurrentRecord+1);
  240. }
  241. m_bFieldsChanged = FALSE; //Reset flag
  242. UpdateData(FALSE); //Update dialog box fields
  243. }
  244. }
  245. void CODBCDepartmentView::OnDestroy() 
  246. {
  247. //Check for changed fields and transaciton ability
  248. if (m_pSet->CanTransact()) {
  249. if (m_bFieldsChanged) {
  250. if (AfxMessageBox (
  251. "Records have been changed.  Do you want to save?", 
  252. MB_YESNO) == IDNO) {
  253. m_pSet->m_pDatabase->Rollback();
  254. }
  255. else {
  256. if (!m_bAddingRecord) { //Currently adding?
  257. //If not, set edit mode
  258. m_pSet->Edit();
  259. }
  260. UpdateData(TRUE); //Get data from dialog box
  261. m_pSet->Update(); //Update data if needed
  262. m_pSet->m_pDatabase->CommitTrans();
  263. }
  264. }
  265. else {
  266. m_pSet->m_pDatabase->CommitTrans();
  267. }
  268. }
  269. CRecordView::OnDestroy();
  270. }
  271. void CODBCDepartmentView::OnChangeDepartmentcode() 
  272. {
  273. m_bFieldsChanged = TRUE;
  274. }
  275. void CODBCDepartmentView::OnChangeDepartmentname() 
  276. {
  277. m_bFieldsChanged = TRUE;
  278. }
  279. void CODBCDepartmentView::OnUpdateFileSave(CCmdUI* pCmdUI) 
  280. { //Enable save functionality if a record has changed
  281. pCmdUI->Enable(m_bFieldsChanged);
  282. }