首先设定
string sql="select * from tb where 1=1"
if(txt_SupplierID.Text!="")sql+=" and SupplierID='"+txt_SupplierID.Text+"'"
if(txt_MaterialName.Text!="")sql+=" and MaterialName='"+txt_MaterialName.Text+"'"
if(txt_Datetime.Text!="")sql+=" and Datetime='"+txt_Datetime.Text+"'"
这样
任意不为空的情况作为查询条件
if len(trim(txt_SupplierID.text))>0 then
'这里是算是字符型
sqladd=sqladd &" where supplierid=N'"& txt_SupplierID.text &"' "
end if
if len(trim(txt_MaterialName.text))>0 then
if len(sqladd)>5 then
sqladd=sqladd &" and "
else
sqladd=sqladd &" where "
end if
sqladd=sqladd &" supplierid=N'"& txt_MaterialName.text &"' "
end if
if len(trim(txt_Datetime.text))>0 then
if len(sqladd)>5 then
sqladd=sqladd &" and "
else
sqladd=sqladd &" where "
end if
sqladd=sqladd &" supplierid=N'"& txt_Datetime.text &"' "
end if
dim sql as string = "select top 100 * from tab1 "& sqladd