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

📄 dbodbcview.cpp

📁 VC环境下进行ACCESS数据库连接,查询等操作的程序,ODBC方式,适合初学者.
💻 CPP
字号:
// DbOdbcView.cpp : implementation of the CDbOdbcView class
//

#include "stdafx.h"
#include "DbOdbc.h"
#include "FilterDlg.h"
#include "DbOdbcSet.h"
#include "DbOdbcDoc.h"
#include "DbOdbcView.h"
#include "MoveToDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDbOdbcView

IMPLEMENT_DYNCREATE(CDbOdbcView, CRecordView)

BEGIN_MESSAGE_MAP(CDbOdbcView, CRecordView)
	//{{AFX_MSG_MAP(CDbOdbcView)
	ON_COMMAND(IDM_RECORD_NEW, OnRecordNew)
	ON_COMMAND(IDM_RECORD_DELETE, OnRecordDelete)
	ON_COMMAND(IDM_RECORD_MOVE, OnRecordMove)
	ON_COMMAND(ID_FILTER_FIRSTNAME, OnFilterFirstname)
	ON_COMMAND(ID_SORT_ID, OnSortId)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDbOdbcView construction/destruction

CDbOdbcView::CDbOdbcView()
	: CRecordView(CDbOdbcView::IDD)
{
	//{{AFX_DATA_INIT(CDbOdbcView)
	m_pSet = NULL;
	m_oledtDOB = COleDateTime::GetCurrentTime();
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CDbOdbcView::~CDbOdbcView()
{
}

void CDbOdbcView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
   
	//{{AFX_DATA_MAP(CDbOdbcView)
	DDX_FieldCheck(pDX, IDC_CBCARD, m_pSet->m_SendCard, m_pSet);
	DDX_FieldText(pDX, IDC_EADDR, m_pSet->m_Address, m_pSet);
	DDX_FieldText(pDX, IDC_ECITY, m_pSet->m_City, m_pSet);
	DDX_FieldText(pDX, IDC_ECOUNTRY, m_pSet->m_Country, m_pSet);
	DDX_FieldText(pDX, IDC_EEMAIL, m_pSet->m_EmailAddress, m_pSet);
	DDX_FieldText(pDX, IDC_EFAX, m_pSet->m_FaxNumber, m_pSet);
	DDX_FieldText(pDX, IDC_EFNAME, m_pSet->m_FirstName, m_pSet);
	DDX_FieldText(pDX, IDC_EHPONE, m_pSet->m_HomePhone, m_pSet);
	DDX_FieldText(pDX, IDC_EID, m_pSet->m_AddressID, m_pSet);
	DDX_FieldText(pDX, IDC_ELNAME, m_pSet->m_LastName, m_pSet);
	DDX_FieldText(pDX, IDC_ENOTES, m_pSet->m_Notes, m_pSet);
	DDX_FieldText(pDX, IDC_ESNAME, m_pSet->m_SpouseName, m_pSet);
	DDX_FieldText(pDX, IDC_ESTATE, m_pSet->m_StateOrProvince, m_pSet);
	DDX_FieldText(pDX, IDC_EWEXT, m_pSet->m_WorkExtension, m_pSet);
	DDX_FieldText(pDX, IDC_EWPHONE, m_pSet->m_WorkPhone, m_pSet);
	DDX_FieldText(pDX, IDC_EZIP, m_pSet->m_PostalCode, m_pSet);
	DDX_Text(pDX, IDC_EDOB, m_oledtDOB);
	//}}AFX_DATA_MAP
	
}

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

	return CRecordView::PreCreateWindow(cs);
}

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

}

/////////////////////////////////////////////////////////////////////////////
// CDbOdbcView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDbOdbcView diagnostics

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

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

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

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


/////////////////////////////////////////////////////////////////////////////
// CDbOdbcView message handlers

void CDbOdbcView::OnRecordNew() 
{
	// TODO: Add your command handler code here
	//get a  pointer to the record set
	CRecordset* pSet=OnGetRecordset();
	//make sure that any changes to the current record
	//have been saved
	
	if(pSet->CanUpdate()&&!pSet->IsDeleted())
	{
		pSet->Edit();
		if(!UpdateData())
			return;

		pSet->Update();
	}
	//get the ID for the new record
	long m_lNewID=m_pSet->GetMaxID()+1;
	//add the new record
	m_pSet->AddNew();
	//set the ID in the new record
	m_pSet->m_AddressID=m_lNewID;
	//save the new record
	m_pSet->Update();
	//REFRESH THE RECORD SET
	m_pSet->Requery();
	//move to  the new record
	m_pSet->MoveLast();
	//update the form
	UpdateData(FALSE);
}

void CDbOdbcView::OnRecordDelete() 
{
	// TODO: Add your command handler code here
	//make sure the user wants to delete this record
	if(MessageBox("真要删除该记录吗?",
		"删除记录?",MB_YESNO|MB_ICONQUESTION)==IDYES)
	{//Delete the record
		m_pSet->Delete();
		//Move to the provious record
		m_pSet->MovePrev();
		if(m_pSet->IsBOF())
			m_pSet->MoveLast();
		if(m_pSet->IsEOF())
			m_pSet->SetFieldNull(NULL);
		//Update the form
		UpdateData(FALSE);
	}
	
}

void CDbOdbcView::OnRecordMove() 
{
	//create an instance of the move to dialog
	CMoveToDlg dlgMoveTo;
	//get the row number to move to  
	if(dlgMoveTo.DoModal()==IDOK)
	{
		//Get a pointer to the record set
		CRecordset* pSet=OnGetRecordset();
        //Make sure that there are no outstanding changes to be saved
		if(pSet->CanUpdate()&&!pSet->IsDeleted())
		{
			pSet->Edit();
			if(!UpdateData())
				return;
			pSet->Update();
		}
		//Set the new position
		pSet->SetAbsolutePosition(dlgMoveTo.m_lRowNbr);
		//update the form
		UpdateData(FALSE);
	}
	
}

void CDbOdbcView::OnFilterFirstname() 
{
	// TODO: Add your command handler code here
	DoFilter("姓氏");
}

void CDbOdbcView::DoFilter(CString col)
{
	CFilterDlg dlg;
	int result=dlg.DoModal();
	if(result==IDOK)
	{
		CString 
	    str = col+"='''+dlg.m_filterValue+'''";
		m_pSet->Close();
		m_pSet->m_strFilter=str;
		m_pSet->Open();
		int recCount=m_pSet->GetRecordCount();
		if(recCount==0)
		{
			MessageBox("没有匹配的记录!");
			m_pSet->Close();
			m_pSet->m_strFilter="";
			m_pSet->Open();
		}
		UpdateData(FALSE);
	}

}

void CDbOdbcView::OnSortId() 
{
m_pSet->Close();
m_pSet->m_strSort="邮编";
m_pSet->Open();
UpdateData(FALSE);	
}

⌨️ 快捷键说明

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