在 VB.NET 中寻找在 MYSQL 中搜索字符串模式的有效程序

looking for efficient procedure in VB.NET on searching string pattern in MYSQL

我正在寻找一个使用 vb.net 查询 mysql 数据库的有效过程,即使字符串出现时也会查找用户请求的字符串requested 拼写错误。

至于现在,我已经想到了这个过程。但我认为这会占用大量内存 space 性能,所以我想提出任何建议或建议。

  1. 遍历每个字符串索引并将每个索引字符替换为“%”(mysql 通配符)
  2. 每个循环查询 mysql 数据库以获取结果
  3. 如果每个循环的结果已经存在于上一个循环周期中,那么将比较每个循环的结果,因此您不会得到重复的结果
  4. 循环结束后,结果将按照字符串相似度百分比进行排序
  5. 字符串相似度的结果将相应显示

例如用户将输入"extra"和一些在数据库中的记录有一个包含"extreme","extravagance","extraordinary",[=38=的字符串],"programming",查询应该return "extravagance","extraordinary","extreme".

谢谢。

我已经为您的第一个问题找到了解决方案。但我需要其他四个的更多信息。

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%' ) 
 )