📄 datachoice.cpp
字号:
// DataChoice.cpp : implementation file
//
#include "stdafx.h"
#include "choice_data.h"
#include "DataChoice.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CString str[50];
extern CString table_name;
/////////////////////////////////////////////////////////////////////////////
// CDataChoice dialog
CDataChoice::CDataChoice(CWnd* pParent /*=NULL*/)
: CDialog(CDataChoice::IDD, pParent)
{
//{{AFX_DATA_INIT(CDataChoice)
m_edit_value = _T("");
//}}AFX_DATA_INIT
}
void CDataChoice::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDataChoice)
DDX_Control(pDX, IDC_LIST_data_choice_gear, m_list_gear_control);
DDX_Control(pDX, IDC_COMBO_rela, m_combo_rela);
DDX_Control(pDX, IDC_COMBO_attri, m_combo_attri);
DDX_Control(pDX, IDC_LIST_data_choice, m_list_control);
DDX_Text(pDX, IDC_EDIT_value, m_edit_value);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDataChoice, CDialog)
//{{AFX_MSG_MAP(CDataChoice)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_ask, OnBUTTONask)
ON_BN_CLICKED(ID_BTN_all, OnBTNall)
ON_NOTIFY(NM_CLICK, IDC_LIST_data_choice, OnClickLISTdatachoice)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDataChoice message handlers
BOOL CDataChoice::OnInitDialog()
{
CDialog::OnInitDialog();
this->SetWindowText("查询");
ClearList();
m_combo_attri.ResetContent();
// TODO: Add extra initialization here
try{
m_pdatabase=new CDatabase();
m_pset=new CRecordset();
m_pdatabase->Open(_T("gear_cutter_database"),FALSE,FALSE,_T("ODBC;DSN=gear_cutter_database"),FALSE);
m_pset->m_pDatabase=m_pdatabase;
}
catch(CDBException* e){
e->ReportError();
e->Delete();
delete m_pset;
delete m_pdatabase;
return TRUE;}
ComboData(table_name);
DataShow(table_name);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
bool CDataChoice::ShowInformation(CString strSQL)
{
if(!m_pdatabase->IsOpen()) return FALSE;
if(!m_pset) return FALSE;
try{
BeginWaitCursor();
if(m_pset->IsOpen()) m_pset->Close();
m_pset->Open(CRecordset::dynaset,strSQL);
if(!m_pset->IsEOF())
{m_pset->MoveLast();
m_pset->MoveFirst();}
int nFieldCount=m_pset->GetODBCFieldCount();
CODBCFieldInfo fieldinfo;
for(int n=0;n<nFieldCount;n++){
m_pset->GetODBCFieldInfo(n,fieldinfo);
int nWidth=m_list_control.GetStringWidth(fieldinfo.m_strName)+35;
m_list_control.InsertColumn(n,fieldinfo.m_strName,LVCFMT_RIGHT,nWidth);
}
CString strValue;
m_pset->MoveFirst();
int nCount=0;
while(!m_pset->IsEOF()){
m_list_control.InsertItem(nCount,strValue);
for(int j=0;j<nFieldCount;j++){
m_pset->GetFieldValue(j,strValue);
m_list_control.SetItemText(nCount,j,strValue);
}
m_pset->MoveNext();
nCount++;
count_cutter=nCount;
}
EndWaitCursor();
}
catch(CDBException *e){
e->Delete();
EndWaitCursor();
return FALSE;
}
return TRUE;
}
void CDataChoice::ClearList()
{
m_list_control.DeleteAllItems();
while(m_list_control.DeleteColumn(0));
UpdateWindow();
}
void CDataChoice::ClearListGear()
{
m_list_gear_control.DeleteAllItems();
while(m_list_gear_control.DeleteColumn(0));
UpdateWindow();
}
void CDataChoice::DataShow(CString table_name)
{
ClearList();
CString strSQL;
strSQL="select * from "+table_name;
ShowInformation(strSQL);
}
void CDataChoice::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
delete m_pset;
delete m_pdatabase;
}
void CDataChoice::OnBUTTONask()
{
// TODO: Add your control notification handler code here
ClearList();
ClearListGear();
CString strSQL;
CString attri,rela;
m_combo_attri.GetWindowText(attri);
m_combo_rela.GetWindowText(rela);
this->UpdateData(TRUE);
if(attri=="齿轮零件号"||attri=="刀具号")
strSQL="select * from "+table_name+" where "+attri+rela+"'"+m_edit_value+"'";
else
strSQL="select * from "+table_name+" where "+attri+rela+m_edit_value;
if(!ShowInformation(strSQL)) MessageBox("对不起,没有您所需要的数据!","提示",MB_ICONWARNING+MB_OK);
return;
}
bool CDataChoice::append_combo(CString strSQL)
{
try
{
if(m_pset->IsOpen()) m_pset->Close();
m_pset->Open(CRecordset::dynaset,strSQL);
if(!m_pset->IsEOF())
{m_pset->MoveLast();
m_pset->MoveFirst();}
int nFieldCount=m_pset->GetODBCFieldCount();
fields_count_cutter=nFieldCount;
CODBCFieldInfo fieldinfo;
for(int n=0;n<nFieldCount;n++){
m_pset->GetODBCFieldInfo(n,fieldinfo);
m_combo_attri.AddString(fieldinfo.m_strName);
}
}
catch(CDBException *e)
{
e->ReportError();
e->Delete();
EndWaitCursor();
return FALSE;
}
return TRUE;
}
void CDataChoice::ComboData(CString table_name)
{
CString strSQL;
m_combo_attri.ResetContent();
strSQL="select * from "+table_name;
if(!append_combo(strSQL)) MessageBox("数据获取失败!","提示",MB_ICONERROR+MB_OK);
}
void CDataChoice::OnOK()
{
// TODO: Add extra validation here
UINT uSelectedCount=m_list_gear_control.GetSelectedCount();
if(uSelectedCount>1||uSelectedCount==0)
{
if(uSelectedCount==0)
{
MessageBox("要选择一条齿轮参数!","提示",MB_ICONWARNING+MB_OK);
return;
}
if(uSelectedCount>1)
{
MessageBox("只能选择一条齿轮参数!","提示",MB_ICONWARNING+MB_OK);
return;
}
}
else
{
int nItemSel=0;
nItemSel=m_list_gear_control.GetNextItem(nItemSel-1,LVNI_SELECTED);
for(int j=0;j<fields_count_gear;j++)
{
str[j]=m_list_gear_control.GetItemText(nItemSel,j);
}
for(int i=0;i<count_cutter;i++)
{
CString gear_num=m_list_control.GetItemText(i,0);
if(gear_num==str[0])
for(int m=1;m<fields_count_cutter;m++)
{
str[m+10]=m_list_control.GetItemText(i,m);
}
}
}
CDialog::OnOK();
}
void CDataChoice::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::EndDialog(0);
}
void CDataChoice::OnBTNall()
{
// TODO: Add your control notification handler code here
ClearList();
ClearListGear();
CString strSQL;
strSQL="select * from "+table_name;
ShowInformation(strSQL);
return;
}
void CDataChoice::OnClickLISTdatachoice(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
Gear_Data_List("gear_table","齿轮零件号");
*pResult = 0;
}
bool CDataChoice::Gear_Data_List(CString table_name,CString attri)
{
if(!m_pdatabase->IsOpen()) {MessageBox("数据获取失败!","提示",MB_ICONERROR+MB_OK);return FALSE;}
UINT uSelectedCount=m_list_control.GetSelectedCount();
if(uSelectedCount>1||uSelectedCount==0)
{
if(uSelectedCount==0)
{
MessageBox("要选择一条齿轮参数!","提示",MB_ICONWARNING+MB_OK);
ClearListGear();
return FALSE;
}
if(uSelectedCount>1)
{
MessageBox("只能选择一条齿轮参数!","提示",MB_ICONWARNING+MB_OK);
ClearListGear();
return FALSE;
}
}
else
{
int nItemSel=0;
nItemSel=m_list_control.GetNextItem(nItemSel-1,LVNI_SELECTED);
CString strID,strSQL;
strID=m_list_control.GetItemText(nItemSel,0);
strSQL="select * from "+table_name+" where "+attri+"='"+strID+"'";
try
{
m_pset_gear.m_pDatabase=m_pdatabase;
if(m_pset_gear.IsOpen()) m_pset_gear.Close();
m_pset_gear.Open(CRecordset::dynaset,strSQL);
BeginWaitCursor();
if(!m_pset_gear.IsEOF())
{m_pset_gear.MoveLast();
m_pset_gear.MoveFirst();}
int nFieldCount=m_pset_gear.GetODBCFieldCount();
fields_count_gear=nFieldCount;
CODBCFieldInfo fieldinfo;
ClearListGear();
for(int n=0;n<nFieldCount;n++){
m_pset_gear.GetODBCFieldInfo(n,fieldinfo);
int nWidth=m_list_gear_control.GetStringWidth(fieldinfo.m_strName)+35;
m_list_gear_control.InsertColumn(n,fieldinfo.m_strName,LVCFMT_RIGHT,nWidth);
}
CString strValue;
m_pset_gear.MoveFirst();
int nCount=0;
while(!m_pset_gear.IsEOF())
{
m_list_gear_control.InsertItem(nCount,strValue);
for(int j=0;j<nFieldCount;j++)
{
m_pset_gear.GetFieldValue(j,strValue);
m_list_gear_control.SetItemText(nCount,j,strValue);
}
m_pset_gear.MoveNext();
nCount++;
}
EndWaitCursor();
}
catch(CDBException* e)
{
e->Delete();
return FALSE;
}
return TRUE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -