?? 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 + -