myodbcsampleview.cpp
来自「VC程序设计与实例中与数据库编程有关的vc++源代码」· C++ 代码 · 共 219 行
CPP
219 行
// MyOdbcSampleView.cpp : implementation of the CMyOdbcSampleView class
//
#include "stdafx.h"
#include "MyOdbcSample.h"
#include "StudentSet.h"
#include "MyOdbcSampleDoc.h"
#include "MyOdbcSampleView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyOdbcSampleView
IMPLEMENT_DYNCREATE(CMyOdbcSampleView, CRecordView)
BEGIN_MESSAGE_MAP(CMyOdbcSampleView, CRecordView)
//{{AFX_MSG_MAP(CMyOdbcSampleView)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMyOdbcSampleView construction/destruction
CMyOdbcSampleView::CMyOdbcSampleView()
: CRecordView(CMyOdbcSampleView::IDD)
{
//{{AFX_DATA_INIT(CMyOdbcSampleView)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CMyOdbcSampleView::~CMyOdbcSampleView()
{
}
void CMyOdbcSampleView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyOdbcSampleView)
DDX_FieldText(pDX, IDC_EDIT_AGE, m_pSet->m_AGE, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_NAME, m_pSet->m_SNAME, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_NUMBER, m_pSet->m_SNO, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_SEX, m_pSet->m_SEX, m_pSet);
//}}AFX_DATA_MAP
}
BOOL CMyOdbcSampleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CMyOdbcSampleView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_studentSet;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CMyOdbcSampleView printing
BOOL CMyOdbcSampleView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMyOdbcSampleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMyOdbcSampleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMyOdbcSampleView diagnostics
#ifdef _DEBUG
void CMyOdbcSampleView::AssertValid() const
{
CRecordView::AssertValid();
}
void CMyOdbcSampleView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CMyOdbcSampleDoc* CMyOdbcSampleView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyOdbcSampleDoc)));
return (CMyOdbcSampleDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMyOdbcSampleView database support
CRecordset* CMyOdbcSampleView::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// CMyOdbcSampleView message handlers
BOOL CMyOdbcSampleView::OnMove(UINT nIDMoveCommand)
{
// TODO: Add your specialized code here and/or call the base class
switch (nIDMoveCommand)
{
case ID_RECORD_PREV:
m_pSet->MovePrev();
if (!m_pSet->IsBOF())
break;
case ID_RECORD_FIRST:
m_pSet->MoveFirst();
break;
case ID_RECORD_NEXT:
m_pSet->MoveNext();
if (!m_pSet->IsEOF())
break;
if (!m_pSet->CanScroll()) {
// Clear out screen since we're sitting on EOF
m_pSet->SetFieldNull(NULL);
break;
}
case ID_RECORD_LAST:
m_pSet->MoveLast();
break;
default:
// unexpected case value
ASSERT(FALSE);
}
// show results of move operation
UpdateData(FALSE);
return TRUE;
//return CRecordView::OnMove(nIDMoveCommand);
}
void CMyOdbcSampleView::OnButtonAdd()
{
// TODO: Add your control notification handler code here
m_pSet->AddNew();
UpdateData(TRUE);
if (m_pSet->CanUpdate())
{
m_pSet->Update();
}
if (!m_pSet->IsEOF())
{
m_pSet->MoveLast();
}
// m_pSet->Requery(); // for sorted sets
UpdateData(FALSE);
}
void CMyOdbcSampleView::OnButtonClear()
{
// TODO: Add your control notification handler code here
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
}
void CMyOdbcSampleView::OnButtonDelete()
{
// TODO: Add your control notification handler code here
CRecordsetStatus status;
try {
m_pSet->Delete();
}
catch(CDBException* e) {
AfxMessageBox(e->m_strError);
e->Delete();
m_pSet->MoveFirst(); // lost our place!
UpdateData(FALSE);
return;
}
m_pSet->GetStatus(status);
if (status.m_lCurrentRecord == 0) {
// We deleted last of 2 records
m_pSet->MoveFirst();
}
else {
m_pSet->MoveNext();
}
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?