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

书籍源码

开发平台:

Visual C++

  1. // OLEDBMFCBlobView.cpp : implementation of the COLEDBMFCBlobView class
  2. //
  3. #include "stdafx.h"
  4. #include "OLEDBMFCBlob.h"
  5. #include "OLEDBMFCBlobSet.h"
  6. #include "OLEDBMFCBlobDoc.h"
  7. #include "OLEDBMFCBlobView.h"
  8. //Added by Chuck Wood for Error Checking
  9. #include "..OLEDBErrorCheckingOLEDBErrorChecking.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // COLEDBMFCBlobView
  17. IMPLEMENT_DYNCREATE(COLEDBMFCBlobView, COleDBRecordView)
  18. BEGIN_MESSAGE_MAP(COLEDBMFCBlobView, COleDBRecordView)
  19. //{{AFX_MSG_MAP(COLEDBMFCBlobView)
  20. ON_EN_CHANGE(IDC_COMMENT, OnChangeComment)
  21. //}}AFX_MSG_MAP
  22. END_MESSAGE_MAP()
  23. /////////////////////////////////////////////////////////////////////////////
  24. // COLEDBMFCBlobView construction/destruction
  25. COLEDBMFCBlobView::COLEDBMFCBlobView()
  26. : COleDBRecordView(COLEDBMFCBlobView::IDD)
  27. {
  28. //{{AFX_DATA_INIT(COLEDBMFCBlobView)
  29. m_pSet = NULL;
  30. //}}AFX_DATA_INIT
  31. m_bCommentChange = FALSE;
  32. }
  33. COLEDBMFCBlobView::~COLEDBMFCBlobView()
  34. {
  35. }
  36. void COLEDBMFCBlobView::DoDataExchange(CDataExchange* pDX)
  37. {
  38. COleDBRecordView::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(COLEDBMFCBlobView)
  40. //}}AFX_DATA_MAP
  41. DDX_Text(pDX, IDC_FIRSTNAME, m_pSet->m_FirstName,31);
  42. DDX_Text(pDX, IDC_MIDDLENAME, m_pSet->m_MidName,31);
  43. DDX_Text(pDX, IDC_LASTNAME, m_pSet->m_LastName,31);
  44. DDX_Text(pDX, IDC_USERID, m_pSet->m_UserID,51);
  45. DDX_Text(pDX, IDC_PASSWORD, m_pSet->m_Password,51);
  46. DDX_Text(pDX, IDC_ADDRESS, m_pSet->m_Address,31);
  47. DDX_Text(pDX, IDC_CITY, m_pSet->m_City,51);
  48. DDX_Text(pDX, IDC_STOTEORPROVINCE, m_pSet->m_StateOrProvince,21);
  49. DDX_Text(pDX, IDC_POSTALCODE, m_pSet->m_PostalCode,21);
  50. DDX_Text(pDX, IDC_PHONENUMBER, m_pSet->m_PhoneNumber,16);
  51. DDX_Text(pDX, IDC_EMAIL, m_pSet->m_EMAIL,51);
  52. DDX_Text(pDX, IDC_MAJOR, m_pSet->m_Major,51);
  53. DDX_Text(pDX, IDC_SSN, m_pSet->m_StudentSSN,31);
  54. }
  55. BOOL COLEDBMFCBlobView::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57. return COleDBRecordView::PreCreateWindow(cs);
  58. }
  59. void COLEDBMFCBlobView::OnInitialUpdate()
  60. {
  61. m_pSet = &GetDocument()->m_oLEDBMFCBlobSet;
  62. {
  63. CWaitCursor wait;
  64. HRESULT hr = m_pSet->Open();
  65. if (hr != S_OK)
  66. {
  67. AfxMessageBox(_T("Record set failed to open."), MB_OK);
  68. m_bOnFirstRecord = TRUE;
  69. m_bOnLastRecord = TRUE;
  70. }
  71. }
  72. COleDBRecordView::OnInitialUpdate();
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // COLEDBMFCBlobView diagnostics
  76. #ifdef _DEBUG
  77. void COLEDBMFCBlobView::AssertValid() const
  78. {
  79. COleDBRecordView::AssertValid();
  80. }
  81. void COLEDBMFCBlobView::Dump(CDumpContext& dc) const
  82. {
  83. COleDBRecordView::Dump(dc);
  84. }
  85. COLEDBMFCBlobDoc* COLEDBMFCBlobView::GetDocument() // non-debug version is inline
  86. {
  87. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COLEDBMFCBlobDoc)));
  88. return (COLEDBMFCBlobDoc*)m_pDocument;
  89. }
  90. #endif //_DEBUG
  91. /////////////////////////////////////////////////////////////////////////////
  92. // COLEDBMFCBlobView database support
  93. CRowset* COLEDBMFCBlobView::OnGetRowset()
  94. {
  95. return m_pSet;
  96. }
  97. /////////////////////////////////////////////////////////////////////////////
  98. // COLEDBMFCBlobView message handlers
  99. BOOL COLEDBMFCBlobView::OnMove(UINT nIDMoveCommand) 
  100. {
  101. UpdateData(TRUE);
  102. BOOL returnValue = COleDBRecordView::OnMove(nIDMoveCommand);
  103. UpdateData(FALSE);
  104. return returnValue;
  105. }
  106. BOOL COLEDBMFCBlobView::UpdateData(BOOL bSaveAndValidate) {
  107. //Use SteveMcQueen to get the BLOB
  108. CString SteveMcQueen = "";
  109. const int BUFFER_SIZE = 1000; //Size of the buffer
  110. ULONG ulIOBytes;
  111. HRESULT hr;
  112. if (bSaveAndValidate && m_bCommentChange) {
  113. //Get Text for BLOB
  114. GetDlgItemText(IDC_COMMENT, SteveMcQueen);
  115. //Use SteveMcQueen to put it to the BLOB
  116. long bufferSize = SteveMcQueen.GetLength();
  117. if (bufferSize) {
  118. hr = m_pSet->m_Comments->Write(
  119. SteveMcQueen.GetBuffer(bufferSize), 
  120. bufferSize, &ulIOBytes);
  121. if (FAILED(hr)) {
  122. COLEDBErrorChecking::DisplayHRRESULTMessage(
  123. hr, "Write in UpdateData");
  124. }
  125. else {
  126. hr = m_pSet->SetData();
  127. if (FAILED(hr)) {
  128. COLEDBErrorChecking::DisplaySingleError(
  129. hr, "SetData in UpdateData");
  130. }
  131. }
  132. }
  133. else {
  134. m_pSet->m_Comments = NULL;
  135. }
  136. }
  137. else{
  138. //Test to see if comments exist
  139. if (m_pSet->m_Comments) {
  140. char bBuffer[BUFFER_SIZE]; //Allocate buffer
  141. do {
  142. hr = m_pSet->m_Comments->Read(
  143. bBuffer, BUFFER_SIZE-1, &ulIOBytes);
  144. if (FAILED(hr)) {
  145. COLEDBErrorChecking::DisplayHRRESULTMessage(hr, "Read in UpdateData");
  146. break; //out of loop
  147. }
  148. bBuffer[ulIOBytes] = 0; //Null terminate
  149. SteveMcQueen = SteveMcQueen + bBuffer;
  150. } while (ulIOBytes > 0);
  151. }
  152. if (SUCCEEDED(hr)) {
  153. SetDlgItemText(IDC_COMMENT, (LPCTSTR) SteveMcQueen);
  154. }
  155. }
  156. m_bCommentChange = FALSE; //Set update flag
  157. //Update the normal, non-BLOB fields
  158. return CWnd::UpdateData(bSaveAndValidate);
  159. }
  160. void COLEDBMFCBlobView::OnChangeComment() 
  161. {
  162. m_bCommentChange = TRUE;
  163. }