📄 bookedit.cpp
字号:
// BOOKEDIT.cpp : implementation file
//
#include "stdafx.h"
#include "图书馆系统.h"
#include "BOOKEDIT.h"
#include "MainFrm.h"
#include "LeftTreeView.h"
#include "RightListView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// BOOKEDIT dialog
BOOKEDIT::BOOKEDIT(CWnd* pParent /*=NULL*/)
: CDialog(BOOKEDIT::IDD, pParent)
{
//{{AFX_DATA_INIT(BOOKEDIT)
m_addbookid = _T("");
m_addbookname = _T("");
m_addbookpub = _T("");
m_addbookwr = _T("");
m_addbookpt1 = _T("");
m_addbookpt2 = _T("");
m_addbookprice = _T("");
//}}AFX_DATA_INIT
}
void BOOKEDIT::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(BOOKEDIT)
DDX_Text(pDX, IDC_EDIT_bookid, m_addbookid);
DDX_Text(pDX, IDC_EDIT_bookname, m_addbookname);
DDX_Text(pDX, IDC_EDIT_bookpub, m_addbookpub);
DDX_Text(pDX, IDC_EDIT_bookwriter, m_addbookwr);
DDX_Text(pDX, IDC_EDIT_pattern1, m_addbookpt1);
DDX_Text(pDX, IDC_EDIT_pattern2, m_addbookpt2);
DDX_Text(pDX, IDC_EDIT_ADD_PRICE, m_addbookprice);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(BOOKEDIT, CDialog)
//{{AFX_MSG_MAP(BOOKEDIT)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// BOOKEDIT message handlers
BOOL BOOKEDIT::OpenRecordSet(_RecordsetPtr &recPtr, CString &strSQL)
{
CMyApp* pApp=(CMyApp*)AfxGetApp();
//创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
//在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
//因为它有时会经常出现一些想不到的错误
try
{
//从数据库中打开表
recPtr->Open(strSQL.AllocSysString(),
pApp->m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch (_com_error e)
{
CString strError;
strError.Format("警告:打开数据表时发生异常。 错误信息: %s",\
e.ErrorMessage());
AfxMessageBox(strError);
return FALSE;
}
return TRUE;
}
void BOOKEDIT::ResetCtrlValue()
{
//初始化各控件值
m_addbookid = _T("");
m_addbookname = _T("");
m_addbookpub = _T("");
m_addbookwr = _T("");
m_addbookpt1 = _T("");
m_addbookpt2 = _T("");
UpdateData(FALSE);
}
BOOL BOOKEDIT::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//设置对话框标题
SetWindowText(m_strTitle);
if(bEdit)
{
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT_bookid);
//当前对话框是否用来编辑,图书ID不允许编辑
pEdit->EnableWindow(!bEdit);
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CRightListView* pRightView;
CLeftTreeView* pLeftView;
//获取当前记录的位置游标
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
pRightView=(CRightListView*)pMainFrm->m_wndSplitter.GetPane(0,1);
//寻找当前选中的记录的位置
POSITION pos=pRightView->m_listCtrl.GetFirstSelectedItemPosition();
int iIndex=pRightView->m_listCtrl.GetNextSelectedItem(pos);
if(iIndex==-1)
{
AfxMessageBox("没有视图表里的内容");
CDialog::OnCancel();
return FALSE;
}
//获取该项在数据库中的序号
CString strNumber,strName,strfirClass;
strNumber=pRightView->m_listCtrl.GetItemText(iIndex,0);
strName=pRightView->m_listCtrl.GetItemText(iIndex,1);
strfirClass=pRightView->m_listCtrl.GetItemText(iIndex,3);
//设置查询语句
CString strSQL;
strSQL.Format("select * from 图书信息情况 where 图书ID='%s' ",\
strNumber);
//打开记录集 选择表名
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
CDialog::OnCancel();
return FALSE;
}
//应该只有一条记录
m_pRecordset->MoveFirst();
m_addbookid=pLeftView->VariantToCString(m_pRecordset->GetCollect("图书ID"));
m_addbookname=pLeftView->VariantToCString(m_pRecordset->GetCollect("图书名称"));
m_addbookwr=pLeftView->VariantToCString(m_pRecordset->GetCollect("作者"));
m_addbookpub=pLeftView->VariantToCString(m_pRecordset->GetCollect("出版社"));
m_addbookprice=pLeftView->VariantToCString(m_pRecordset->GetCollect("图书价格"));
m_addbookpt1=pLeftView->VariantToCString(m_pRecordset->GetCollect("一级类型"));
m_addbookpt2=pLeftView->VariantToCString(m_pRecordset->GetCollect("二级类型"));
UpdateData(FALSE);
m_pRecordset->Close();
m_pRecordset=NULL;
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void BOOKEDIT::OnOK()
{
// TODO: Add extra validation here
//定义变量
CString strSQL;
//上述准备完毕,下面开始插入内容
if(bEdit)
{
//更新控件变量的值
UpdateData(TRUE);
//姓名、类别为空时返回
//删除空格
m_addbookname.Remove(' ');
m_addbookpt1.Remove(' ');
m_addbookpt2.Remove(' ');
if(m_addbookname.IsEmpty()||m_addbookpt1.IsEmpty()||m_addbookpt2.IsEmpty())
{
AfxMessageBox("图书名称和类别不能为空");
return;
}
//打开记录集 选择表名
strSQL.Format("select * from 图书信息情况 where 图书ID='%s' ",m_addbookid);
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return;
}
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
//上述准备完毕,下面开始插入内容
try
{
//m_pRecordset->PutCollect("图书ID",_variant_t(m_addbookid));
m_pRecordset->PutCollect("图书名称",_variant_t(m_addbookname));
m_pRecordset->PutCollect("作者",_variant_t(m_addbookwr));
m_pRecordset->PutCollect("出版社",_variant_t(m_addbookpub));
m_pRecordset->PutCollect("图书价格",_variant_t(m_addbookprice));
m_pRecordset->PutCollect("一级类型",_variant_t(m_addbookpt1));
m_pRecordset->PutCollect("二级类型",_variant_t(m_addbookpt2));
//更新数据库
m_pRecordset->Update();
//当前记录移动到最后
m_pRecordset->MoveLast();
}
catch(_com_error e)
{
CString strError;
strError.Format("警告:插入信息时发生异常。错误信息: %s",\
e.ErrorMessage());
AfxMessageBox(strError);
}
//上述准备完毕,下面开始插入内容
strSQL.Format("select * from 借阅信息表 where 图书ID='%s' ",m_addbookid);
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return;
}
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
while(!m_pRecordset->adoEOF)
{
try
{
m_pRecordset->PutCollect("图书名称",_variant_t(m_addbookname));
}
catch(_com_error e)
{
CString strError;
strError.Format("警告:插入信息时发生异常。错误信息: %s",\
e.ErrorMessage());
AfxMessageBox(strError);
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pRecordset=NULL;
//更新树、表
strSQL="select * from 图书信息情况";
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CRightListView* pRightView;
CLeftTreeView* pLeftView;
//获取当前记录的位置游标
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
pRightView=(CRightListView*)pMainFrm->m_wndSplitter.GetPane(0,1);
pLeftView->ShowTree();
pRightView->ListShow(strSQL);
}
else
{
//更新控件变量的值
UpdateData(TRUE);
//打开记录集 选择表名
strSQL="select * from 图书信息情况";
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return;
}
//姓名、类别为空时返回
m_addbookid.Remove(' ');//删除空格
m_addbookname.Remove(' ');
m_addbookpt1.Remove(' ');
m_addbookpt2.Remove(' ');
if(m_addbookid.IsEmpty()||m_addbookname.IsEmpty()||m_addbookpt1.IsEmpty()||m_addbookpt2.IsEmpty())
{
AfxMessageBox("图书ID、名称和类别不能为空");
return;
}
try
{
//添加数据,姓名只允许添加不允许更改
m_pRecordset->AddNew();
m_pRecordset->PutCollect("图书ID",_variant_t(m_addbookid));
m_pRecordset->PutCollect("图书名称",_variant_t(m_addbookname));
m_pRecordset->PutCollect("作者",_variant_t(m_addbookwr));
m_pRecordset->PutCollect("出版社",_variant_t(m_addbookpub));
m_pRecordset->PutCollect("图书价格",_variant_t(m_addbookprice));
m_pRecordset->PutCollect("一级类型",_variant_t(m_addbookpt1));
m_pRecordset->PutCollect("二级类型",_variant_t(m_addbookpt2));
//更新数据库
m_pRecordset->Update();
//当前记录移动到最后
m_pRecordset->MoveLast();
m_pRecordset->Close();
m_pRecordset=NULL;
}
catch(_com_error e)
{
CString strError;
strError.Format("警告: 插入信息时发生异常。 错误信息: %s",\
e.ErrorMessage());
AfxMessageBox(strError);
return;
}
AfxMessageBox("插入成功!");
//更新树、表
strSQL="select * from 图书信息情况";
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CRightListView* pRightView;
CLeftTreeView* pLeftView;
//获取当前记录的位置游标
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
pRightView=(CRightListView*)pMainFrm->m_wndSplitter.GetPane(0,1);
pLeftView->ShowTree();
pRightView->ListShow(strSQL);
}
//将各控件值置空
ResetCtrlValue();
CDialog::OnOK();
}
void BOOKEDIT::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -