📄 readertypelistdlg.cpp
字号:
// ReaderTypeListDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LibraryM.h"
#include "ReaderTypeListDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReaderTypeListDlg dialog
CReaderTypeListDlg::CReaderTypeListDlg(CWnd* pParent /*=NULL*/)
: CDialog(CReaderTypeListDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CReaderTypeListDlg)
m_BorrowedAmount = _T("");
m_UsefulLife = _T("");
m_ReaderTypeName = _T("");
m_ReaderTypeID = _T("");
m_DaysLimit = _T("");
//}}AFX_DATA_INIT
}
void CReaderTypeListDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReaderTypeListDlg)
DDX_Control(pDX, IDC_LIST_READERTYPE, m_ReaderTypeList);
DDX_Text(pDX, IDC_EDIT_BAMOUNT, m_BorrowedAmount);
DDX_Text(pDX, IDC_EDIT_USEFULLIFE, m_UsefulLife);
DDX_Text(pDX, IDC_EDIT_READERTYPENAME, m_ReaderTypeName);
DDX_Text(pDX, IDC_EDIT_READERTYPEID, m_ReaderTypeID);
DDX_Text(pDX, IDC_EDIT_DAYSLIMIT, m_DaysLimit);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CReaderTypeListDlg, CDialog)
//{{AFX_MSG_MAP(CReaderTypeListDlg)
ON_BN_CLICKED(IDC_READERTYPELIST_INTERT_BTN, OnReadertypelistIntertBtn)
ON_BN_CLICKED(IDC_READERTYPELIST_CANCEL_BTN2, OnReadertypelistCancelBtn2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReaderTypeListDlg message handlers
BOOL CReaderTypeListDlg::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_ReaderTypeList.InsertColumn(0,"ID",LVCFMT_LEFT,100);
m_ReaderTypeList.InsertColumn(1,"TypeName",LVCFMT_CENTER,140);
m_ReaderTypeList.InsertColumn(2,"CanBorrowAmount(本)",LVCFMT_CENTER,160);
m_ReaderTypeList.InsertColumn(3,"DaysLimit(天)",LVCFMT_CENTER,110);
m_ReaderTypeList.InsertColumn(4,"usefullife(年)",LVCFMT_CENTER,110);
m_ReaderTypeList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
InitListCtrl();
return TRUE;
}
void CReaderTypeListDlg::InitListCtrl()
{
HRESULT hr;
_RecordsetPtr pReaderTypeList;
hr=pReaderTypeList.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 ReaderType";
try
{
hr=pReaderTypeList->Open(_variant_t(strSql),m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
if(SUCCEEDED(hr))
{
while(!pReaderTypeList->adoEOF)
{
getvar=pReaderTypeList->GetCollect("ReaderTypeID");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_ReaderTypeList.InsertItem(curItem,strValue);
getvar=pReaderTypeList->GetCollect("TypeName");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_ReaderTypeList.SetItemText(curItem,1,strValue);
getvar=pReaderTypeList->GetCollect("BorrowedAmount");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_ReaderTypeList.SetItemText(curItem,2,strValue);
getvar=pReaderTypeList->GetCollect("DaysLimit");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_ReaderTypeList.SetItemText(curItem,3,strValue);
getvar=pReaderTypeList->GetCollect("UsefulLife");
if(getvar.vt!=VT_NULL)
{
strValue=(LPCSTR)_bstr_t(getvar);
}
else
strValue=" ";
m_ReaderTypeList.SetItemText(curItem,4,strValue);
pReaderTypeList->MoveNext();
curItem++;
}
}
else
{
AfxMessageBox("open recordset failed!");
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pReaderTypeList->Close();
pReaderTypeList=NULL;
}
void CReaderTypeListDlg::OnReadertypelistIntertBtn()
{
UpdateData(TRUE); //得到输入
//判断是否输入了要插入的数据项
if(m_ReaderTypeID.IsEmpty() || m_ReaderTypeName.IsEmpty() ||m_BorrowedAmount.IsEmpty()
|| m_DaysLimit.IsEmpty() || m_UsefulLife.IsEmpty())
{
AfxMessageBox("empty input!");
return;
}
_RecordsetPtr pset;
pset.CreateInstance(_uuidof(Recordset));
HRESULT hr;
try
{
hr=pset->Open("select * from ReaderType",m_pConnection.GetInterfacePtr(),
adOpenDynamic,adLockOptimistic,adCmdText);
//添加记录
if(SUCCEEDED(hr))
{
pset->AddNew();
//写入每个字段值
pset->PutCollect("ReaderTypeID",_variant_t(m_ReaderTypeID));
pset->PutCollect("TypeName",_variant_t(m_ReaderTypeName));
pset->PutCollect("BorrowedAmount",_variant_t(m_BorrowedAmount));
pset->PutCollect("DaysLimit",_variant_t(m_DaysLimit));
pset->PutCollect("UsefulLife",_variant_t(m_UsefulLife));
pset->Update();
}
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
pset->Close();
pset=NULL;
m_ReaderTypeList.DeleteAllItems();
InitListCtrl();
}
void CReaderTypeListDlg::OnReadertypelistCancelBtn2()
{
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -