sql语句多条件查询?

时间:2021-10-08 05:39:58
sql语句多条件查询?,如图所示,不知道图片能否传上去,我想实现的功能是,当文本框为空时,查询所有满足是否在线和是否允许发言的用户,当输输入用户名后按这个三个条件查询,用户名、是否在线、是否允许发言,怎样写sql语句啊,我这是一部分,有错误,
 string txtUserName = this.TextBox1.Text.ToString().Trim();
        string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1";
        
      
        if (txtUserName == "")
        {
            sqlText += "";
        }
        else if (this.DropDownList1.SelectedValue == "在线")
        {
            sqlText += "and UserName=@userName and IsOnline=true";
        }
        else if(this.DropDownList2.SelectedValue=="可以"){
            sqlText += "and IsAllowMessage=true";
        }
        

        OleDbConnection con = DB.createCon();
        con.Open();

        DataTable table = new DataTable();
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlText,con );

        OleDbParameter para1 = new OleDbParameter("@userName", OleDbType.WChar, 50);
        para1.Value = txtUserName;
        dataAdapter.SelectCommand.Parameters.Add(para1);
        dataAdapter.Fill(table);

        this.GridView1.DataSource = table.DefaultView;
        this.GridView1.DataBind();
        

24 个解决方案

#1


sql语句多条件查询?,图片怎么传不上去啊,谁帮我一下?

#2


string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1 "; 这句首先1=1后加上一个空格,再说

#3


if (txtUserName != "") 
        { 
            sqlText += " and UserName=@userName "; 
        } 
        if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += " and IsOnline=true "; 
        } 
        if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += " and IsAllowMessage=true"; 
        } 

#4


我的sql语句没问题,问题是我不知道这个逻辑怎么写,三个条件,不会得写6个判断语句吧,要是再多个条件那就写不完了!

#5


引用 4 楼 benpao412 的回复:
我的sql语句没问题,问题是我不知道这个逻辑怎么写,三个条件,不会得写6个判断语句吧,要是再多个条件那就写不完了!

怎么就没有问题??这里明显少空格!
就是这么写的!!!拼接sql

#6


我刚才又运行了一下,能查询,我想知道多个条件查询改怎么写,if语句怎么嵌套

#7


你就不该这样写:
this.DropDownList1.SelectedValue 的值这样的东西,在前台组织好以后,
全部传给BLL层就好,而BLL传DAL再调存储过程。

把查询语句放在表示层,当然会造成过度的依赖~

建议楼主找个三层结构的文章去看看~

#8


没有上传图片,只能根据你现在的Sql进行修改
1、下面这句最后留一个空格出来。  
string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1"; 
2、if (txtUserName == "") 修改为 if(this.TextBox1.Text.Trim().Length<1) 
      
        if(this.TextBox1.Text.Trim().Length<1)
        { 
            sqlText += ""; 
        } 
        else if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += "and UserName=@userName and IsOnline=true"; 
        } 
        else if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += "and IsAllowMessage=true"; 
        } 
        
这样来看看。

#9


图片是什么啊
把and换成or试试

#10


我还是那句话,应该空格啊!
这里是并列的拼接sql,不是嵌套吧?
        if (txtUserName == "") 
        { 
            sqlText += " and UserName=@userName "; 
        } 
        if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += " and IsOnline=true"; 
        } 
        if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += " and IsAllowMessage=true"; 
        } 

#11


sql 有true 和 false吗
应该用0或1吧
      if (txtUserName == "")
        {
            sqlText += "";
        }
        else if (this.DropDownList1.SelectedValue == "在线")
        {
            sqlText += "and UserName=@userName and IsOnline=1";
        }
        else if(this.DropDownList2.SelectedValue=="可以"){
            sqlText += "and IsAllowMessage=1";
        } 

#12


string txtUserName = this.TextBox1.Text.ToString().Trim();
        string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1 "; //空格
       
     
        if (txtUserName == "")
        {
            sqlText += "";
        }
        else if (this.DropDownList1.SelectedValue == "在线")
        {
            sqlText += "and UserName=@userName and IsOnline=1";
        }
        else if(this.DropDownList2.SelectedValue=="可以"){
            sqlText += "and IsAllowMessage=1";
        }
       

        OleDbConnection con = DB.createCon();
        con.Open();

        DataTable table = new DataTable();
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlText,con );

        OleDbParameter para1 = new OleDbParameter("@userName", OleDbType.WChar, 50);
        para1.Value = txtUserName;
        dataAdapter.SelectCommand.Parameters.Add(para1);
        dataAdapter.Fill(table);

        this.GridView1.DataSource = table.DefaultView;
        this.GridView1.DataBind(); 

#13


 

太感谢大家了,我是Access数据库,有三个条件,结果有六种情况,我不知道怎么列出这六种情况,我下面这样写肯定不对,我不知道怎样把所有情况都列出来。
 if (txtUserName == "") 
        { 
            sqlText += ""; 
        } 
        else if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += "and UserName=@userName and IsOnline=true"; 
        } 
         else if (this.DropDownList1.SelectedValue == "不在线") 
        { 
            sqlText += "and UserName=@userName and IsOnline=false;
        }
        else if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += "and IsAllowMessage=true"; 
        } 
        else if(this.DropDownList2.SelectedValue=="不可以"){ 
            sqlText += "and IsAllowMessage=false;
        } 

#14


select * from my_table
where (f1 = @p1 or @p1 is null)
and (f2 = @p2 or @p2 is null)

如果要忽略 f1 列,就传 null 给 @p1

#15


引用 1 楼 benpao412 的回复:
,图片怎么传不上去啊,谁帮我一下?

截图就是这个,怎么写可以实现所有的条件都能查询啊,当用户名为空时可以查询在线允许发言、在线不允许发言、不在线允许发言、不在线不允许发言,当用户名不为空时也能查询所有条件,这几中情况我用语句写不出来,麻烦各位了,帮帮我!

#16


string sql = "select * from userinformation where UserName like '%{0}%' and IsOnline={1} and IsAllowMessage={2}";
sql = String.Format(sql, TextBox1.Text.Replace("'","''").Trim(), this.DropDownList1.SelectedValue == "在线", this.DropDownList2.SelectedValue=="可以");

#17


引用 16 楼 sq_zhuyi 的回复:
string sql = "select * from userinformation where UserName like '%{0}%' and IsOnline={1} and IsAllowMessage={2}";
sql = String.Format(sql, TextBox1.Text.Replace("'","''").Trim(), this.DropDownList1.SelectedValue == "在线", this.DropDownList2.SelectedValue=="可以");

我学的时间不长呢,看不懂啊,能不能解释一下?

#18


string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1"; 
    if (txtUserName == "") 
        { 
            sqlText += ""; 
        } 
     sqlText += this.DropDownList1.SelectedValue.Equals("在线")?"and UserName=@userName and IsOnline=true":"and UserName=@userName and IsOnline=false"; 
sqlText += "and UserName=@userName and IsOnline=false"; 
       sqlText += this.DropDownList2.SelectedValue.Equals("可以")?"IsAllowMessage=true":"and IsAllowMessage=false"; 

#19


呵呵,太漂亮了,谢谢,马上结贴!

#20


不对啊,有点错,如果用户名为空的话,sqlText += this.DropDownList1.SelectedValue.Equals("在线")?"and UserName=@userName and IsOnline=true":"and UserName=@userName and IsOnline=false"; 这条语句中UserName=""那就出错了吧

#21


嚯嚯。。。。

#22


在sql语句查询时,如果遇到多个查询条件,怎么进行判断呢?

#23


多条件查询,怎么不支持那,纠结
select * from UserInfo where (Name like '%大%' or Lname like '%大%' or Tel like '%大%' or Address like '%大%' or ZhuYiingYeWu like '%大%' or TeSeFuWu like 

'%大%' or DaiLiPinPai like '%大%' or JingYingFanWei like '%大%' or body like '%大%' or DianJiCiShu like '%大%' or HuiYuan like '%大%' or area like '%大%') 

and (Name like '%又红又专%' or Lname like '%又红又专%' or Tel like '%又红又专%' or Address like '%又红又专%' or ZhuYiingYeWu like '%又红又专%' or TeSeFuWu 

like '%又红又专%' or DaiLiPinPai like '%又红又专%' or JingYingFanWei like '%又红又专%' or body like '%又红又专%' or DianJiCiShu like '%又红又专%' or HuiYuan 

like '%又红又专%' or area like '%又红又专%') 
只要查不到值,就报错

#24


貌似很火啊,坐等结贴

#1


sql语句多条件查询?,图片怎么传不上去啊,谁帮我一下?

#2


string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1 "; 这句首先1=1后加上一个空格,再说

#3


if (txtUserName != "") 
        { 
            sqlText += " and UserName=@userName "; 
        } 
        if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += " and IsOnline=true "; 
        } 
        if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += " and IsAllowMessage=true"; 
        } 

#4


我的sql语句没问题,问题是我不知道这个逻辑怎么写,三个条件,不会得写6个判断语句吧,要是再多个条件那就写不完了!

#5


引用 4 楼 benpao412 的回复:
我的sql语句没问题,问题是我不知道这个逻辑怎么写,三个条件,不会得写6个判断语句吧,要是再多个条件那就写不完了!

怎么就没有问题??这里明显少空格!
就是这么写的!!!拼接sql

#6


我刚才又运行了一下,能查询,我想知道多个条件查询改怎么写,if语句怎么嵌套

#7


你就不该这样写:
this.DropDownList1.SelectedValue 的值这样的东西,在前台组织好以后,
全部传给BLL层就好,而BLL传DAL再调存储过程。

把查询语句放在表示层,当然会造成过度的依赖~

建议楼主找个三层结构的文章去看看~

#8


没有上传图片,只能根据你现在的Sql进行修改
1、下面这句最后留一个空格出来。  
string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1"; 
2、if (txtUserName == "") 修改为 if(this.TextBox1.Text.Trim().Length<1) 
      
        if(this.TextBox1.Text.Trim().Length<1)
        { 
            sqlText += ""; 
        } 
        else if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += "and UserName=@userName and IsOnline=true"; 
        } 
        else if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += "and IsAllowMessage=true"; 
        } 
        
这样来看看。

#9


图片是什么啊
把and换成or试试

#10


我还是那句话,应该空格啊!
这里是并列的拼接sql,不是嵌套吧?
        if (txtUserName == "") 
        { 
            sqlText += " and UserName=@userName "; 
        } 
        if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += " and IsOnline=true"; 
        } 
        if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += " and IsAllowMessage=true"; 
        } 

#11


sql 有true 和 false吗
应该用0或1吧
      if (txtUserName == "")
        {
            sqlText += "";
        }
        else if (this.DropDownList1.SelectedValue == "在线")
        {
            sqlText += "and UserName=@userName and IsOnline=1";
        }
        else if(this.DropDownList2.SelectedValue=="可以"){
            sqlText += "and IsAllowMessage=1";
        } 

#12


string txtUserName = this.TextBox1.Text.ToString().Trim();
        string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1 "; //空格
       
     
        if (txtUserName == "")
        {
            sqlText += "";
        }
        else if (this.DropDownList1.SelectedValue == "在线")
        {
            sqlText += "and UserName=@userName and IsOnline=1";
        }
        else if(this.DropDownList2.SelectedValue=="可以"){
            sqlText += "and IsAllowMessage=1";
        }
       

        OleDbConnection con = DB.createCon();
        con.Open();

        DataTable table = new DataTable();
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlText,con );

        OleDbParameter para1 = new OleDbParameter("@userName", OleDbType.WChar, 50);
        para1.Value = txtUserName;
        dataAdapter.SelectCommand.Parameters.Add(para1);
        dataAdapter.Fill(table);

        this.GridView1.DataSource = table.DefaultView;
        this.GridView1.DataBind(); 

#13


 

太感谢大家了,我是Access数据库,有三个条件,结果有六种情况,我不知道怎么列出这六种情况,我下面这样写肯定不对,我不知道怎样把所有情况都列出来。
 if (txtUserName == "") 
        { 
            sqlText += ""; 
        } 
        else if (this.DropDownList1.SelectedValue == "在线") 
        { 
            sqlText += "and UserName=@userName and IsOnline=true"; 
        } 
         else if (this.DropDownList1.SelectedValue == "不在线") 
        { 
            sqlText += "and UserName=@userName and IsOnline=false;
        }
        else if(this.DropDownList2.SelectedValue=="可以"){ 
            sqlText += "and IsAllowMessage=true"; 
        } 
        else if(this.DropDownList2.SelectedValue=="不可以"){ 
            sqlText += "and IsAllowMessage=false;
        } 

#14


select * from my_table
where (f1 = @p1 or @p1 is null)
and (f2 = @p2 or @p2 is null)

如果要忽略 f1 列,就传 null 给 @p1

#15


引用 1 楼 benpao412 的回复:
,图片怎么传不上去啊,谁帮我一下?

截图就是这个,怎么写可以实现所有的条件都能查询啊,当用户名为空时可以查询在线允许发言、在线不允许发言、不在线允许发言、不在线不允许发言,当用户名不为空时也能查询所有条件,这几中情况我用语句写不出来,麻烦各位了,帮帮我!

#16


string sql = "select * from userinformation where UserName like '%{0}%' and IsOnline={1} and IsAllowMessage={2}";
sql = String.Format(sql, TextBox1.Text.Replace("'","''").Trim(), this.DropDownList1.SelectedValue == "在线", this.DropDownList2.SelectedValue=="可以");

#17


引用 16 楼 sq_zhuyi 的回复:
string sql = "select * from userinformation where UserName like '%{0}%' and IsOnline={1} and IsAllowMessage={2}";
sql = String.Format(sql, TextBox1.Text.Replace("'","''").Trim(), this.DropDownList1.SelectedValue == "在线", this.DropDownList2.SelectedValue=="可以");

我学的时间不长呢,看不懂啊,能不能解释一下?

#18


string sqlText = "select name,isonline,isallowmessage from userinformation where 1=1"; 
    if (txtUserName == "") 
        { 
            sqlText += ""; 
        } 
     sqlText += this.DropDownList1.SelectedValue.Equals("在线")?"and UserName=@userName and IsOnline=true":"and UserName=@userName and IsOnline=false"; 
sqlText += "and UserName=@userName and IsOnline=false"; 
       sqlText += this.DropDownList2.SelectedValue.Equals("可以")?"IsAllowMessage=true":"and IsAllowMessage=false"; 

#19


呵呵,太漂亮了,谢谢,马上结贴!

#20


不对啊,有点错,如果用户名为空的话,sqlText += this.DropDownList1.SelectedValue.Equals("在线")?"and UserName=@userName and IsOnline=true":"and UserName=@userName and IsOnline=false"; 这条语句中UserName=""那就出错了吧

#21


嚯嚯。。。。

#22


在sql语句查询时,如果遇到多个查询条件,怎么进行判断呢?

#23


多条件查询,怎么不支持那,纠结
select * from UserInfo where (Name like '%大%' or Lname like '%大%' or Tel like '%大%' or Address like '%大%' or ZhuYiingYeWu like '%大%' or TeSeFuWu like 

'%大%' or DaiLiPinPai like '%大%' or JingYingFanWei like '%大%' or body like '%大%' or DianJiCiShu like '%大%' or HuiYuan like '%大%' or area like '%大%') 

and (Name like '%又红又专%' or Lname like '%又红又专%' or Tel like '%又红又专%' or Address like '%又红又专%' or ZhuYiingYeWu like '%又红又专%' or TeSeFuWu 

like '%又红又专%' or DaiLiPinPai like '%又红又专%' or JingYingFanWei like '%又红又专%' or body like '%又红又专%' or DianJiCiShu like '%又红又专%' or HuiYuan 

like '%又红又专%' or area like '%又红又专%') 
只要查不到值,就报错

#24


貌似很火啊,坐等结贴