?? 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 + -