📄 databasedoc.cpp
字号:
// DatabaseDoc.cpp : implementation of the CDatabaseDoc class
//
#include "stdafx.h"
#include "Database.h"
#include "DatabaseDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDatabaseDoc
IMPLEMENT_DYNCREATE(CDatabaseDoc, CDocument)
BEGIN_MESSAGE_MAP(CDatabaseDoc, CDocument)
//{{AFX_MSG_MAP(CDatabaseDoc)
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDatabaseDoc construction/destruction
CDatabaseDoc::CDatabaseDoc()
{
//初始化成员变量
m_pDB = NULL;
m_pSet = NULL;
}
CDatabaseDoc::~CDatabaseDoc()
{
//删除变量
delete m_pSet;
delete m_pDB;
}
BOOL CDatabaseDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
if(m_pSet!=NULL){ //关闭 Dataset
m_pSet->Close();
delete m_pSet;
m_pSet=NULL;
}
if(m_pDB!=NULL){ //关闭 Database
m_pDB->Close();
delete m_pDB;
m_pDB=NULL;
}
m_bFileOpen = FALSE; // 数据库未打开
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CDatabaseDoc serialization
void CDatabaseDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CDatabaseDoc diagnostics
#ifdef _DEBUG
void CDatabaseDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CDatabaseDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDatabaseDoc commands
BOOL CDatabaseDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
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;
}
//如果成功打开数据库,则继续打开 EMPLOYEES 表
m_pSet = new CDaoRecordsetEmployee(m_pDB);
ASSERT(m_pSet != NULL);
try
{
m_pSet->Open();
long numRec = 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;
}
// boolean variable to indicate database is open
m_bFileOpen = TRUE;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -