我正在寻找一个使用vb.net在mysql数据库上查询的高效过程,它将查找用户请求的字符串,即使请求的字符串拼写错误。
就目前而言,我想到的是这个程序。但我认为这将占用很大的记忆空间上的表现,所以我想征求任何建议或建议。
例如,用户将输入“额外”,并在数据库记录中输入包含“极端”、“奢侈”、“非凡”、“垃圾”、“编程”的字符串,查询应该返回“奢侈”、“非凡”、“极端”。
谢谢。
发布于 2015-02-06 20:07:59
我已经为你的第一个问题找到了解决办法。但我需要其他四个人的更多信息。
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 FunctionTextBox1.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%' ) 编辑
搜索乘法词只是/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%' )
) https://stackoverflow.com/questions/28350834
复制相似问题