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

📄 odbc_exampleview.cpp

📁 Visual C++数据库编程源代码 《Visual C++程序员成长攻略》一书的附带源代码
💻 CPP
字号:
// ODBC_EXAMPLEView.cpp : implementation of the CODBC_EXAMPLEView class
//

#include "stdafx.h"
#include "ODBC_EXAMPLE.h"

#include "ODBC_EXAMPLESet.h"
#include "ODBC_EXAMPLEDoc.h"
#include "ODBC_EXAMPLEView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CODBC_EXAMPLEView

IMPLEMENT_DYNCREATE(CODBC_EXAMPLEView, CRecordView)

BEGIN_MESSAGE_MAP(CODBC_EXAMPLEView, CRecordView)
	//{{AFX_MSG_MAP(CODBC_EXAMPLEView)
	ON_COMMAND(ID_FIND_RECORD, OnFindRecord)
	ON_COMMAND(ID_ADD_RECORD, OnAddRecord)
	ON_COMMAND(ID_DELETE_RECORD, OnDeleteRecord)
	ON_UPDATE_COMMAND_UI(ID_DELETE_RECORD, OnUpdateDeleteRecord)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CODBC_EXAMPLEView construction/destruction

CODBC_EXAMPLEView::CODBC_EXAMPLEView()
	: CRecordView(CODBC_EXAMPLEView::IDD)
{
	//{{AFX_DATA_INIT(CODBC_EXAMPLEView)  //向导自动生成的代码
	m_pSet = NULL;
	m_FindStudent_Name = _T("");
	//}}AFX_DATA_INIT                     //向导自动生成的代码
	//"加入自定义代码"
	m_IsAddRecord = FALSE;
}

CODBC_EXAMPLEView::~CODBC_EXAMPLEView()
{
}

void CODBC_EXAMPLEView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CODBC_EXAMPLEView)        //向导自动生成的代码
	DDX_FieldText(pDX, IDC_SCORE, m_pSet->m_Score, m_pSet);
	DDX_FieldText(pDX, IDC_STUDENT_NAME, m_pSet->m_Student_name, m_pSet);
	DDX_Text(pDX, ID_FIND_NAME, m_FindStudent_Name);
	//}}AFX_DATA_MAP           //向导自动生成的代码
}

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

	return CRecordView::PreCreateWindow(cs);
}

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

}

/////////////////////////////////////////////////////////////////////////////
// CODBC_EXAMPLEView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CODBC_EXAMPLEView diagnostics

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

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

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

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


/////////////////////////////////////////////////////////////////////////////
// CODBC_EXAMPLEView message handlers

BOOL CODBC_EXAMPLEView::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (m_IsAddRecord)                           //判断是否添加
    {   
        UpdateData(TRUE);
        m_pSet ->Update();
        m_pSet ->MoveLast();                     //转向所添加的记录
        m_IsAddRecord=FALSE;
    }
	return CRecordView::OnMove(nIDMoveCommand);
}

void CODBC_EXAMPLEView::OnAddRecord() 
{
	// TODO: Add your command handler code here
    m_IsAddRecord = TRUE ;
    m_pSet ->SetFieldNull(NULL);                  //清空所有
	m_pSet ->AddNew();                          //设置数据库为新添加模式
	CWnd *pWnd;    CString str;
	pWnd = GetDlgItem( IDC_STUDENT_NAME );
	pWnd->GetWindowText(str);
	m_pSet->m_Student_name = str;
	pWnd = GetDlgItem( IDC_SCORE );
	pWnd->GetWindowText(str);
	m_pSet->m_Score = str;
	UpdateData(FALSE);                          //更新对话框
}

void CODBC_EXAMPLEView::OnDeleteRecord() 
{
	//"加入自定义代码"
	if ( AfxMessageBox("是否确定删除该记录?", MB_YESNO) != IDYES)
        return;
	if (m_IsAddRecord)                           //所删除记录处于被添加状态
	{                          
		m_pSet -> CancelUpdate();
		m_IsAddRecord = FALSE ;
		m_pSet -> MovePrev() ;
		return;
	}
	try
	{
		m_pSet -> Delete();
	}
	catch (CDBException * a )                      //捕获错误
	{
			AfxMessageBox ( "删除记录错误! \n" + a -> m_strError ,	MB_ICONEXCLAMATION);
			m_pSet -> MoveFirst();
			 a -> Delete();
			 UpdateData(FALSE);
		 return;
	}
	m_pSet -> MoveFirst();
	m_pSet -> Close();                         //关闭记录集
	m_pSet -> Open();
	UpdateData(FALSE);
}

void CODBC_EXAMPLEView::OnUpdateDeleteRecord(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI -> Enable( ( ! m_pSet -> IsBOF()) && ( ! m_pSet -> IsDeleted()) && ( ! m_pSet -> IsEOF()));
    //判断是否存在当前记录,从而决定是否使用OnDeleteRecord()删除函数
}

void CODBC_EXAMPLEView::OnFindRecord() 
{
	// TODO: Add your command handler code here
	CWnd *pWnd;
	pWnd = GetDlgItem( ID_FIND_NAME );
	pWnd->GetWindowText(m_FindStudent_Name);
	int i=0;
	if(m_FindStudent_Name != "")
	{
		do{
			if(m_FindStudent_Name != m_pSet->m_Student_name)
				m_pSet->MoveNext();
			else
			{
				AfxMessageBox( "查询成功!" );
				pWnd = GetDlgItem( IDC_STUDENT_NAME );
				pWnd->SetWindowText(m_pSet->m_Student_name);
				pWnd = GetDlgItem( IDC_SCORE );
				pWnd->SetWindowText(m_pSet->m_Score);
				m_pSet->MoveNext();
				i=1;
			}
		}while(!m_pSet->IsEOF());
		if(i==0)
			AfxMessageBox ( "没有找到记录!" , MB_ICONEXCLAMATION);
	}
	else
		AfxMessageBox ( "请输入需查询记录!" , MB_ICONEXCLAMATION);
}

⌨️ 快捷键说明

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