heyou
路人甲
路人甲
  • 注册日期2004-04-07
  • 发帖数59
  • QQ
  • 铜币324枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:1394回复:0

VC编程环境下的SQL查询功能代码.如下:

楼主#
更多 发布于:2005-05-16 19:07
<P>我将代码全都贴上了:</P>
<P>// SQLSearchDlg.cpp : 实现文件
//</P>
<P>#include "stdafx.h"
#include "MpGIS.h"
#include "SQLSearchDlg.h"</P>
<P>
// CSQLSearchDlg 对话框</P>
<P>IMPLEMENT_DYNAMIC(CSQLSearchDlg, CDialog)
CSQLSearchDlg::CSQLSearchDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CSQLSearchDlg::IDD, pParent)
 , m_strSearchValue(_T(""))
{
 m_strExpression="";
}</P>
<P>CSQLSearchDlg::~CSQLSearchDlg()
{
}</P>
<P>void CSQLSearchDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_Control(pDX, IDC_EDIT_DISPLAY, m_edtSQL);
 DDX_Text(pDX, IDC_EDIT_VALUE, m_strSearchValue);
}</P>
<P>
BEGIN_MESSAGE_MAP(CSQLSearchDlg, CDialog)
 ON_CBN_SELENDOK(IDC_COMBO_TABLE, OnCbnSelendokComboTable)
 ON_CBN_SELENDOK(IDC_COMBO_FIELDS, OnCbnSelendokComboFields)
 ON_CBN_SELENDOK(IDC_COMBO_OPERATOR, OnCbnSelendokComboOperator)
 ON_CBN_SELENDOK(IDC_COMBO_AGGREGATION, OnCbnSelendokComboAggregation)
 ON_CBN_SELENDOK(IDC_COMBO_FUNTION, OnCbnSelendokComboFuntion)
 //ON_CBN_SELENDOK(IDC_COMBO_VALUE, OnCbnSelendokComboValue)
 ON_BN_CLICKED(IDOK, OnBnClickedOk)
 ON_EN_CHANGE(IDC_EDIT_VALUE, OnEnChangeEditValue)
END_MESSAGE_MAP()</P>

<P>// CSQLSearchDlg 消息处理程序</P>
<P>BOOL CSQLSearchDlg::SetSQL(CString strLayerName,CString strFieldName,CString strCondition/*条件*/)
{
 //设计表达式并显示
 //Select * from LayerName WHERE ID LIKE “%北京%” </P>
<P> CString str;
 str="select "+strFieldName+" from "+strLayerName+" where "+strFieldName+strCondition;
 
 this->m_edtSQL .SetWindowText (str);</P>
<P> return TRUE;
}
BOOL CSQLSearchDlg::OnInitDialog()
{
 CDialog::OnInitDialog();</P>
<P> CComboBox * pLyrCombo=(CComboBox *)GetDlgItem(IDC_COMBO_TABLE);
 ASSERT(pLyrCombo);
 for(int i=0;i<m_pMapX->GetLayers ().GetCount ();i++)
 {
  CMapXLayer lyr;
  lyr=m_pMapX->GetLayers ().Item (i+1);
  pLyrCombo->AddString (lyr.GetName ());
 }
 pLyrCombo->SetWindowText ("请选择查询图层表!");
 
 return TRUE;  // return TRUE unless you set the focus to a control
 // 异常:OCX 属性页应返回 FALSE
}</P>
<P>void CSQLSearchDlg::OnCbnSelendokComboTable()
{
 CComboBox * pLyrCombo=(CComboBox *)GetDlgItem(IDC_COMBO_TABLE);
 int nCurSel=pLyrCombo->GetCurSel ();
 int len=pLyrCombo->GetLBTextLen (nCurSel);
 pLyrCombo->GetLBText (nCurSel,m_strLayerName.GetBuffer (len));
 m_strLayerName.ReleaseBuffer ();</P>
<P> //填充对应图层的字段
 CComboBox * pFieldCombo=(CComboBox *)GetDlgItem(IDC_COMBO_FIELDS);
 ASSERT(pFieldCombo);
 pFieldCombo->ResetContent ();</P>
<P> CMapXLayer lyr;
 lyr=m_pMapX->GetLayers ().Item (m_strLayerName);
 CMapXDataset ds;
 ds=lyr.GetDatasets ().Item (1);
 for(int i=0;i<ds.GetFields ().GetCount ();i++)
 {
  CMapXField fld;
  fld=ds.GetFields ().Item (i+1);
  pFieldCombo->AddString (fld.GetName ());
 }</P>
<P> SetSQL(m_strLayerName,m_strFieldName,m_strExpression);</P>
<P>
}
void CSQLSearchDlg::OnCbnSelendokComboFields()
{
 CString buffer;</P>
<P> CComboBox * pFldCombo=(CComboBox *)GetDlgItem(IDC_COMBO_FIELDS);
 int nCurSel=pFldCombo->GetCurSel ();
 int len=pFldCombo->GetLBTextLen (nCurSel);
 pFldCombo->GetLBText (nCurSel,m_strFieldName.GetBuffer (len));
 m_strFieldName.ReleaseBuffer ();</P>
<P> SetSQL(m_strLayerName,m_strFieldName,m_strExpression);
}
void CSQLSearchDlg::OnCbnSelendokComboOperator()
{
 CComboBox * pOperatorCombo=(CComboBox *)GetDlgItem(IDC_COMBO_OPERATOR);
 int nCurSel=pOperatorCombo->GetCurSel ();
 int len=pOperatorCombo->GetLBTextLen (nCurSel);
 pOperatorCombo->GetLBText (nCurSel,m_strOperator.GetBuffer (len));
 m_strOperator.ReleaseBuffer ();</P>
<P> if(m_strOperator=="Like")
  m_strExpression=m_strExpression+" "+m_strOperator+"%";
 else
  m_strExpression=m_strExpression+" "+m_strOperator;</P>
<P> SetSQL(m_strLayerName,m_strFieldName,m_strExpression);
}</P>
<P>void CSQLSearchDlg::OnCbnSelendokComboAggregation()
{
 CComboBox * pAggregationCombo=(CComboBox *)GetDlgItem(IDC_COMBO_AGGREGATION);
 int nCurSel=pAggregationCombo->GetCurSel ();
 int len=pAggregationCombo->GetLBTextLen (nCurSel);
 pAggregationCombo->GetLBText (nCurSel,m_strAggregation.GetBuffer (len));
 m_strAggregation.ReleaseBuffer ();</P>
<P> m_strExpression=m_strExpression+","+m_strAggregation.Trim ()+"()";
 
 SetSQL(m_strLayerName,m_strFieldName,m_strExpression);
}</P>
<P>void CSQLSearchDlg::OnCbnSelendokComboFuntion()
{
 CComboBox * pFunctionCombo=(CComboBox *)GetDlgItem(IDC_COMBO_FUNTION);
 int nCurSel=pFunctionCombo->GetCurSel ();
 int len=pFunctionCombo->GetLBTextLen (nCurSel);
 pFunctionCombo->GetLBText (nCurSel,m_strFunction.GetBuffer (len));
 m_strFunction.ReleaseBuffer ();</P>
<P> m_strExpression=m_strExpression+" "+m_strFunction.Trim ()+"()";
 
 SetSQL(m_strLayerName,m_strFieldName,m_strExpression);
}</P>

