?? borrowlistdlg.cpp
字號:
// BorrowListDlg.cpp : implementation file
//
#include "stdafx.h"
#include "librarym.h"
#include "BorrowListDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBorrowListDlg dialog
CBorrowListDlg::CBorrowListDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBorrowListDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBorrowListDlg)
m_BrwBookID = _T("");
m_BrwReaderID = _T("");
//}}AFX_DATA_INIT
}
void CBorrowListDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBorrowListDlg)
DDX_Control(pDX, IDC_LIST_BORROW, m_BorrowList);
DDX_Text(pDX, IDC_EDIT_BBOOKID, m_BrwBookID);
DDX_Text(pDX, IDC_EDIT_BREADERID, m_BrwReaderID);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBorrowListDlg, CDialog)
//{{AFX_MSG_MAP(CBorrowListDlg)
ON_BN_CLICKED(IDC_BORROWLIST_SEARCH_BTN, OnBorrowlistSearchBtn)
ON_BN_CLICKED(IDC_BORROWLIST_CANCEL_BTN2, OnBorrowlistCancelBtn2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBorrowListDlg message handlers
BOOL CBorrowListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//初始化數據庫連接
HRESULT result;
try
{
//實例化連接對象
result=m_pConnection.CreateInstance(_uuidof(Connection));
if(SUCCEEDED(result))
{
//設置連接屬性為UDL文件
m_pConnection->ConnectionString="File Name=LIBRARYM.udl";
//設置等待連接打開的時間為20s
m_pConnection->ConnectionTimeout=20;
result=m_pConnection->Open("","","",adConnectUnspecified);
if(FAILED(result))
{
AfxMessageBox("open failed");
return TRUE;
}
}
else
{
AfxMessageBox("createinstance of connection failed!");
return TRUE;
}
}
catch(_com_error e)
{
//輸出異常信息
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
AfxMessageBox(bstrSource+bstrDescription);
return TRUE;
}
//初始化列表
m_BorrowList.InsertColumn(0,"BorrowNo",LVCFMT_LEFT,100);
m_BorrowList.InsertColumn(1,"BookID",LVCFMT_CENTER,100);
m_BorrowList.InsertColumn(2,"ReaderID",LVCFMT_CENTER,100);
m_BorrowList.InsertColumn(3,"BorrowDate",LVCFMT_CENTER,100);
m_BorrowList.InsertColumn(4,"ReturnDate",LVCFMT_CENTER,100);
m_BorrowList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
InitListCtrl();
return TRUE;
}
void CBorrowListDlg::InitListCtrl()
{
HRESULT hr;
_RecordsetPtr pBorrowList;
hr=pBorrowList.CreateInstance(_uuidof(Recordset));
if(FAILED(hr))
{
AfxMessageBox("createinstance of recordset failed!\ncan't initiate borrowlist control");
return;
}
CString strSql;
_variant_t getvar;
CString strValue;
int curItem=0;
strSql="select * from BorrowRelation";
try
{
hr=pBorrowList->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
if(SUCCEEDED(hr))
{
while(!pBorrowList->adoEOF)
{
getvar=pBorrowList->GetCollect("BorrowNo");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.InsertItem(curItem,strValue);
getvar=pBorrowList->GetCollect("BookID");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,1,strValue);
getvar=pBorrowList->GetCollect("ReaderID");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,2,strValue);
getvar=pBorrowList->GetCollect("BorrowDate");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,3,strValue);
getvar=pBorrowList->GetCollect("ReturnDate");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,4,strValue);
pBorrowList->MoveNext();
curItem++;
}
}
else
{
AfxMessageBox("open recordset failed!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pBorrowList->Close();
pBorrowList=NULL;
}
void CBorrowListDlg::OnBorrowlistSearchBtn()
{
UpdateData(TRUE);
CString temp;
bool bset=false;
CString strSql="select * from BorrowRelation where ";
if(!m_BrwBookID.IsEmpty())
{
temp.Format("BookID='%s'",m_BrwBookID);
bset=true;
}
if(!temp.IsEmpty())
strSql+=temp;
temp="";
if(!m_BrwReaderID.IsEmpty() )
{
if(bset)
{
temp.Format("and ReaderID='%s'",m_BrwReaderID);
}
else
{
temp.Format("ReaderID='%s'",m_BrwReaderID);
bset=true;
}
}
if(!temp.IsEmpty())
strSql+=temp;
if(!bset)
{
AfxMessageBox("empty input!");
return;
}
_RecordsetPtr pBorrowList;
pBorrowList.CreateInstance(_uuidof(Recordset));
_variant_t getvar;
CString strValue;
int curItem=0;
try
{
HRESULT hr;
hr=pBorrowList->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
if(SUCCEEDED(hr))
{
m_BorrowList.DeleteAllItems();
while(!pBorrowList->adoEOF)
{
getvar=pBorrowList->GetCollect("BorrowNo");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.InsertItem(curItem,strValue);
getvar=pBorrowList->GetCollect("BookID");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,1,strValue);
getvar=pBorrowList->GetCollect("ReaderID");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,2,strValue);
getvar=pBorrowList->GetCollect("BorrowDate");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,3,strValue);
getvar=pBorrowList->GetCollect("ReturnDate");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_BorrowList.SetItemText(curItem,4,strValue);
pBorrowList->MoveNext();
curItem++;
}
}
else
{
AfxMessageBox("open recordset failed!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pBorrowList->Close();
pBorrowList=NULL;
}
void CBorrowListDlg::OnBorrowlistCancelBtn2()
{
CDialog::OnCancel();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -