📄 searchbookdlg.cpp
字号:
// SearchbookDlg.cpp : implementation file
//
#include "stdafx.h"
#include "libmis.h"
#include "SearchbookDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSearchbookDlg dialog
CSearchbookDlg::CSearchbookDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSearchbookDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSearchbookDlg)
m_strVal = _T("");
//}}AFX_DATA_INIT
}
void CSearchbookDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSearchbookDlg)
DDX_Control(pDX, IDC_LIST1, m_cList);
DDX_Control(pDX, IDC_COMBO1, m_cCombo);
DDX_Text(pDX, IDC_EDIT1, m_strVal);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSearchbookDlg, CDialog)
//{{AFX_MSG_MAP(CSearchbookDlg)
ON_BN_CLICKED(IDC_BUTSEARCH, OnButsearch)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSearchbookDlg message handlers
void CSearchbookDlg::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 book WHERE ";
switch (nIndex)
{
case 0: strSql+="ID LIKE '%"+m_strVal+="%'";break;
case 1: strSql+="name LIKE '%"+m_strVal+="%'";break;
case 2: strSql+="type="+m_strVal+="";break;
case 3: strSql+="pub LIKE '%"+m_strVal+="%'";break;
default: return;
}
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 ("type")));
m_cList.SetItemText (nIndex,3,(_bstr_t)(m_pRecordset->GetCollect ("num")));
m_cList.SetItemText (nIndex,4,(_bstr_t)(m_pRecordset->GetCollect ("pub")));
nIndex++;
m_pRecordset->MoveNext ();
}
m_pRecordset->Close ();
}
BOOL CSearchbookDlg::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,70);
m_cList.InsertColumn (1,"书名",LVCFMT_CENTER,170);
m_cList.InsertColumn (2,"类别",LVCFMT_CENTER,60);
m_cList.InsertColumn (3,"数量",LVCFMT_CENTER,60);
m_cList.InsertColumn (4,"出版社",LVCFMT_CENTER,260);
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 book";
m_pRecordset->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
_variant_t vName,vPsw;
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 ("type")));
m_cList.SetItemText (nIndex,3,(_bstr_t)(m_pRecordset->GetCollect ("num")));
m_cList.SetItemText (nIndex,4,(_bstr_t)(m_pRecordset->GetCollect ("pub")));
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 CSearchbookDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
m_pConnection->Close ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -