如何使用vb.net中的数据阅读器使用查询结果填充AutoCompleteCustomSource列表?
需要示例代码。
编辑1:
这是我尝试过的,它不起作用
Private Sub cbEmployeeNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbEmployeeNo.Click
cbEmployeeNo.AutoCompleteCustomSource.Clear()
Dim mySelectQuery As String = "SELECT * FROM EmployeeTable WHERE " & cbSearch.Text & " LIKE '" & cbEmployeeNo.Text & "' AND Status LIKE '" & cbStatus.Text & "'"
Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
sqConnection.Open()
Try
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
Dim m As Integer
Do While sqReader.Read
For m = 1 To sqReader.FieldCount() - 1
If (Not sqReader.IsDBNull(m)) Then
If cbSearch.Text = "EmployeeID" Then
cbEmployeeNo.AutoCompleteCustomSource.Add(sqReader.GetInt32(m))
Else
cbEmployeeNo.AutoCompleteCustomSource.Add(sqReader.GetString(m))
End If
End If
Next m
Loop
sqReader.Close()
Catch ex As Exception
MsgBox("Error: " & ex.ToString, vbExclamation)
Finally
sqConnection.Close()
End Try
End Sub
编辑2:这是我尝试的第二个代码(遵循DevExpress的答案中的链接)。它无法正常工作
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sStringColl As New AutoCompleteStringCollection
Dim mySelectQuery As String = "SELECT * FROM EmployeeTable WHERE " & cbSearch.Text & " LIKE '" & cbEmployeeNo.Text & "' AND Status LIKE '" & cbStatus.Text & "'"
Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
sqConnection.Open()
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
While sqReader.Read()
sStringColl.AddRange(New String() {sqReader(0)})
End While
cbEmployeeNo.AutoCompleteCustomSource = sStringColl
End Sub