📄 bookquery.cpp
字号:
// BOOKQUERY.cpp : implementation file
//
#include "stdafx.h"
#include "图书馆系统.h"
#include "BOOKQUERY.h"
#include "RightListView.h"
#include "LeftTreeView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// BOOKQUERY dialog
BOOKQUERY::BOOKQUERY(CWnd* pParent /*=NULL*/)
: CDialog(BOOKQUERY::IDD, pParent)
{
//{{AFX_DATA_INIT(BOOKQUERY)
m_strWhereValue = _T("");
//}}AFX_DATA_INIT
}
void BOOKQUERY::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(BOOKQUERY)
DDX_Text(pDX, IDC_OBJECT_EDIT, m_strWhereValue);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(BOOKQUERY, CDialog)
//{{AFX_MSG_MAP(BOOKQUERY)
ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddSQL)
ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteSQL)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// BOOKQUERY message handlers
BOOL BOOKQUERY::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//初始化组合框一
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CRightListView* pRightView;
CLeftTreeView* pLeftView;
//获取当前记录的位置游标
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
pRightView=(CRightListView*)pMainFrm->m_wndSplitter.GetPane(0,1);
//寻找当前选中的记录的位置
POSITION pos=pRightView->m_listCtrl.GetFirstSelectedItemPosition();
int iIndex=pRightView->m_listCtrl.GetNextSelectedItem(pos);
if(iIndex==-1)
{
AfxMessageBox("请点击视图表,用以选中它进行操作!");
CDialog::OnCancel();
return FALSE;
}
char chField[7][9]={"图书ID","图书名称","作者","出版社",
"图书价格","一级类型","二级类型"};
CComboBox* pCombox=(CComboBox*)GetDlgItem(IDC_OBJECT_COMBO);
for(int i=0;i<7;i++)
{
pCombox->AddString(chField[i]);
}
pCombox->SetCurSel(0);
//初始化组合框二
pCombox=(CComboBox*)GetDlgItem(IDC_RELATION_COMBO);
pCombox->AddString("=");
pCombox->AddString("like");
pCombox->SetCurSel(0);
//查询语句
m_strSql="";
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void BOOKQUERY::OnAddSQL()
{
// TODO: Add your control notification handler code here
CString strField,strRelation,strWhereValue,strSQL;
CComboBox* pCombox=(CComboBox*)GetDlgItem(IDC_OBJECT_COMBO);
pCombox->GetLBText(pCombox->GetCurSel(),strField);
pCombox=(CComboBox*)GetDlgItem(IDC_RELATION_COMBO);
pCombox->GetLBText(pCombox->GetCurSel(),strRelation);
//获取条件值
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_OBJECT_EDIT);
pEdit->GetWindowText(strWhereValue);
//必须填写查询条件值
if(!strWhereValue.Compare("''")||strWhereValue.IsEmpty())
{
AfxMessageBox("请检查条件字段值:)");
return;
}
//针对"="和"like"生成单个查询语句
if(pCombox->GetCurSel()==0)
{
//需要加上单引号
strSQL.Format("'%s'",strWhereValue);
strSQL=strField+strRelation+strSQL;
}
else
{
//设置百分号变量用于设置like查询(模糊查询)
char chTem='%';
strSQL.Format("'%c%s%c'",chTem,strWhereValue,chTem);
strSQL=strField+" "+strRelation+" "+strSQL;
}
CListBox* pListBox=(CListBox*)GetDlgItem(IDC_LIST_QL);
//将生成的单个查询语句添加到列表框
pListBox->AddString(strSQL);
}
void BOOKQUERY::OnDeleteSQL()
{
// TODO: Add your control notification handler code here
int iSel=-1;
CListBox* pListBox=(CListBox*)GetDlgItem(IDC_LIST_QL);
iSel=pListBox->GetCurSel();
if(iSel<0)
{
AfxMessageBox("请先选择SQL语句:)");
}
//删除所选查询语句
pListBox->DeleteString(iSel);
}
CString& BOOKQUERY::GetSQL()
{
CListBox* pListBox=(CListBox*)GetDlgItem(IDC_LIST_QL);
int iSql=0;
CString strText,strTem;
//获取总的条件数
iSql=pListBox->GetCount();
for(int i=0;i<iSql;i++)
{
if(i<iSql-1)
{
pListBox->GetText(i,strTem);
strText.Format("%s and ",strTem);
m_strSql=m_strSql+strText;
}
else
{
pListBox->GetText(i,strTem);
m_strSql=m_strSql+strTem;
}
}
return m_strSql;
}
BOOL BOOKQUERY::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;
}
void BOOKQUERY::OnOK()
{
// TODO: Add extra validation here
GetSQL();
CString strSQL;
strSQL.Format("select * from 图书信息情况 where %s",m_strSql);
CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CRightListView* pRightView;
CLeftTreeView* pLeftView;
pLeftView=(CLeftTreeView*)pMainFrm->m_wndSplitter.GetPane(0,0);
pRightView=(CRightListView*)pMainFrm->m_wndSplitter.GetPane(0,1);
//pRightView->m_strWhere=m_strSql;
//pRightView->SendMessage(LIST_EVERYONE,3,0);
pRightView->m_listCtrl.DeleteAllItems();
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;
pRightView->m_listCtrl.InsertItem (i,str);
varValue=m_pRecordset->GetFields()->GetItem("图书ID")->Value;
str=pLeftView->VariantToCString(varValue);
pRightView->m_listCtrl.SetItemText (i, 0, str);
varValue=m_pRecordset->GetFields()->GetItem("图书名称")->Value;
str=pLeftView->VariantToCString(varValue);
pRightView->m_listCtrl.SetItemText (i, 1, str);
varValue=m_pRecordset->GetFields()->GetItem("作者")->Value;
str=pLeftView->VariantToCString(varValue);
pRightView->m_listCtrl.SetItemText (i, 2, str);
varValue=m_pRecordset->GetFields()->GetItem("一级类型")->Value;
str=pLeftView->VariantToCString(varValue);
pRightView->m_listCtrl.SetItemText (i, 3, str);
varValue=m_pRecordset->GetFields()->GetItem("二级类型")->Value;
str=pLeftView->VariantToCString(varValue);
pRightView->m_listCtrl.SetItemText (i, 4, str);
i++;
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pRecordset=NULL;
CDialog::OnOK();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -