我已经为您的第一个问题找到了解决方案。但我需要其他四个的更多信息。
Private Function GetWhereClause() As String
Dim strSearch As String = TextBox1.Text
Dim strWhere As String = ""
If Not String.IsNullOrEmpty(strSearch) Then
strWhere = "WHERE"
For i As Integer = 1 To strSearch.Length
Dim tmp As String = strSearch
tmp = tmp.Insert(i, "%")
tmp = tmp.Remove(i - 1, 1)
If Not tmp.StartsWith("%") Then tmp = "%" & tmp
If Not tmp.EndsWith("%") Then tmp = tmp & "%"
strWhere &= IIf(i > 1, " OR", "") & " ( FieldToSearch LIKE '" & tmp & "' ) " & vbCrLf
Next
End If
Return strWhere
End Function
TextBox1.Text = "extra" 的结果是
WHERE ( FieldToSearch LIKE '%xtra%' )
OR ( FieldToSearch LIKE '%e%tra%' )
OR ( FieldToSearch LIKE '%ex%ra%' )
OR ( FieldToSearch LIKE '%ext%a%' )
OR ( FieldToSearch LIKE '%extr%' )
编辑
搜索多个单词只是另一个 for/next 循环。
Private Function GetWhereClause() As String
Dim strSearch() As String = TextBox1.Text.Split(",")
Dim strWhere As String = ""
If Not String.IsNullOrEmpty(strSearch.ToString) Then
strWhere = "WHERE"
For j As Integer = LBound(strSearch) To UBound(strSearch)
Dim strSplit As String = strSearch(j)
If j >= 1 Then
strWhere &= vbCrLf & " OR ( "
Else
strWhere &= " ( "
End If
For i As Integer = 1 To strSplit.Length
Dim tmp As String = strSplit
tmp = tmp.Insert(i, "%")
tmp = tmp.Remove(i - 1, 1)
If Not tmp.StartsWith("%") Then tmp = "%" & tmp
If Not tmp.EndsWith("%") Then tmp = tmp & "%"
strWhere &= IIf(i > 1, " OR", "") & " ( FieldToSearch LIKE '" & tmp & "' ) " & vbCrLf
Next
strWhere &= " ) "
Next
End If
Return strWhere
End Function
我的搜索字符串是extra,strings,结果如下:
WHERE ( ( FieldToSearch LIKE '%xtra%' )
OR ( FieldToSearch LIKE '%e%tra%' )
OR ( FieldToSearch LIKE '%ex%ra%' )
OR ( FieldToSearch LIKE '%ext%a%' )
OR ( FieldToSearch LIKE '%extr%' )
)
OR ( ( FieldToSearch LIKE '%trings%' )
OR ( FieldToSearch LIKE '%s%rings%' )
OR ( FieldToSearch LIKE '%st%ings%' )
OR ( FieldToSearch LIKE '%str%ngs%' )
OR ( FieldToSearch LIKE '%stri%gs%' )
OR ( FieldToSearch LIKE '%strin%s%' )
OR ( FieldToSearch LIKE '%string%' )
)