如何在 VB.NET 中使搜索不敏感?
How do I make a search insensitive in VB.NET?
这是我的第一个post,请让我知道如何改进。
我的问题分为两部分。首先,我制作了一个搜索功能,它在文本框中获取文本,在数据库中引用它,并将结果添加到数据网格视图中。它有效,但它区分大小写,而且冗长。我已经设法解决了敏感性问题,但并非没有添加大量行。此外,我的方法在数据网格视图中产生了多个相同的结果。
我的问题是:是否有更简单的方法来编写此搜索代码,以及如何使其不敏感?
dgvFileInfo.Rows.Clear()
If searchBox.Text = "" Then
ElseIf srchCrit = "ProjectNumber" Then
Dim srchDesc As String = ""
Dim rtrnDesc As String = ""
Dim j As Integer = 0
For Each row As DataRow In TblFiles.Rows
rtrnDesc = row.Item(srchCrit).ToString
srchDesc = searchBox.Text
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
Next
Else
Dim srchDesc As String = ""
Dim rtrnDesc As String = ""
Dim j As Integer = 0
Dim letter As Char = searchBox.Text.Substring(0, 1)
Dim line As String = searchBox.Text.Substring(1, searchBox.Text.Length - 1)
For Each row As DataRow In TblFiles.Rows '***Here's my sensitivity issue**
rtrnDesc = row.Item(srchCrit).ToString
srchDesc = UCase(letter) & line
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
srchDesc = LCase(letter) & line
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
srchDesc = LCase(letter) & LCase(line)
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
srchDesc = UCase(letter) & UCase(line)
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
Next
End If
1) 您正在应用程序端进行比较,这意味着它发生在 VB 代码中。要进行不区分大小写的搜索,您可以执行 .ToUpper()
或 .ToLower()
并以这种方式进行比较。或者,您可以使用 String.Compare
.
' using ToUpper (ToLower would be the same)
Dim isFound as Boolean = ("My string".ToUpper() = row("myField").ToString().ToUpper())
' using String.Compare
Dim isFound as Boolean = (String.Compare("My String", row("myField"), True) = 0)
2) 最简单的方法是在数据库中进行搜索。如果你能做到这一点并且你的数据库不区分大小写(SQL 服务器实例默认不区分大小写),你现在可以使用 LINQ and/or 一个像 NHibernate 这样的 ORM 来构建和执行 sql数据库。这将是一长串问题,这些问题已在 SO 和网络上被多次回答。
我通过其他一些帖子找到了一种不区分大小写的搜索方法。
If myString.IndexOf(myVar, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
'your code here
End If
这对我来说效果很好,我希望你觉得它有用。
这是我的第一个post,请让我知道如何改进。
我的问题分为两部分。首先,我制作了一个搜索功能,它在文本框中获取文本,在数据库中引用它,并将结果添加到数据网格视图中。它有效,但它区分大小写,而且冗长。我已经设法解决了敏感性问题,但并非没有添加大量行。此外,我的方法在数据网格视图中产生了多个相同的结果。
我的问题是:是否有更简单的方法来编写此搜索代码,以及如何使其不敏感?
dgvFileInfo.Rows.Clear()
If searchBox.Text = "" Then
ElseIf srchCrit = "ProjectNumber" Then
Dim srchDesc As String = ""
Dim rtrnDesc As String = ""
Dim j As Integer = 0
For Each row As DataRow In TblFiles.Rows
rtrnDesc = row.Item(srchCrit).ToString
srchDesc = searchBox.Text
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
Next
Else
Dim srchDesc As String = ""
Dim rtrnDesc As String = ""
Dim j As Integer = 0
Dim letter As Char = searchBox.Text.Substring(0, 1)
Dim line As String = searchBox.Text.Substring(1, searchBox.Text.Length - 1)
For Each row As DataRow In TblFiles.Rows '***Here's my sensitivity issue**
rtrnDesc = row.Item(srchCrit).ToString
srchDesc = UCase(letter) & line
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
srchDesc = LCase(letter) & line
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
srchDesc = LCase(letter) & LCase(line)
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
srchDesc = UCase(letter) & UCase(line)
If rtrnDesc.Contains(srchDesc) Then
dgvFileInfo.Rows.Add()
dgvFileInfo.Item(0, j).Value = row.Item("ProjectNumber").ToString
dgvFileInfo.Item(1, j).Value = row.Item("ProjectName").ToString
dgvFileInfo.Item(2, j).Value = row.Item("Client").ToString
dgvFileInfo.Item(3, j).Value = row.Item("City").ToString & ", " & row.Item("State").ToString
j += 1
End If
Next
End If
1) 您正在应用程序端进行比较,这意味着它发生在 VB 代码中。要进行不区分大小写的搜索,您可以执行 .ToUpper()
或 .ToLower()
并以这种方式进行比较。或者,您可以使用 String.Compare
.
' using ToUpper (ToLower would be the same)
Dim isFound as Boolean = ("My string".ToUpper() = row("myField").ToString().ToUpper())
' using String.Compare
Dim isFound as Boolean = (String.Compare("My String", row("myField"), True) = 0)
2) 最简单的方法是在数据库中进行搜索。如果你能做到这一点并且你的数据库不区分大小写(SQL 服务器实例默认不区分大小写),你现在可以使用 LINQ and/or 一个像 NHibernate 这样的 ORM 来构建和执行 sql数据库。这将是一长串问题,这些问题已在 SO 和网络上被多次回答。
我通过其他一些帖子找到了一种不区分大小写的搜索方法。
If myString.IndexOf(myVar, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
'your code here
End If
这对我来说效果很好,我希望你觉得它有用。