
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

#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";
}
{
sqlText += " and UserName=@userName ";
}
if (this.DropDownList1.SelectedValue == "在线")
{
sqlText += " and IsOnline=true ";
}
if(this.DropDownList2.SelectedValue=="可以"){
sqlText += " and IsAllowMessage=true";
}
#4
我的sql语句没问题,问题是我不知道这个逻辑怎么写,三个条件,不会得写6个判断语句吧,要是再多个条件那就写不完了!
#5
怎么就没有问题??这里明显少空格!
就是这么写的!!!拼接sql
#6
我刚才又运行了一下,能查询,我想知道多个条件查询改怎么写,if语句怎么嵌套
#7
你就不该这样写:
this.DropDownList1.SelectedValue 的值这样的东西,在前台组织好以后,
全部传给BLL层就好,而BLL传DAL再调存储过程。
把查询语句放在表示层,当然会造成过度的依赖~
建议楼主找个三层结构的文章去看看~
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";
}
这样来看看。
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试试
把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";
}
这里是并列的拼接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吧
应该用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
where (f1 = @p1 or @p1 is null)
and (f2 = @p2 or @p2 is null)
如果要忽略 f1 列,就传 null 给 @p1
#15
截图就是这个,怎么写可以实现所有的条件都能查询啊,当用户名为空时可以查询在线允许发言、在线不允许发言、不在线允许发言、不在线不允许发言,当用户名不为空时也能查询所有条件,这几中情况我用语句写不出来,麻烦各位了,帮帮我!
#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=="可以");
sql = String.Format(sql, TextBox1.Text.Replace("'","''").Trim(), this.DropDownList1.SelectedValue == "在线", this.DropDownList2.SelectedValue=="可以");
#17
我学的时间不长呢,看不懂啊,能不能解释一下?
#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";
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 '%又红又专%')
只要查不到值,就报错
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

#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";
}
{
sqlText += " and UserName=@userName ";
}
if (this.DropDownList1.SelectedValue == "在线")
{
sqlText += " and IsOnline=true ";
}
if(this.DropDownList2.SelectedValue=="可以"){
sqlText += " and IsAllowMessage=true";
}
#4
我的sql语句没问题,问题是我不知道这个逻辑怎么写,三个条件,不会得写6个判断语句吧,要是再多个条件那就写不完了!
#5
怎么就没有问题??这里明显少空格!
就是这么写的!!!拼接sql
#6
我刚才又运行了一下,能查询,我想知道多个条件查询改怎么写,if语句怎么嵌套
#7
你就不该这样写:
this.DropDownList1.SelectedValue 的值这样的东西,在前台组织好以后,
全部传给BLL层就好,而BLL传DAL再调存储过程。
把查询语句放在表示层,当然会造成过度的依赖~
建议楼主找个三层结构的文章去看看~
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";
}
这样来看看。
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试试
把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";
}
这里是并列的拼接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吧
应该用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
where (f1 = @p1 or @p1 is null)
and (f2 = @p2 or @p2 is null)
如果要忽略 f1 列,就传 null 给 @p1
#15
截图就是这个,怎么写可以实现所有的条件都能查询啊,当用户名为空时可以查询在线允许发言、在线不允许发言、不在线允许发言、不在线不允许发言,当用户名不为空时也能查询所有条件,这几中情况我用语句写不出来,麻烦各位了,帮帮我!
#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=="可以");
sql = String.Format(sql, TextBox1.Text.Replace("'","''").Trim(), this.DropDownList1.SelectedValue == "在线", this.DropDownList2.SelectedValue=="可以");
#17
我学的时间不长呢,看不懂啊,能不能解释一下?
#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";
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 '%又红又专%')
只要查不到值,就报错
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
貌似很火啊,坐等结贴