我有10几个TextBox的查询,当TextBox内容为空时,我想将其滤掉
但是在sql里的条件怎么写
例:select * from 表 where 列1=TextBox1.Text and 列2 = TextBox2.Text
....
如果TextBox2为空时 那么条件 列2 = TextBox2.Text 就不要了了这段sql应该怎么写好
但是在sql里的条件怎么写
例:select * from 表 where 列1=TextBox1.Text and 列2 = TextBox2.Text
....
如果TextBox2为空时 那么条件 列2 = TextBox2.Text 就不要了了这段sql应该怎么写好
set @sql='select * from T where 1=1 '
if TextBox1.Text<>""
set @sql=@sql+' and 列1='''+TextBox1.Text+''' '
if TextBox2.Text<>""
set @sql=@sql+' and 列2='''+TextBox2.Text+''' '
...exec(@sql)--引號問題自己注意下
if TextBox1.Text =="" || TextBox1.Text ==null
sql+=" and 列1='"+TextBox1.Text+"'";
iSQL = "select * from 表 where 1=1"
iSQL = iSQL & iif(TextBox1.Text = "", "", " and 列1='" & TextBox1.Text & "'")
iSQL = iSQL & iif(TextBox2.Text = "", "", " and 列1='" & TextBox2.Text & "'")
.....
iSQL = iSQL & iif(TextBox10.Text = "", "", " and 列1='" & TextBox10.Text & "'")这样可以拼出一个sql语句 , 然后用command对象执行这个语句就可以了.
string sql="select * from 表 where 1=1 ";
if TextBox1.Text <>"" && TextBox1.Text <>null
sql+=" and 列1='"+TextBox1.Text+"'";
一种用动态SQL写,为空就不要这个条件不为空就+' and 条件'
另一种用(条件 or 参数 is null) and ...
dim sql as string
dim flag as boolean
sql="select * from 表"
flag=false
if trim(TextBox1.Text)<>"" then
sql=sql & " where 列1=" & TextBox1.Text
flag=true
end ifif trim(TextBox2.Text)<>"" then
if flag=false then
sql=sql & " where 列2=" & TextBox2.Text
else
sql=sql & " and 列2=" & TextBox2.Text
flag=true
end ifif trim(TextBox3.Text)<>"" then
if flag=false then
sql=sql & " where 列3=" & TextBox3.Text
else
sql=sql & " and 列3=" & TextBox3.Text
flag=true
end if...
column2_name like '%'+TextBox2.Text+'%' and column3_name like '%'+TextBox3.Text+'%'like 就可以了。
str sql = "select * from 表 where 1=1"
if (TextBox1.Text != NULL)
strsql =str + " and" +"列1=TextBox2.Text"