⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myodbcview.cpp

📁 这个是我们学校用的VC++教案
💻 CPP
字号:
// MyodbcView.cpp : implementation of the CMyodbcView class
//

#include "stdafx.h"
#include "Myodbc.h"

#include "MyodbcSet.h"
#include "MyodbcDoc.h"
#include "MyodbcView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyodbcView

IMPLEMENT_DYNCREATE(CMyodbcView, CRecordView)

BEGIN_MESSAGE_MAP(CMyodbcView, CRecordView)
	//{{AFX_MSG_MAP(CMyodbcView)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
	ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyodbcView construction/destruction

CMyodbcView::CMyodbcView()
	: CRecordView(CMyodbcView::IDD)
{
	//{{AFX_DATA_INIT(CMyodbcView)
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_bAddMode=FALSE;
}

CMyodbcView::~CMyodbcView()
{
}

void CMyodbcView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyodbcView)
	DDX_FieldText(pDX, IDC_DISCOUNT, m_pSet->m_Discount, m_pSet);
	DDX_FieldText(pDX, IDC_ORDERID, m_pSet->m_OrderID, m_pSet);
	DDX_FieldText(pDX, IDC_PRODUCTID, m_pSet->m_ProductID, m_pSet);
	DDX_FieldText(pDX, IDC_QUANTITY, m_pSet->m_Quantity, m_pSet);
	DDX_FieldText(pDX, IDC_UNITPRICE, m_pSet->m_UnitPrice, m_pSet);
	//}}AFX_DATA_MAP
}

BOOL CMyodbcView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CMyodbcView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_myodbcSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CMyodbcView printing

BOOL CMyodbcView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMyodbcView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMyodbcView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMyodbcView diagnostics

#ifdef _DEBUG
void CMyodbcView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CMyodbcView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CMyodbcDoc* CMyodbcView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyodbcDoc)));
	return (CMyodbcDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyodbcView database support
CRecordset* CMyodbcView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CMyodbcView message handlers

void CMyodbcView::OnRecordAdd() 
{
	// TODO: Add your command handler code here
	if(m_bAddMode) //如果已处于添加模式,则完成添加操作
		OnMove(ID_RECORD_FIRST);
		//CString strCurrentCourse = m_pSet->m_CourseID;
	m_pSet->AddNew();
      //m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
      //m_pSet->m_CourseID = strCurrentCourse;
	m_bAddMode=TRUE;
      //m_ctlSection.SetReadOnly(FALSE);
	UpdateData(FALSE); //更新表单视图
	
}

void CMyodbcView::OnRecordDelete() 
{
	// TODO: Add your command handler code here
	TRY
	{
		m_pSet->Delete();
	}
	CATCH(CDBException,e)
	{
		AfxMessageBox(e->m_strError);
		return;
	}
	END_CATCH
	m_pSet->MoveNext(); //滚动到下一个记录
	if (m_pSet->IsEOF()) //如果滚出了记录集的边界,则滚动到最后一个记录
		m_pSet->MoveLast();
	if (m_pSet->IsBOF()) //如果记录变空了,则清除域数据成员
		m_pSet->SetFieldNull(NULL);
	UpdateData(FALSE); //更新表单视图
	
}

void CMyodbcView::OnRecordRefresh() 
{
	// TODO: Add your command handler code here
	if (m_bAddMode==TRUE)
	{
		m_pSet->Move(AFX_MOVE_REFRESH); //取消添加模式
      //m_ctlSection.SetReadOnly(TRUE);
		m_bAddMode=FALSE;
	}
	UpdateData(FALSE); //更新表单视图
	
}

BOOL CMyodbcView::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (m_bAddMode)
	{
		if (!UpdateData())
			return FALSE;
		TRY
		{
			m_pSet->Update();
		}
		CATCH(CDBException,e)
		{
			AfxMessageBox(e->m_strError);
			return FALSE;
		}
		END_CATCH
		m_pSet->Requery(); //重新查询,使新加的记录对用户可见
		UpdateData(FALSE);
      //m_ctlSection.SetReadOnly(TRUE);
		m_bAddMode=FALSE;
		return TRUE;
	}

	return CRecordView::OnMove(nIDMoveCommand);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -