ASP.NET如何实现多条件的查询语句
如何实现多条件的查询语句
string str = "select * from pub ";
if(text1.Text != " ")
str += " where id= ' " + this.text1.Text + " ' ";
if(text2.Text != " ")
str += " ' and name= ' " + this.text2.Text + " ' ";
if(text3.Text != " ")
str += " ' and condition = ' " + this.text3.Text + " ' ";
.
.
.
如何实现呢?
------解决方案--------------------string str = "select * from pub "; 改为:string str = "select * from pub where 1=1 ";然后下面全部用and conditon就可以了
------解决方案--------------------1)string str = "select * from pub "; 改为:string str = "select * from pub where 1=1 ";然后下面全部用and conditon就可以了
2)
如何实现多条件的查询语句
str = " ";
if(text1.Text != " ")
str += " where id= ' " + this.text1.Text + " ' ";
if(text2.Text != " ")
str += " ' and name= ' " + this.text2.Text + " ' ";
if(text3.Text != " ")
str += " ' and condition = ' " + this.text3.Text + " ' ";
------解决方案--------------------str = " ";
if(text1.Text != " ")
{
if (str!= " ") str+= " and "
str += " id= ' " + this.text1.Text + " ' ";
}
if(text2.Text != " ")
{
if (str!= " ") str+= " and "
str += " ' and name= ' " + this.text2.Text + " ' ";
if(text3.Text != " ")
str += " ' and condition = ' " + this.text3.Text + " ' ";