對於我的代碼,您是否有任何建議可以提高性能?這是.NET 2.0框架,並且DataTable被綁定到DataGridview。數據通過.ReadXML()加載到數據表中,它不是來自數據庫。可以有任何地方從80到100K的記錄。 Terms()數組是用戶通過搜索的內容。所以「鮑勃塔科」將是術語(0)=「鮑勃」,條款(1)=「塔科」。我有一個正則表達式來維護任何引用的條款。所以「鮑勃炸玉米餅」將在陣列的一個元素。有沒有更好的辦法?我嘗試使用Dataview,因爲它具有更好的性能,但它看起來不像我可以使用LIKE運算符。歡迎任何建議,我真的想加快這一點。將Datatable綁定到Datagridview
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
順便說一句:如果術語是一維數組,您可以使用術語變量的Lenght屬性insead。 – smok1 2009-08-04 14:35:50