我使用来自 SQLite 数据库的查询作为 ComboBox 的 AutocompleteCustomSource。另外,我想在单独的线程中加载数据。我的 LoadData 方法在直接调用时工作正常,但在从 BackgroundWorker 线程调用时会失败。当从后台线程调用它时,它会在行Specified cast is not valid
上抛出异常csearch.AutoCompleteCustomSource.Add(hh("Taj"))
。下面是我的代码:
Sub LoadData()
Dim connetionString As String
Dim cnn As SQLiteConnection
connetionString = "Data Source=" + Application.StartupPath + "\Mydatabase.db;"
cnn = New SQLiteConnection(connetionString)
cnn.Open()
Dim sqlComm88 As New SQLiteCommand("SELECT Taj FROM Taj_deu ", cnn)
Dim hh As SQLiteDataReader = sqlComm88.ExecuteReader()
While hh.Read()
csearch.AutoCompleteCustomSource.Add(hh("Taj"))
End While
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Call loaddata()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
BackgroundWorker1.RunWorkerAsync()
End Sub