2011-04-30 28 views
1

如何用vb.net中的數據閱讀器填充AutoCompleteCustomSource列表和查詢結果?如何填寫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 

回答