简体   繁体   中英

Implement an autocomplete textbox that is efficient with large database in vb.net?

In a Windows application, I'm experiencing performance issues when populating autocompletestring Collection with a large number of items (appr. 60000). Retrieving the data from the database to a datatable is quick enough(< 1 sec), but populating the Collection is much slower since I am iterating through a datatable to fill the collection. Is there a quicker way to perform this operation. I'm populating like this:

    Private Sub txtName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
    If txtName.TextLength = 2 And e.KeyCode <> Keys.Back Then
        DataCollection.Clear()
        pObjDT = pobjDB.GetDT("Select Name from partymaster where AgentRef ='" & txtAgent.Text & "' And  Name LIKE '" & txtName.Text & "%' ")
        If Not pObjDT Is Nothing Then
            For Each lObjDataRow As DataRow In pObjDT.Rows
                DataCollection.Add(lObjDataRow.Item(0))
            Next
        End If
    End If
End Sub

Why you query so big collection for autocomplete? I would query something like top 10 or 20 like

Select top 10 Name from partymaster where AgentRef ='" & txtAgent.Text & "' And  Name LIKE '" & txtName.Text & "%

Check if AddRange() is more efficient than for each:

Dim theStrings As String() = pObjDT.AsEnumerable().Select(Function(therow)
                                                             Return therow(0).ToString()
                                                          End Function).ToArray()
 DataCollection.AddRange(theStrings)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM