📄 borrowlist.cpp
字号:
// BORROWLIST.cpp : implementation file
//
#include "stdafx.h"
#include "图书馆系统.h"
#include "BORROWLIST.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// BORROWLIST dialog
BORROWLIST::BORROWLIST(CWnd* pParent /*=NULL*/)
: CDialog(BORROWLIST::IDD, pParent)
{
//{{AFX_DATA_INIT(BORROWLIST)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void BORROWLIST::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(BORROWLIST)
DDX_Control(pDX, IDC_BORROW_LIST, m_borListCtrl);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(BORROWLIST, CDialog)
//{{AFX_MSG_MAP(BORROWLIST)
ON_BN_CLICKED(IDC_BOOK_BS, OnBookBs)
ON_BN_CLICKED(IDC_BOOK_GH, OnBookGh)
ON_BN_CLICKED(IDC_REBORROW, OnReborrow)
ON_BN_CLICKED(IDC_UPDATELIST, OnUpdatelist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// BORROWLIST message handlers
BOOL BORROWLIST::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;
}
BOOL BORROWLIST::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CRect rect;
m_borListCtrl.GetClientRect(&rect);
//设置列表控件风格
DWORD dwStyle=::GetWindowLong(m_hWnd,GWL_STYLE);
dwStyle|=LVS_REPORT|LVS_SHOWSELALWAYS|LVS_EDITLABELS;
::SetWindowLong(m_hWnd,GWL_STYLE,dwStyle);
dwStyle=m_borListCtrl.GetExtendedStyle();
dwStyle|=LVS_EX_FULLROWSELECT;//LVS_EX_GRIDLINES|
//设置扩展风格
m_borListCtrl.SetExtendedStyle(dwStyle);
m_borListCtrl.InsertColumn(0, "图书ID", LVCFMT_LEFT, rect.Width()/4);
m_borListCtrl.InsertColumn(1, "图书名称", LVCFMT_LEFT, rect.Width()/4);
m_borListCtrl.InsertColumn(2, "借阅日期", LVCFMT_LEFT, rect.Width()/3);
m_borListCtrl.InsertColumn(3, "借阅次数", LVCFMT_LEFT, rect.Width()/6);
//这是改变后删除,其实应该是改变前删除才对
m_borListCtrl.DeleteAllItems();
CString strSQL;
strSQL.Format("select * from 借阅信息表 where 借阅ID='%s' ",m_getblid);
//打开数据表
if(!OpenRecordSet(m_pRecordset,strSQL))
{
return FALSE;
}
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
//循环插入
int i=0;
_variant_t varValue;
while(!m_pRecordset->adoEOF)
{
CString str;
m_borListCtrl.InsertItem (i,str);
varValue=m_pRecordset->GetFields()->GetItem("图书ID")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 0, str);
varValue=m_pRecordset->GetFields()->GetItem("图书名称")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 1, str);
varValue=m_pRecordset->GetFields()->GetItem("借阅日期")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 2, str);
varValue=m_pRecordset->GetFields()->GetItem("借阅次数")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 3, str);
i++;
m_pRecordset->MoveNext();
}
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 BORROWLIST::OnBookBs()// 图书报失
{
// TODO: Add your control notification handler code here
POSITION pos=m_borListCtrl.GetFirstSelectedItemPosition();
//获取当前记录的位置游标
int iIndex=m_borListCtrl.GetNextSelectedItem(pos);
if(iIndex==-1)
{
AfxMessageBox("请选中要报失的图书");
return;
}
CString strNumber,strSQL;
float payprice;
strNumber=m_borListCtrl.GetItemText(iIndex,0);
strSQL.Format("select * from 图书信息情况 where 图书ID='%s' ",\
strNumber);
//打开记录集 选择表名
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return ;
}
//应该只有一条记录
m_pRecordset->MoveFirst();
payprice=(float)m_pRecordset->GetCollect("图书价格");
strSQL.Format("delete * from 借阅信息表 where 图书ID='%s' ",\
strNumber);
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return ;
}
AfxMessageBox("报失成功");
strSQL.Format("书的原价是'%f',该用户须赔偿'%f'",payprice,2*payprice);
AfxMessageBox(strSQL);
}
void BORROWLIST::OnReborrow() //图书续借
{
// TODO: Add your control notification handler code here
POSITION pos=m_borListCtrl.GetFirstSelectedItemPosition();
//获取当前记录的位置游标
int iIndex=m_borListCtrl.GetNextSelectedItem(pos);
if(iIndex==-1)
{
AfxMessageBox("请选中要续借的图书");
return;
}
CString strNumber,strSQL,strtime;
strNumber=m_borListCtrl.GetItemText(iIndex,0);
strSQL.Format("select * from 借阅信息表 where 图书ID='%s' ",\
strNumber);
//打开记录集 选择表名
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return ;
}
//应该只有一条记录
m_pRecordset->MoveFirst();
strtime=pLeftView->VariantToCString(m_pRecordset->GetCollect("借阅次数"));//最多续借2次
if(strtime=="2")
{
AfxMessageBox("您已经续借过一次了,不能再续借了!");
return ;
}
strtime="2";
//上述准备完毕,下面开始插入内容
try
{
m_pRecordset->PutCollect("借阅次数",_variant_t(strtime));
COleDateTime oleTime;
CDateTimeCtrl* pDtCtrl=(CDateTimeCtrl*)GetDlgItem(IDC_DATE_PICKER);
pDtCtrl->GetTime(oleTime);
m_pRecordset->PutCollect("借阅日期",_variant_t(oleTime));
//更新数据库
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);
}
AfxMessageBox("续借成功");
}
void BORROWLIST::OnBookGh() //图书归还
{
// TODO: Add your control notification handler code here
POSITION pos=m_borListCtrl.GetFirstSelectedItemPosition();
//获取当前记录的位置游标
int iIndex=m_borListCtrl.GetNextSelectedItem(pos);
if(iIndex==-1)
{
AfxMessageBox("请选中要归还的图书");
return;
}
CString strNumber,strSQL,strtime;
strNumber=m_borListCtrl.GetItemText(iIndex,0);
strSQL.Format("delete * from 借阅信息表 where 图书ID='%s' ",\
strNumber);
if (MessageBox("是否归还该图书?","确认",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
return;
}
//打开记录集 选择表名
if(!OpenRecordSet(m_pRecordset,strSQL))
{
AfxMessageBox("没有成功打开数据表");
return ;
}
AfxMessageBox("归还操作成功!");
}
void BORROWLIST::OnUpdatelist()
{
// TODO: Add your control notification handler code here
m_borListCtrl.DeleteAllItems();
CString strSQL;
strSQL.Format("select * from 借阅信息表 where 借阅ID='%s' ",m_getblid);
//打开数据表
if(!OpenRecordSet(m_pRecordset,strSQL))
{
return;
}
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
//循环插入
int i=0;
_variant_t varValue;
while(!m_pRecordset->adoEOF)
{
CString str;
m_borListCtrl.InsertItem (i,str);
varValue=m_pRecordset->GetFields()->GetItem("图书ID")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 0, str);
varValue=m_pRecordset->GetFields()->GetItem("图书名称")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 1, str);
varValue=m_pRecordset->GetFields()->GetItem("借阅日期")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 2, str);
varValue=m_pRecordset->GetFields()->GetItem("借阅次数")->Value;
str=pLeftView->VariantToCString(varValue);
m_borListCtrl.SetItemText (i, 3, str);
i++;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pRecordset=NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -