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

📄 addressbookdoc.cpp

📁 采用MFC DAO对象和接口编程技术实现一个地址簿数据库的管理器。
💻 CPP
字号:
// AddressBookDoc.cpp : implementation of the CAddressBookDoc class
//

#include "stdafx.h"
#include "AddressBook.h"

#include "AddressBookDoc.h"
#include "Hints.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc

IMPLEMENT_DYNCREATE(CAddressBookDoc, CDocument)

BEGIN_MESSAGE_MAP(CAddressBookDoc, CDocument)
	//{{AFX_MSG_MAP(CAddressBookDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc construction/destruction

CAddressBookDoc* CAddressBookDoc::g_Doc=NULL;

CAddressBookDoc::CAddressBookDoc()
{
	// TODO: add one-time construction code here
    ASSERT(!g_Doc);
	g_Doc =this;
	m_pDB  = NULL;
	m_pSet = NULL;
	m_RecordCount = 0 ;
}

CAddressBookDoc::~CAddressBookDoc()
{
	if(m_pSet!=NULL)
	{
		delete m_pSet; m_pSet = NULL;
	}
}

BOOL CAddressBookDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;
	
	if(m_pSet!=NULL) 
	{
		m_pSet->Close(); delete m_pSet; m_pSet = NULL;
	}
	if(m_pDB!=NULL)
	{
		m_pDB->Close();	delete m_pDB; m_pDB=NULL;
	}
	m_bFileOpen = FALSE;

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc serialization

void CAddressBookDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc diagnostics

#ifdef _DEBUG
void CAddressBookDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CAddressBookDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAddressBookDoc commands

BOOL CAddressBookDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName)) 
	{
		AfxMessageBox("可以通过程序菜单“文件”中“打开数据库”\n重新定位数据库的位置!");
		return FALSE;
	}
	
    
	m_pDB = new CDaoDatabase;
	ASSERT(m_pDB!=NULL);
	try
	{
		m_pDB->Open(lpszPathName);
	}
	catch(CDaoException* e)
	{
		delete m_pDB;
		m_pDB = NULL;

		TCHAR szCause[255];
		CString strFormatted = _T("The data file could not be opened because of this error: \n");
		e->GetErrorMessage(szCause, 255);
		strFormatted += szCause;
		AfxMessageBox(strFormatted, MB_OK | MB_ICONEXCLAMATION);
		e->Delete();
		m_bFileOpen = FALSE;
		return FALSE;
	}
	m_pSet = new CDaoRecordsetAccess(m_pDB) ;	
	try 
	{
		m_pSet->Open();
		m_RecordCount=m_pSet->GetRecordCount();
	}
	catch(CDaoException* e)
	{
		delete m_pSet; m_pSet = NULL;

		TCHAR szCause[255];
		CString strFormatted = _T("The data file could not be opened because of this error: \n");
		e->GetErrorMessage(szCause, 255);
		strFormatted += szCause;
		AfxMessageBox(strFormatted, MB_OK | MB_ICONEXCLAMATION);
		e->Delete();
		m_bFileOpen = FALSE;
		return FALSE;
	}

	m_bFileOpen = TRUE;
	UpdateAllViews(NULL,HINT_DB_OPENED);
	
	return TRUE;
}

⌨️ 快捷键说明

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