DBDialog.cpp
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:3k
- // DBDialog.cpp : Implementation of CDBDialog
- #include "stdafx.h"
- #include "DBDialog.h"
- /////////////////////////////////////////////////////////////////////////////
- // CDBDialog
- /**********************************************
- The following were added by Chuck Wood for
- Visual C++ Database Developer's Guide
- **********************************************/
- void CDBDialog::OnMove(int position)
- {
- //This OnMove is not really an event, but
- //rather is called by other move functions
- HRESULT hr;
- if (FAILED(SaveDepartment())) {
- DisplayStatus("Save failed. No move is possible.");
- return; //End if save did not work.
- }
- DisplayStatus("");
- switch (position) {
- case (FIRST) :
- hr = m_pSet->MoveFirst();
- break;
- case (NEXT) :
- hr = m_pSet->MoveNext();
- if (hr != S_OK) { //EOF
- DisplayStatus("Last record reached.");
- hr = m_pSet->MoveLast();
- }
- break;
- case (LAST) :
- hr = m_pSet->MoveLast();
- break;
- case (PREV) :
- hr = m_pSet->MovePrev();
- if (hr != S_OK) { //BOF
- DisplayStatus("First record reached.");
- hr = m_pSet->MoveFirst();
- }
- break;
- }
- if (FAILED(hr)) {
- DisplayError("No records found. Adding new record");
- DisplayStatus("No records found.");
- AddRecord();
- }
- UpdateData(FALSE); //Update Screen
- }
- void CDBDialog::ResetRowSet()
- {
- if (m_pSet->MoveNext() != S_OK) { //Go to next record
- if (m_pSet->MoveFirst() != S_OK) {
- MessageBox("No more records", "",
- MB_ICONEXCLAMATION);
- //No records, so set up an add record
- AddRecord();
- }
- }
- UpdateData(FALSE); //Write changes to window
- }
- void CDBDialog::AddRecord()
- {
- m_pSet->ClearRecord();
- m_bAddingRecord = TRUE;
- UpdateData(FALSE); //Update the window
- }
- HRESULT CDBDialog::SaveDepartment()
- {
- UpdateData(TRUE); //Read from screen
- HRESULT hr;
- if (m_bAddingRecord) {
- hr = m_pSet->Insert(); //Insert New rew
- }
- else {
- hr = m_pSet->SetData(); //Update current row
- }
- if (FAILED(hr)) {
- DisplayError("Update Failed.", hr, "SaveDepartment");
- }
- else {
- m_bAddingRecord = FALSE;
- }
- return hr;
- }
- void CDBDialog::UpdateData(BOOL bSaveChangesToSet)
- {
- if (bSaveChangesToSet) {
- //Read From Screen
- GetDlgItemText(IDC_DEPARTMENTCODE,
- (char *) m_pSet->m_DepartmentCode, 5);
- GetDlgItemText(IDC_DEPARTMENTNAME,
- (char *) m_pSet->m_DepartmentName, 51);
- }
- else {
- //Write to Screen
- SetDlgItemText(IDC_DEPARTMENTCODE,
- (char *) m_pSet->m_DepartmentCode);
- SetDlgItemText(IDC_DEPARTMENTNAME,
- (char *) m_pSet->m_DepartmentName);
- }
- }
- void CDBDialog::DisplayStatus(char *strMessage)
- {
- SetDlgItemText(IDC_STATUS, strMessage);
- }
- void CDBDialog::DisplayError(char *strMessage, HRESULT hresult, char *strFunction)
- {
- //Allow 1 k for the error message
- char message[1024];
- strcpy(message, strMessage);
- if (strFunction) { //Check for function equal to null
- //Function was passed, so see what function it was
- strcat(message, " in the ");
- strcat(message, strFunction);
- strcat(message, " function ");
- }
- if (FAILED(hresult)) {
- char holdmessage[512]; //Allow 512 bytes for HR message
- sprintf(holdmessage, "nnHRESULT was %ld", hresult);
- strcat(message, holdmessage);
- }
- MessageBox(message, "An error has occurred");
- }