📄 readergldlg.cpp
字号:
// ReaderglDlg.cpp : implementation file
//
#include "stdafx.h"
#include "libmis.h"
#include "ReaderglDlg.h"
#include "ReadereditDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReaderglDlg dialog
CReaderglDlg::CReaderglDlg(CWnd* pParent /*=NULL*/)
: CDialog(CReaderglDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CReaderglDlg)
m_strVal = _T("");
//}}AFX_DATA_INIT
}
void CReaderglDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReaderglDlg)
DDX_Control(pDX, IDC_COMBO1, m_cCombo);
DDX_Control(pDX, IDC_LIST1, m_cList);
DDX_Text(pDX, IDC_EDIT1, m_strVal);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CReaderglDlg, CDialog)
//{{AFX_MSG_MAP(CReaderglDlg)
ON_BN_CLICKED(IDC_BUTSEARCH, OnButsearch)
ON_BN_CLICKED(ID_ADD, OnAdd)
ON_BN_CLICKED(ID_DEL, OnDel)
ON_BN_CLICKED(ID_UPDATE, OnUpdate)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReaderglDlg message handlers
BOOL CReaderglDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CString strSql;
m_cList.SetExtendedStyle (LVS_EX_TRACKSELECT|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_cList.InsertColumn (0,"读者ID",LVCFMT_CENTER,100);
m_cList.InsertColumn (1,"姓名",LVCFMT_CENTER,170);
m_cList.InsertColumn (2,"性别",LVCFMT_CENTER,60);
m_cList.InsertColumn (3,"有效性",LVCFMT_CENTER,80);
try
{
if (!m_pConnection.CreateInstance(__uuidof(Connection)))
m_pConnection->Open("Provider=Microsoft.JET.OLEDB.4.0;Data Source=lib.mdb","","",adModeUnknown);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return FALSE;
}
try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
strSql="SELECT * FROM reader";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
int nIndex=0;
while (!m_pRecordset->adoEOF)
{
m_cList.InsertItem (nIndex,(_bstr_t)(m_pRecordset->GetCollect ("id")));
m_cList.SetItemText (nIndex,1,(_bstr_t)(m_pRecordset->GetCollect ("name")));
m_cList.SetItemText (nIndex,2,(_bstr_t)(m_pRecordset->GetCollect ("sex")));
m_cList.SetItemText (nIndex,3,(_bstr_t)(m_pRecordset->GetCollect ("valid")));
nIndex++;
m_pRecordset->MoveNext ();
}
m_pRecordset->Close ();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CReaderglDlg::OnButsearch()
{
// TODO: Add your control notification handler code here
UpdateData ();
m_strVal.TrimLeft ();
m_strVal.TrimRight ();
int nIndex=m_cCombo.GetCurSel ();
CString strSql="SELECT * FROM reader WHERE ";
switch (nIndex)
{
case 0: strSql+="id LIKE '%"+m_strVal+="%'";break;
case 1: strSql+="name LIKE '%"+m_strVal+="%'";break;
case 2: strSql+="sex LIKE '%"+m_strVal+="%'";break;
case 3: strSql+="valid LIKE'%"+m_strVal+="%'";break;
default: return;
}
try
{
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_cList.DeleteAllItems ();
nIndex=0;
while (!m_pRecordset->adoEOF)
{
m_cList.InsertItem (nIndex,(_bstr_t)(m_pRecordset->GetCollect ("id")));
m_cList.SetItemText (nIndex,1,(_bstr_t)(m_pRecordset->GetCollect ("name")));
m_cList.SetItemText (nIndex,2,(_bstr_t)(m_pRecordset->GetCollect ("sex")));
m_cList.SetItemText (nIndex,3,(_bstr_t)(m_pRecordset->GetCollect ("valid")));
nIndex++;
m_pRecordset->MoveNext ();
}
m_pRecordset->Close ();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
}
void CReaderglDlg::OnAdd()
{
// TODO: Add your control notification handler code here
CReadereditDlg dlg;
CString strSql;
if (IDOK==dlg.DoModal ())
{
try
{
strSql="SELECT * FROM reader WHERE id '="+dlg.m_strId+"'";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
if (m_pRecordset->adoEOF)
m_pRecordset->AddNew ();
m_pRecordset->PutCollect ("id",(_variant_t)dlg.m_strId);
m_pRecordset->PutCollect ("name",(_variant_t)dlg.m_strName);
m_pRecordset->PutCollect ("sex",(_variant_t)dlg.m_strSex);
m_pRecordset->PutCollect ("valid",(_variant_t)dlg.m_strValid);
m_pRecordset->Update();
m_pRecordset->Close ();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
AfxMessageBox ("添加成功!");
}
}
void CReaderglDlg::OnDel()
{
// TODO: Add your control notification handler code here
int nIndex;
CString strSql;
try
{
if (!m_cList.GetFirstSelectedItemPosition ())
return;
if (2==MessageBox ("确认要删除吗?","删除",MB_OKCANCEL|MB_ICONWARNING))
return;
for (nIndex=m_cList.GetItemCount ()-1;nIndex>=0;nIndex--)
{
if (m_cList.GetItemState (nIndex,LVIS_SELECTED))
{
strSql="SELECT * FROM reader WHERE id='"+m_cList.GetItemText (nIndex,0)+"'";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->Delete (adAffectCurrent);
m_pRecordset->Update();
m_cList.DeleteItem (nIndex);
m_pRecordset->Close ();
}
}
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
}
void CReaderglDlg::OnUpdate()
{
// TODO: Add your control notification handler code here
CReadereditDlg dlg;
int nIndex;
POSITION pos=m_cList.GetFirstSelectedItemPosition ();
CString strSql;
try
{
nIndex=m_cList.GetNextSelectedItem (pos);
dlg.m_strId=m_cList.GetItemText (nIndex,0);
dlg.m_strName=m_cList.GetItemText (nIndex,1);
dlg.m_strSex=m_cList.GetItemText (nIndex,2);
dlg.m_strValid=m_cList.GetItemText (nIndex,3);
if (IDOK==dlg.DoModal ())
{
strSql="SELECT * FROM reader WHERE id='"+m_cList.GetItemText (nIndex,0)+"'";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->PutCollect ("id",(_variant_t)dlg.m_strId);
m_pRecordset->PutCollect ("name",(_variant_t)dlg.m_strName);
m_pRecordset->PutCollect ("sex",(_variant_t)dlg.m_strSex);
m_pRecordset->PutCollect ("valid",(_variant_t)dlg.m_strValid);
m_pRecordset->Update();
m_pRecordset->Close ();
}
m_cList.SetItemText (nIndex,0,dlg.m_strId);
m_cList.SetItemText (nIndex,1,dlg.m_strName);
m_cList.SetItemText (nIndex,2,dlg.m_strSex);
m_cList.SetItemText (nIndex,3,dlg.m_strValid);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
}
void CReaderglDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
m_pConnection->Close ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -