MFC图书馆管理系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MFC图书馆管理系统
MFC课程设计
图书馆管理系统图书馆管理系统
院系 :计算机科学技术学院二系
班级:计07–2班
姓名:白杨(07)
合作者:刘飞
指导教师 :王双利
2021 年01月4日
MFC课程设计任务书
一、题目 : 图书馆管理系统
二、设计要求
(1)刘飞 ( 组长 ) 和白杨组成设计小组。

(2)小组成员分工协作完成。

要求每个成员有自己相对独立的模块,同时要认
识其他组员完成的内容。

(3)查阅相关资料,自学详细课题中涉及到的新知识。

(4)采用结构化、模块化程序设计方法设计,功能要完满,界面雅观。

(5)所设计的系统要最少应用一个课程中也许与其亲近相关的算法。

(6)按要求写出课程设计报告。

其主要内容包括 : 封皮、课程设计任务书,指导教师考语与成绩、目录、归纳、软件整体设计、详细设计、软件的调试、总结、附录 : 带中文说明的程序清单、参照文件。

报告一律用 A4 纸打印,中文字体为宋体,西文字体用Time New Roma,一律用小四号字,行距采用“固定值〞 18 磅,首行缩进 2 字符。

整体设计应配合软件整体模块结构图来说明软件应拥有的功能。

详细设
计阐述自己设计模块局部的设计思想、应用到的理论和算法、程序流程等等,调试
的表达应配合出错场景的抓图来说明出现了哪些错误,如何解决的。

(7)课程设计报告中的软件整体设计、详细设计、软件的调试等主体内容要以
文字描述、图表等形式为主,可配以主要核心代码,在附录中附程序清单。

三、课程设计工作量
由于是设计小组团结协作完成设计任务,一般每人的程序量在 200 行有效程序行左右,不得抄袭。

四、课程设计工作方案
2021年 12月 07日,指导教师讲课,学生依照题目准备资料;
2021年 12月 08日,设计小组进行整体方案设计和任务分工;
2021年 12月 09日,2021 年 12 月 14 日,每人完成自己担当的程序模块并经过独立编译 ;
2021年 12月 15日,2021 年 12 月 17 日,将各模块集成为一个完满的系统,并录入足够的数据进行调试运行;
2021 年 12 月 18 日,查收、开始撰写报告;
2021 年 12 月 21 日前,提交课程设计报告。

指导教师签章 :
教研室主任签章
2
MFC课程设计指导教师考语与成绩
指导教师考语 :
课程设计表现成绩 :
课程设计查收成绩 :
课程设计报成伟绩 :
课程设计总成绩 :
指导教师签章
2021 年 12 月 26日
3
目录
第 1 章概述 (5)
性能需求 (5)
功能需求 (5)
第 2 章大纲设计 (6)
功能模块设计 (6)
算法解析和设计 (6)
第 3 章详细设计 (7)
数据库操作 (7)
模块设计详细设计 (7)
程序流程图 (8)
调试解析 (9)
测试结果 (9)
第 5 章总结 ....................................................22参考文件.......................................................22附录 .. (23)
4
第1章归纳
性能需求
本系统采用 VC做为设计语言,开发工具采用作为开发工具,
本系统可广泛应用于以Windows为平台的 PC上,由于当前主流操作系统即为Windows,顾客广泛实行。

1.2 功能需求
素来以来人们使用传统的人工方式管理图书馆的平常工作,对于图书馆的借书和
还书过程,想必大家都已很熟悉。

在计算机还没有在图书馆广泛使用从前,借书和
还书过程主要依靠手工。

一个最典型的手工办理还书过程就是 : 读者将要借的书和借阅证交给工作人员,工作人员将每本书上附带的描述书的信息的卡片和读者的借阅证放在一个小格栏里,并在借阅证和每本书贴的借阅条上填写借阅信息。

这样借书过程就完成了。

还书时,读者将要还的书交给工作人员,工作人员依照图书信息找到相应的书卡和借阅证,并填好相应的还书信息,这样还书过程就完成了。

以上所描述的手工过程的缺乏之处不问可知,第一办理借书、还书业务流程的效率很低,其次办理能力比较低,一段时间内,所能效劳的读者人数是有限的。

利用计算机来办理这些流程无疑会极大程度地提高效率和办理能力。

我们将会看到排队等候借书、还书的队伍不再那么长,工作人员出错的概率也小了,读者可以花更多的时间在选择书和看书上。

为方便对图书馆书籍、读者资料、借还书等进行高效的管理,特编写该程序以提高图书馆的管理效率。

使用该程序此后,工作人员可以盘问某位读者、某种图书的借阅情况,还可以对当前图书借阅情况进行一些统计,给出统计表格,以便全面掌握图书的流通情况。

图书管理系统是一种基于集中一致规划的数据库数据管理新模式。

在对图书、读者的管理,其实是对图书、读者数据的管理。

本系统的建成无疑会为管理者对图书管理系统供应极大的帮助。

5
第2章大纲设计
2.1 功能模块设计
第一需要对图书管理系统详细地认识和解析,一个功能完满满足根本需要的系统,必定包括以下的几个模块。

(1)根本数据保护功能模块
在这个功能模块中,供应了使用者录入、更正并进行保护根本数据的路子。

比如在这个模块中可以输入读者的信息、书籍的各项信息,也可以对这些信息做修改、更新。

(2)根本业务功能模块
这个功能模块主要实现对读者利用图书馆借还书籍的管理,比方依照读者的借用书籍要求更新图书馆书籍数据库系统,若是书籍已经借出,可以进行预留操作。

用户每次还书也要进行数据库记录的各项更新。

这个模块是整个图书管理系统的关键局部。

(5)安全 / 使用管理功能模块
这是任何一个信息管理系统都需要的局部,图书管理系统的操作也只能由回主
菜专人进行,只有图书管理部门的工作人员才能拥有权限,特别是图书的借书情况,输出单若是没有安全管理局部,结果难以想象,可能每次登录都需要用户身份的考据。

Hash

图书管理系统
基基安
本本全数业使
据务用
维功管护能理功模能块
图 1系统问倒功能需求框图
2.2 算法解析和设计
本系统数据库操作局部采用DAO技术,并应用了多线程技术,各模块均有自己独立的线程进行操作,但由于DAO自己其实不支持多线程,故所有与数据库相关的操作均在主线程中,各模块经过发送自定义信息与主线程通信来获取数据库信息。

6
第3章详细设计
3.1 数据库操作
数据库操作局部采用 DAO技术, DAO(数据接见对象 ) 是一种应用程序编程接口(API) ,存在于微软的 Visual Basic 中,它赞同程序员央求对微软的 Access 数据库的接见。

DAO是微软的第一个面向对象的数据库接口。

DAO对象封闭了 Access 的 Jet 函数。

经过 Jet 函数,它还可以接见其他的结构化盘问语言 (SQL)数据库。

3.2 模块设计详细设计
1.根本数据保护
在这个功能模块中,供应了使用者录入、更正并进行保护根本数据的路子。


如在这个模块中可以输入读者的信息、书籍的各项信息,也可以对这些信息做修
改、更新。

2.根本业务功能
这一局部主要包括以下五局部 :
(1) 库存图书信息
该局部主若是列出所有库存图书,在管理员和员工登录模式下供应对图书信息
的更正等功能,在读者的登录模式下供应借阅功能。

详细代码及截图见调试解析部
分。

(2)员工信息
该局部主要列出所有员工信息,并供应信息更正功能,且本局部只有在以管理员权限登录后才可用,在以一般员工登陆和读者登陆时无效。

详细代码及截图见调试解析局部。

(3)读者信息
该局部主要列出所有读者信息,并供应增加更正功能,本局部只有在以管理员或一般员工模式登陆时才有效,读者登陆时无效。

详细代码及截图见调试解析部分。

(4)图书入库
该局部主要供应图书的入库功能,在以管理员和一般员工登录时有效。

详细代码及截图见调试解析局部。

(5)借阅信息
该局部主要列出读者的借阅信息,借阅的图书数量,及日期信息等。

详细代码及截图见调试解析局部。

7
3.3 程序流程图
登陆
权限 ,
读者
一般员工管理员
图员读图借
书工者书阅
信信信入信
息息息库息
信息增加、更正、删除
8
第 4 章调试解析与测试结果 4.1 调试解析
经过老师的查收和自己的总结,我们程序能正常的完成上述所表达的工程。


然程序各项功能都能很好的实现,但仍有好多缺乏之处,由于我们当前能力还很有限,时间也有些紧,所以有好多地方还没有做到位。

比方: 该次课程设计中的一些功能没有获取很好的实现, 像动画都没有很好的实现。

但是,我们在整个程序中实
现了所要求的内容,整体也算完满,填充了上述带来的缺憾。

4.2 测试结果
(1)登陆代
码以下 :
CLoadDlg dlg;
aa:if(dlg.DoModal() == IDOK)
{
if(!m_bAccept)
{
AfxMessageBox(" 用户名或密码错误,请重新输入~ ");
goto aa;
}
}
else
return FALSE;
图片 :
9
(2)库存图书信息
void CTestView::OnBook() {
CTestDoc *pDoc = GetDocument();
m_nTableNO = 1;
if(m_strDatabase != "")
pDoc->OpenMdb();
else
{
MessageBox("未设置数据库,请退出并设置数据库~"); }
}
void CBookDlg::OnButtonAdd() {
if(m_nTableNO == 1)
{
SetValue();
::SendMessage(hwnd,WM_USER_ADD,0,0); }else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonDel() {
if(m_nTableNO == 1)
{
::SendMessage(hwnd,WM_USER_DEL,0,0); OnButtonFirst();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonEdit() {
if(m_nTableNO == 1)
{
SetValue();
::SendMessage(hwnd,WM_USER_EDIT,0,0); }
10
else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonFirst() {
if(m_nTableNO == 1)
{
::SendMessage(hwnd,WM_USER_FIRST,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonLast() {
if(m_nTableNO == 1)
{
::SendMessage(hwnd,WM_USER_LAST,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonNext() {
if(m_nTableNO == 1)
{
::SendMessage(hwnd,WM_USER_NEXT,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonPrv() {
11
if(m_nTableNO == 1)
{
::SendMessage(hwnd,WM_USER_PREV,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CBookDlg::OnButtonRequary() {
if(m_nTableNO == 1)
{
::SendMessage(hwnd,WM_USER_REQUERY,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
图片 :
(3)员工信息
void CTestView::OnStaff()
{
if(m_nPurview == 0)
{
CTestDoc *pDoc = GetDocument();
12
m_nTableNO = 2;
if(m_strDatabase != "")
pDoc->OpenMdb();
else
{
MessageBox("未设置数据库,请退出并设置数据库~"); }
}else
{
MessageBox("对不起,您没有此权限~~");
}
}
void CStaffDlg::OnButtonAdd() {
if(m_nTableNO == 2)
{
SetValue();
::SendMessage(hwnd,WM_USER_ADD,0,0);
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonDel() {
if(m_nTableNO == 2)
{
::SendMessage(hwnd,WM_USER_DEL,0,0); OnButtonFirst();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonEdit() {
if(m_nTableNO == 2)
{
SetValue();
::SendMessage(hwnd,WM_USER_EDIT,0,0); }else
13
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonFirst() {
if(m_nTableNO == 2)
{
::SendMessage(hwnd,WM_USER_FIRST,0,0); GetValue();
}
else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonLast() {
if(m_nTableNO == 2)
{
::SendMessage(hwnd,WM_USER_LAST,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonNext() {
if(m_nTableNO == 2)
{
::SendMessage(hwnd,WM_USER_NEXT,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonPrv() {
14
if(m_nTableNO == 2)
{
::SendMessage(hwnd,WM_USER_PREV,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStaffDlg::OnButtonRequary() {
if(m_nTableNO == 2)
{
::SendMessage(hwnd,WM_USER_REQUERY,0,0); GetValue();
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
图片 :
(4)读者信息
void CTestView::OnReader()
{
if(m_nPurview != 2)
{
CTestDoc *pDoc = GetDocument();
15
m_nTableNO = 0;
if(m_strDatabase != "")
pDoc->OpenMdb();
else
{
MessageBox("未设置数据库,请退出并设置数据库~"); }
}else
{
MessageBox("对不起,您没有此权限~~");
}
}
void CStudentDlg::OnButtonAdd() {
if(m_nTableNO == 0)
{
SetValue();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
::SendMessage(hwnd,WM_USER_ADD,0,0); }else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonDel() {
if(m_nTableNO == 0)
{
::SendMessage(hwnd,WM_USER_DEL,0,0); OnButtonFirst();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonEdit() {
if(m_nTableNO == 0)
16
{
SetValue();
::SendMessage(hwnd,WM_USER_EDIT,0,0); m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonFirst() {
if(m_nTableNO == 0)
{
::SendMessage(hwnd,WM_USER_FIRST,0,0); GetValue();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonNext() {
if(m_nTableNO == 0)
{
::SendMessage(hwnd,WM_USER_NEXT,0,0); GetValue();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonPrv() {
if(m_nTableNO == 0)
{
::SendMessage(hwnd,WM_USER_PREV,0,0); 17
GetValue();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonRequary() {
if(m_nTableNO == 0)
{
::SendMessage(hwnd,WM_USER_REQUERY,0,0); GetValue();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonLast() {
if(m_nTableNO == 0)
{
::SendMessage(hwnd,WM_USER_LAST,0,0); GetValue();
m_strReaderID = m_strID;
m_strBookNumber = m_strNumber;
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
void CStudentDlg::OnButtonBorrow() {
if(m_nTableNO == 0)
{
if(m_strBookNumber == "0")
{
MessageBox("该用户借阅记录为零~ ");
18
}
else
{
CDialog::OnOK();
::SendMessage(hwnd,WM_USER_DEMAND,0,0); }
}else
{
MessageBox("系统忙,请稍后再试~ ");
}
}
图片 :
(5)图书入库
void CTestView::OnAdd() {
if(m_nPurview != 2)
{
CTestDoc *pDoc = GetDocument();
m_nTableNO = 3;
if(m_strDatabase != "")
pDoc->OpenMdb();
else
{
MessageBox("未设置数据库,请退出并设置数据库~"); }
}else
{
MessageBox("对不起,您没有此权限~~");
19
}
}
void CAddDlg::OnButtonAdd() {
if(m_nTableNO == 3)
{
SetValue();
::SendMessage(hwnd,WM_USER_ADD,0,0); }else
{
MessageBox("系统忙,操作无效~ ");
}
}
图片 :
(6)借阅信息
void CTestView::OnReaderBook() { CTestDoc *pDoc = GetDocument();
if(m_strDatabase != "")
{
if(m_nPurview == 2)
{
if(m_strInfo == "0")
MessageBox("您的借阅记录为空~ ");
}else
{
if(m_nTableNO == 0)
20
{
m_nTableNO = 4;
if(AfxMessageBox("该用户借阅记录为空~可否创立,",MB_YESNO) == IDYES) {
m_nFieldRow = m_nSelectedRow;
pDoc->OnDemand();
if(AfxMessageBox("数据库已建立~可否增加,",MB_YESNO) == IDYES)
{
/* m_strReaderID = m_arrayFieldValue[5];
pDoc->OpenMdb();*/
}
}
}
}
}
else
{
MessageBox("未设置数据库,请退出并设置数据库~");
}
}
图片 :
21
第5章总结
本程序主要实现了 ;
经过两年半的学习在编程方面了很的大进步,特别在课程设计的时候更能锻炼
我们的综合能力,在此次课程设计的时候也遇到了很多麻烦,同时也认识到自己的
能力还有待提高,但是此次在老师和同学们的热情帮助下,比较圆满的完成了任务,今后要更加努力,不断的吸取知识,让自己的编程能力不断提高~
最后,要特别感谢王双利老师,这一次课程设计恩赐了我们很大的帮助,也让
我们学到了更多技巧和知识。

还要感王双利老师对我们一学期的支持和耐心的指
导,他不但教会了我们知识还教会了我们好多人生的道理,让我们受益匪浅。

参照文件
[1]任哲, MFC Windows应用程序设计 ( 第二版 ) ,北京,清华大学初版社,2007 年 9 月。

[2]武莹,彭文明, Visual C++ 开发合用编程 200 例,中国铁道初版社, 2006 年1月。

[3] 任哲,高诚,软件工程,中国水利水电初版社,2021 年 10 月 [4] VC++ 技术内幕 ( 第四版 )
22
附录
#include "stdafx.h"
#include "test.h"
#include "MainFrm.h"
#include "testDoc.h"
#include "testView.h"
#include "LoadDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int m_nPurview;
extern BOOL m_bAccept;
////////////////////////////////////////////////////////////////////
/////////
// CTestApp
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
//{{AFX_MSG_MAP(CTestApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
//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
//Standard file based document commands
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP()
////////////////////////////////////////////////////////////////////
/////////
//CTestApp construction
CTestApp::CTestApp()
{
//TODO: add construction code here,
//Place all significant initialization in
InitInstance 23
}
////////////////////////////////////////////////////////////////////
///////// // The one and only CTestApp object
CTestApp theApp;
////////////////////////////////////////////////////////////////////
///////// // CTestApp initialization
BOOL CTestApp::InitInstance()
{
AfxEnableControlContainer();
//Standard initialization
//If you are not using these features and wish to reduce the size //of your final executable, you should remove from the following //the specific initialization routines you do not need.
aa:if(dlg.DoModal() == IDOK)
{
if(!m_bAccept)
{
AfxMessageBox(" 用户名或密码错误,请重新输入~");
goto aa;
}
}
else
return FALSE;
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared
DLL #else
Enable3dControlsStatic(); // Call this when linking to
MFC statically #endif
//Change the registry key under which our settings are stored.
//TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options
(including MRU)
//Register the application's document templates. Document templates
24
//serve as the connection between documents, frame windows
and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMainFrame), // main SDI frame
window RUNTIME_CLASS(CTestView));
AddDocTemplate(pDocTemplate);
//Parse command line for standard shell commands, DDE, file
open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo);
//Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
////////////////////////////////////////////////////////////////////
/////////
//CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
//ClassWizard generated virtual function
overrides //{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL
25
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//App command to run the dialog void CTestApp::OnAppAbout() { CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
//testDoc.cpp : implementation of the CTestDoc class
//
#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "database.h"
#include "field.h"
26
#include "index.h"
#include "tabledef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
CStringArray m_arrayFieldName;
CStringArray m_arrayFieldValue; CWordArray m_arrayFieldSize; CString m_strDatabase;
CString m_strConnect;
CString m_strQuery;
int m_nRowCount;
int m_nFields;
int m_nTables;
int m_nRows = 0;
extern int m_nPurview;
extern CString m_strOldCode;
extern CString m_strUserID;
extern CString m_strNewCode;
extern BOOL m_bAccept;
extern int m_nTableNO;
extern CString m_strReaderID;
extern CString m_strBookNumber; extern int m_nFieldRow;
int flag;
////////////////////////////////////////////////////////////////////
/////////
// CTestDoc
void DaoErrorMsg(CDaoException* e) {
char errorMsg[301];
wsprintf(errorMsg, "DAO error %d, SOURCE = %s, DESCR = %s", e->m_pErrorInfo->m_lErrorCode,
(const char*) e->m_pErrorInfo->m_strSource,
(const char*) e->m_pErrorInfo->m_strDescription);
AfxMessageBox(errorMsg);
}
IMPLEMENT_DYNCREATE(CTestDoc, CDocument)
27
BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
//{{AFX_MSG_MAP(CTestDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////////////////////////////// /////////
//CTestDoc construction/destruction
CTestDoc::CTestDoc()
{
m_arrayTableName.SetSize(5);
m_arrayTableName[0] = " 读者信息 ";
m_arrayTableName[1] = " 库存图书 ";
m_arrayTableName[2] = " 员工信息 ";
m_arrayTableName[3] = " 库存图书 ";
m_arrayTableName[4] = "";
m_arrayField.SetSize(3);
m_arrayField[0] = "ID"; m_arrayField[1]
= " 书名 "; m_arrayField[2] = " 借阅日
期 ";
}
CTestDoc::~CTestDoc()
{
if(m_database.IsOpen())
CloseMdb();
}
BOOL CTestDoc::OnNewDocument()
{ if (!CDocument::OnNewDocument())
return FALSE;
ConnectMdb();
return TRUE;
}
//////////////////////////////////////////////////////////////////// /////////
//CTestDoc
serialization 28
void CTestDoc::Serialize(CArchive& ar)
{ if (ar.IsStoring())
{
//TODO: add storing code here
}
else
{
//TODO: add loading code here
}
}
//////////////////////////////////////////////////////////////////// /////////
//CTestDoc diagnostics
#ifdef _DEBUG
void CTestDoc::AssertValid() const
{ CDocument::AssertValid();
}
void CTestDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
////////////////////////////////////////////////////////////////////
/////////
// CTestDoc commands
void CTestDoc::ConnectMdb()
{
int nTables;
int n = 0;
BeginWaitCursor();
try {
// nonexclusive, read-only
m_database.Open(m_strDatabase, FALSE, FALSE);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
EndWaitCursor();
e->Delete();
return;
29
}
m_strDatabase.Empty();
m_strDatabase = m_database.GetName();
TRACE("database name = %s, connect = %s\n",
(const char*) m_strDatabase, (const char*) m_strConnect);
nTables = m_database.GetTableDefCount();
CDaoTableDefInfo tdi;
for(int i = 0;i < nTables;i ++)
{
m_database.GetTableDefInfo(i,tdi);
TRACE("table name = %s\n",(const char*)tdi.m_strName);
if (tdi.m_strName.Left(4) != "MSys") { m_nTables ++;
}
}
tbDef = new CDaoTableDef(&m_database);
EndWaitCursor();
}
void CTestDoc::OpenMdb()
{
BeginWaitCursor();
int i = 0;
m_arrayTableName[4] = m_strReaderID;
m_strQuery.Format("select * from [%s]",
m_arrayTableName[m_nTableNO]);
m_pRecordset = new CDaoRecordset(&m_database);
try {
m_pRecordset->Open(dbOpenDynaset, m_strQuery);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
UpdateAllViews(NULL);
e->Delete();
return;
}
if(!m_pRecordset->IsBOF())
{
m_pRecordset->MoveLast();
}
m_nRowCount = m_pRecordset->GetAbsolutePosition() + 2; TRACE("m_nRowCount = %d\n",m_nRowCount); m_nFields = (int)m_pRecordset->GetFieldCount();
m_arrayFieldName.SetSize(m_nFields);
m_arrayFieldSize.SetSize(m_nFields); 30
CDaoFieldInfo fi;
for(i = 0;i < m_nFields;i ++)
{
m_pRecordset->GetFieldInfo(i,fi);
m_arrayFieldName[i] = fi.m_strName;
m_arrayFieldSize[i] =
(short)max(min(fi.m_lSize,50),fi.m_strName.GetLength());
TRACE("field name = %s\n",fi.m_strName);
}
UpdateAllViews(NULL);
EndWaitCursor();
}
void CTestDoc::CloseMdb() {
m_pRecordset->Close();
m_database.Close();
m_pRecordset = NULL;
m_nRowCount = 0;
m_nFields = 0;
m_nTables = 0;
UpdateAllViews(NULL);
}
void CTestDoc::OnUserNext() {
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows - 1);
flag = 0;
}
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->MoveLast();
GetValue();
}
void CTestDoc::OnUserPrev() {
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
31
m_pRecordset->SetAbsolutePosition(m_nRows - 1); flag = 0;
}
m_pRecordset->MovePrev();
if(m_pRecordset->IsBOF())
m_pRecordset->MoveFirst();
GetValue();
}
void CTestDoc::OnUserFirst() {
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows - 1); flag = 0;
}
m_pRecordset->MoveFirst();
GetValue();
}
void CTestDoc::OnUserLast() {
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows - 1); flag = 0;
}
m_pRecordset->MoveLast();
GetValue();
}
void CTestDoc::OnUserAdd() {
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows - 1);
flag = 0;
}
32
try
{
m_pRecordset->AddNew();
SetValue();
m_pRecordset->Update();
m_nRowCount ++;
}
catch(CDaoException *e)
{
CString strMessage = _T("Couldn't add the record--Exception:"); strMessage += e->m_pErrorInfo->m_strDescription; AfxMessageBox(strMessage);
e->Delete();
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserDel()
{
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows - 1);
flag = 0;
}
if(m_pRecordset->IsBOF() || m_pRecordset->IsEOF())
return;
try
{
m_pRecordset->Delete();
m_nRowCount --;
}
catch(CDaoException *e)
{
CString strMessage = _T("Couldn't delete current record-- Exception:");
strMessage += e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
return;
}
UpdateAllViews(NULL);
}
33
void CTestDoc::OnUserEdit() {
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows - 1);
flag = 0;
}
try
{
m_pRecordset->Edit();
SetValue();
m_pRecordset->Update();
}
catch(CDaoException *e)
{
CString strMessage = _T("Couldn't modify current record-- Exception:");
strMessage += e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserRequery() {
try
{
m_pRecordset->Requery();
UpdateAllViews(NULL);
}
catch(CDaoException *e)
{
CString strMessage = _T("Couldn't requery current record-- Exception:");
strMessage += e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
if(flag == 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
34
m_pRecordset->SetAbsolutePosition(m_nRows - 1);
flag = 0;
}
}
void CTestDoc::OnUserAddTable()
{
CDaoTableDef *m_newdef;
m_newdef = NULL;
if(createNewTableDef(&m_database,&m_newdef,m_arrayFieldValue[5])) {
OnUserAddField(m_newdef);
appendTableDef(&m_database, m_newdef);
}
}
void CTestDoc::OnUserAddField(CDaoTableDef *newdef)
{ CDaoFieldInfo m_newfieldinfo;
int i;
for(i = 0;i < 3;i ++)
{
m_newfieldinfo.m_strName = _T(m_arrayField[i]);
m_newfieldinfo.m_nType = dbText;
m_newfieldinfo.m_lSize = 100;
m_newfieldinfo.m_strDefaultValue = _T("");
m_newfieldinfo.m_bRequired = FALSE;
m_newfieldinfo.m_lAttributes = dbUpdatableField;
m_newfieldinfo.m_bAllowZeroLength = TRUE;
m_newfieldinfo.m_strValidationRule = _T("");
m_newfieldinfo.m_strValidationText = _T("");
createNewField(newdef,&m_newfieldinfo);
}
}
void CTestDoc::OnUserGetValue()
{
try{
m_pRecordset->SetAbsolutePosition(m_nRows - 1);
if(m_pRecordset->GetAbsolutePosition() == (m_nRows - 1)) {
GetValue();
}
}
35
catch(CDaoException *e)
{
if(e->m_pErrorInfo->m_lErrorCode == 3167) AfxMessageBox("***RECORD DELETED***");
else{
m_pRecordset->MoveLast();
}
e->Delete();
}
}
void CTestDoc::GetValue()
{
COleVariant var;
if(m_arrayFieldValue.GetSize() != m_nFields)
m_arrayFieldValue.SetSize(m_nFields);
for(int i = 0;i < m_nFields;i ++)
{
var = m_pRecordset->GetFieldValue(i);
switch(var.vt)
{
case VT_BSTR:
m_arrayFieldValue[i] = (LPCSTR) var.bstrVal; // narrow characters in DAO
break;
case VT_I2:
m_arrayFieldValue[i].Format("%d", (int) var.iVal);
break;
case VT_I4:
m_arrayFieldValue[i].Format("%d", var.lVal);
break;
case VT_R4:
m_arrayFieldValue[i].Format("%10.2f", (double) var.fltVal);
break;
case VT_R8:
m_arrayFieldValue[i].Format("%10.2f", var.dblVal);
break;
case VT_CY:。

相关文档
最新文档