在一个对话框上添加 4个编辑框用来输入查询条件,一个查询按钮,一个ListCtrl用来显示结果。
编辑框 关联的变量分别为:
IDC_EDIT1  m_hphm
IDC_EDIT2  m_hpzl
IDC_EDIT3  m_wzsj1
IDC_EDIT4  m_wzsj2
单个条件查询我能够实现:
如: CString temp="%";
   CString str="SELECT * FROM  RECORD ";
//模糊查询
if (!m_hphm.IsEmpty())
{
str.Format(str+"WHERE HPHM like '%s%s%S'",temp,m_hphm,temp);
}
//时间段查询
  if((!m_wzsj1.IsEmpty()) & (!m_wzsj2.IsEmpty()))
{
str.Format(str+"WHERE WZSJ BETWEEN '%s' AND '%s'",m_wzsj1,m_wzsj2);
}
现在的问题是,如何实现多条件组合查询(如果某个编辑框为空就不去考虑其条件,不为空才作为查询条件,需判断)。
两天了还没搞定,哪位大侠能够出手相助,不胜感激(急用!!!)

解决方案 »

  1.   


    void CFindDlg::OnBnClickedBtnFindQuery()
    {
    UpdateData(TRUE); CString strCondition;
    if (m_strSN.GetLength() != 0)
    {
    strCondition += " uid like '%";
    strCondition += m_strSN;
    strCondition += "%'";
    }
    if (m_strManufacturer.GetLength() != 0)
    {
    strCondition += strCondition.GetLength()==0 ? "" : " and ";
    strCondition += "manufacturer like '%";
    strCondition += m_strManufacturer;
    strCondition += "%'";
    }
    if (m_strUnit.GetLength() != 0)
    {
    strCondition += strCondition.GetLength()==0 ? "" : " and ";
    strCondition += "unit like '%";
    strCondition += m_strUnit;
    strCondition += "%'";
    }
    if (m_strDept.GetLength() != 0)
    {
    strCondition += strCondition.GetLength()==0 ? "" : " and ";
    strCondition += "dept like '%";
    strCondition += m_strDept;
    strCondition += "%'";
    }
    if (m_strUser.GetLength() != 0)
    {
    strCondition += strCondition.GetLength()==0 ? "" : " and ";
    strCondition += "user like '%";
    strCondition += m_strUser;
    strCondition += "%'";
    } m_strCondition = strCondition; CDialogEx::OnOK();
    }
      

  2.   

    这里面处理的是where语句,查询时添加到select后面就行了
      

  3.   

    自己已经解决了,谢谢各位
    ZZ_LGY 方法也不错就是有代码有点太长