日期:2014-05-19  浏览次数:20431 次

搜索条件?写SQL语句
做个搜索器,有几个条件供用户选择,但用户也有可能不是所有的选项都选,可能有些条件是空的,这样的SQL语句该怎么写啊?

------解决方案--------------------
string sql= "select * from tb where 1=1 ";
if(txt1.Text.Trim().lenth> 0)
{
sql+= " and title like '%+txt1.Text.Trim()+% ' ";
}
if(txt2.Text.Trim().lenth> 0)
{
sql+= " and content like '%+txt2.Text.Trim()+% ' ";
}

------解决方案--------------------
顶,就是上面说的那样写
------解决方案--------------------
string sql= "select * from tb where 1=1 ";
if(txt1.Text.Trim().lenth> 0)
{
sql+= " and title like '%+txt1.Text.Trim()+% ' ";
}
if(txt2.Text.Trim().lenth> 0)
{
sql+= " and content like '%+txt2.Text.Trim()+% ' ";

}

就魔力阳光这样写。