日期:2014-05-18  浏览次数:20854 次

一个页面多种可能查询
一个页面多种可能查询,怎么实现呢?只有一个按钮,可是查询条件组合可能有多个,例如按@code和@customer,或@email和@type,组合条件很多,这样情况下该怎么判断?
  string @code = coupon_code.Text.Trim();
  string @customer = customers_name.Text.Trim();
  string @email = customers_email.Text.Trim();
  string @sdate = create_date.Text.Trim();
  string @edate = credate.Text.Trim();
  string @type = ctype.Text.Trim();
  string @status = cstatus.Text.Trim();

------解决方案--------------------
条件从何而来,根据条件组合不同的sql就是了,到底用那几个组合,这是你定的
------解决方案--------------------
select * from table1 where (code=@code and customer=@customer) or (email=@email and type=@type)

这样
------解决方案--------------------
select * from table1 where (code=@code or @code is null) and (email=@email or @email is null)

默认为空就行,有值的话就并列判断;无值的话始终为真,并判断其他的条件是否满足
------解决方案--------------------
请Google关键字:Specification

利用规约实现复杂查询条件,表达会相对简洁许多。
------解决方案--------------------
C# code

string strSQL = "SELECT * FROM TABLE ";
strSQL = "WHERE 1 = 1 ";

if (coupon_code.Text.Trim() != "")
{
    strSQL += "AND _code LIKE '%"+coupon_code.Text.Trim()+"%' ";
}
if (customers_name.Text.Trim() != "")
{
    strSQL += "AND _custorers LIKE '%"+customers_name.Text.Trim()+"%' ";
}
......
......
......

------解决方案--------------------
like %...% 的写法尽量避免,性能较差,不能利用索引
------解决方案--------------------
探讨
C# code


string strSQL = "SELECT * FROM TABLE ";
strSQL = "WHERE 1 = 1 ";

if (coupon_code.Text.Trim() != "")
{
strSQL += "AND _code LIKE '%"+coupon_code.Text.Trim()+"%' ";
}
if (custom……