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

复杂条件查询
比方说我有8种条件 用户可以输入其中任意几个,这样子我判断会有28种 给我一种思路 更灵活的

------解决方案--------------------
一个方法是使用StringBuider,但最后要转换为string类型,另一种直接使用字符串相加


C# code

string strsql = "select * from userinfo where 1=1";
if(!string.IsNullOrEmpty(txtUserName.Text.Trim()))
 strsql+= " and username='"+txtUserName.Text.Trim()+"'";
if(!string.IsNullOrEmpty(cmbAge.Text.Trim()))
 strsql+= " and age='"+cmbAge.Text.Trim()+"'";
if(!string.IsNullOrEmpty(datePickerBirthday.Text))
 strsql+=" and Birthday ='"+datePickerBirthday.Text+"'";