使用SQL LIKE运算符自动完成TextBox

时间:2016-04-03 04:52:40

标签: .net sql-server vb.net visual-studio vb.net-2010

我在桌面应用程序中有一个自动完成TextBox,它从Sql DataBase填充列表。
键入时,仅从第一个字符开始过滤 我如何提供一个选项来搜索自动完成文本的中间或第一个或任何字符作为LIKE运算符?

这是我的代码(不工作):

Private Sub OrderNoTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderNoTextBox.Click

    Dim connection As New SqlConnection(cnn)
    connection.Open()

    Dim cmd As SqlCommand = New SqlCommand("SELECT dbo.[Order].OrderNo FROM dbo.[Order] INNER JOIN dbo.BatchInformation ON dbo.[Order].OrderID = dbo.BatchInformation.OrderID GROUP BY dbo.[Order].OrderNo HAVING dbo.[Order].OrderNo LIKE '%" & Trim(OrderNoTextBox.Text) & "%' ", connection)
    Dim reader As SqlDataReader = cmd.ExecuteReader()
    Dim list As New AutoCompleteStringCollection

    While reader.Read
        If reader.IsDBNull(0) = False Then
            list.Add(reader.Item("OrderNo"))
        End If
    End While

    reader.Close()

    OrderNoTextBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    OrderNoTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource
    OrderNoTextBox.AutoCompleteCustomSource = list

    connection.Close()
End Sub

0 个答案:

没有答案