如何基于vb.net中六个文本框中的用户输入发出dinamic sql请求?

如何基于vb.net中六个文本框中的用户输入发出dinamic sql请求?,vb.net,Vb.net,我正在为我的应用程序制作一个搜索细化程序,目前我正在根据用户填写的表单进行查询,返回相关列表。问题是表单字段是可选的,我无法确定如何根据输入的字段更改查找 我们在SQL中所做的是执行以下操作: Select * from table where (ISNULL(@field1, '') = '' OR @field1 = table.field1) and (ISNULL(@field2, '') = '' OR @field2 = table.field2) a

我正在为我的应用程序制作一个搜索细化程序,目前我正在根据用户填写的表单进行查询,返回相关列表。问题是表单字段是可选的,我无法确定如何根据输入的字段更改查找

我们在SQL中所做的是执行以下操作:

Select * from table where 
    (ISNULL(@field1, '') = '' OR @field1 = table.field1) 
    and (ISNULL(@field2, '') = '' OR @field2 = table.field2)  
    and (ISNULL(@field3, '') = '' OR @field3 = table.field3)  
    and (ISNULL(@field4, '') = '' OR @field4 = table.field4)  
    and (ISNULL(@field5, '') = '' OR @field5 = table.field5)  
    and (ISNULL(@field6, '') = '' OR @field6 = table.field6)
或者在VB中

dim sql as string = "Select * from table where 
    (ISNULL(" & textBobx1.text & ", '') = '' OR " & textBobx1.text & "= table.field1) 
    and (ISNULL(" & textBobx2.text & ", '') = '' OR " & textBobx2.text & "= table.field2)  
    and (ISNULL(" & textBobx3.text & ", '') = '' OR " & textBobx3.text & "= table.field3)  
    and (ISNULL(" & textBobx4.text & ", '') = '' OR " & textBobx4.text & "= table.field4)  
    and (ISNULL(" & textBobx5.text & ", '') = '' OR " & textBobx5.text & "= table.field5)  
    and (ISNULL(" & textBobx6.text & ", '') = '' OR " & textBobx6.text & "= table.field6)"

您只需传递所有字段,确保如果表单上的字段未填写,请传递一个空格,或者可以调用您在文本框文本中传递的函数,该函数返回NULL。

没有您的代码,任何人都无法提供任何好的帮助。请检查是否允许SQL注入。你应该经常使用参数。谢谢你的回答。我刚刚添加了几个单引号,效果非常好!