mysql实现搜索_mysql – 如何实现更高效的搜索功能?

原始答案

(向下滚动以查看更新)

你能尝试以下方法吗?

>构建一个列表,仅包含具有输入的文本框的值

>设置连接的字符串以及“AND”字符串

>将该字符串附加到标准SELECT语句

代码如下所示:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim Predicate1 As String = Me.TextBox1.Text

Dim Predicate2 As String = Me.TextBox2.Text

Dim Predicate3 As String = Me.TextBox3.Text

Dim PredicateList As New List(Of String)

Dim WhereClause As String

Dim Query As String

If Predicate1 <> String.Empty Then

PredicateList.Add("Name=""" & Predicate1 & """")

End If

If Predicate2 <> String.Empty Then

PredicateList.Add("Age=""" & Predicate2 & """")

End If

If Predicate3 <> String.Empty Then

PredicateList.Add("Gender=""" & Predicate3 & """")

End If

WhereClause = String.Join(" AND ", PredicateList.ToArray)

Query = "SELECT * FROM TABLE WHERE " & WhereClause

MessageBox.Show(Query)

End Sub

更新

继SQL注入的注释之后,这里有一个更新的示例.

Dim Command As SqlClient.SqlCommand

Dim Predicate1 As String = Me.TextBox1.Text

Dim Predicate2 As String = Me.TextBox2.Text

Dim Predicate3 As String = Me.TextBox2.Text

Dim ParameterList As New List(Of SqlClient.SqlParameter)

Dim PredicateList As New List(Of String)

Dim BaseQuery As String = "SELECT * FROM TABLE WHERE "

If Predicate1 <> String.Empty Then

PredicateList.Add("name = @name")

ParameterList.Add(New SqlClient.SqlParameter("@name", Predicate1))

End If

If Predicate2 <> String.Empty Then

PredicateList.Add("age = @age")

ParameterList.Add(New SqlClient.SqlParameter("@age", Predicate2))

End If

If Predicate3 <> String.Empty Then

PredicateList.Add("gender = @gender")

ParameterList.Add(New SqlClient.SqlParameter("@gender", Predicate3))

End If

Command = New SqlClient.SqlCommand(BaseQuery & String.Join(" AND ", PredicateList.ToArray))

Command.Parameters.AddRange(ParameterList.ToArray)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值