<P>void CSQLSearchDlg::OnBnClickedOk()
{
 UpdateData(TRUE);</P>
<P>   //获取条件表达式
  CString strExpress,strSql;
  m_edtSQL .GetWindowText (strExpress);
  //</P>
<P>  int whereSt=strExpress.Find ("where");
  strSql=strExpress.Mid (whereSt+5);
  strSql=strSql.Trim ();</P>
<P> // MessageBox(strSql);</P>

<P>  CMapXDataset ds;
          
  CMapXLayer  lyr;
  CMapXFeatures ftrs;
  CMapXFeature ftr;
  //CMapXVariables Vs;</P>
<P>  ftr.CreateDispatch (ftr.GetClsid ());
  //Vs.CreateDispatch (Vs.GetClsid ());
  try
  {
   //数据未有绑定/////////////////////////////////</P>
<P>   MessageBox(m_strLayerName);</P>
<P>   lyr=m_pMapX->GetLayers ().Item (m_strLayerName);</P>
<P>   COleVariant layerVt;
   layerVt.vt = VT_DISPATCH;
   layerVt.pdispVal = lyr.m_lpDispatch;
   layerVt.pdispVal->AddRef();
  
   ds = m_pMapX->GetDatasets().Add(miDataSetLayer, layerVt);
   </P>
<P>   VARIANT vFtr;
   vFtr.vt =VT_DISPATCH;
   vFtr.pdispVal =ftr.m_lpDispatch ;</P>
<P>
   ftr=lyr.AllFeatures ().Item (1);
   //Vs.Add ("f",vFtr);</P>
<P>   ftrs=lyr.Search (strSql);//,Vs);</P>
<P>   lyr.GetSelection ().ClearSelection ();
   lyr.GetSelection ().Replace (ftrs);</P>
<P>  //获取的特征在ftr中和该层的选择集中
  //以及在这里进行的确查询 和查询结果在案浏缆窗口中的显示!!!!
  }
  catch (COleDispatchException *e)
  {
   e->ReportError();
   e->Delete();
  }
  catch (COleException *e)
  {
   e->ReportError();
   e->Delete();
  }
  ds=NULL;
  lyr=NULL;
  ftrs=NULL;
  ftr=NULL;</P>
<P> OnOK();
}</P>
<P>void CSQLSearchDlg::OnEnChangeEditValue()
{
 // TOD  如果该控件是 RICHEDIT 控件,则它将不会
 // 发送该通知,除非重写 CDialog::OnInitDialog()
 // 函数并调用 CRichEditCtrl().SetEventMask(),
 // 同时将 ENM_CHANGE 标志“或”运算到掩码中。</P>
<P> UpdateData(TRUE);</P>
<P> CString str;
 str=CString ("='"+m_strSearchValue+"'");</P>
<P> SetSQL(m_strLayerName,m_strFieldName,m_strExpression+str/*m_strExpression*/ );</P>
<P>}</P>
<P>求教斑竹,怎么出现这个错误呀@
   The string resource (6) could not be accessed!
这怎么解决啊!请斑竹帮忙调试检查呀!!!
  
</P>
喜欢0 评分0
游客

返回顶部