Do you have any suggestions for my code below that you might have to improve the performance? This is .NET 2.0 framework, and the DataTable gets bound to a DataGridview. The data is loaded into the Datatable via .ReadXML() it doesn't come from a database. There can be any where from 80 to 100k of records. The Terms() array is what the user passed it for the search. So "bob taco" would be terms(0) = "bob" , terms(1) = "taco". I have a regex that maintains any quoted terms. So "bob taco" would be on one element of the array. Is there a better way? I tried using Dataview since that has better performance but it didn't look like I could use the LIKE operator. Any suggestions are welcome, I'd really like to speed this up a bit.
Public Function Search(ByVal Terms() As String, ByRef ResidentTBL As DataTable) As DataTable
'Dim t As Long = Now.Ticks
Dim j As Integer
Dim newdt As New DataTable("Users")
Dim newtable As New DataTable
newtable = ResidentTBL.Clone
Dim termsceiling As Integer
termsceiling = Terms.GetUpperBound(0)
Dim filterstr As String = String.Empty
Dim foundrows() As DataRow
Dim sortOrder As String = "displayname ASC"
Dim tempstr As String
For j = 0 To termsceiling
'remedy any invalid sql characters
tempstr = Terms(j).Trim.ToUpper
tempstr = tempstr.Replace("'", "''")
tempstr = tempstr.Replace("*", "")
tempstr = tempstr.Replace("%", "")
'assemble the sql query
filterstr = filterstr & _
"((column1 LIKE '" & tempstr & "%') OR " & _
"(column2 LIKE '" & tempstr & "%') OR " & _
"(column3 LIKE '" & tempstr & "%') OR " & _
"(column4 LIKE '" & tempstr & "%') OR " & _
"(column5 LIKE '" & tempstr & "%') OR " & _
"(column6 LIKE '" & tempstr & "%') OR " & _
"(column7 LIKE '" & tempstr & "%') OR " & _
"(column8 LIKE '" & tempstr & "%') OR " & _
"(column9 LIKE '" & tempstr & "%') OR " & _
"(column10 LIKE '" & tempstr & "%'))"
'if there are further iterations append an AND (user typed more than one term)
If termsceiling > 0 And j <> termsceiling Then
filterstr = filterstr & " AND "
End If
Next j
filterstr = "(" & filterstr & ")" 'wrap the entire query
foundrows = ResidentTBL.Select(filterstr, sortOrder)
For i = 0 To foundrows.Length - 1
newtable.ImportRow(foundrows(i))
Next i
newdt = newtable
'Begin Debugging Code:
't = Now.Ticks - t
'MessageBox.Show("Took " & (t / 10000000) & " seconds.")
'End Debugging Code:
Return newdt
End Function