我已经使用以下代码从Microsoft Access数据库中提取数据以填充VB.NET中的Combobox
Private Sub PopulateList()
If RBDura.Checked = True Then
CmbTstNo.Items.Clear()
myConnection.ConnectionString = connstring
Try
myConnection.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
str = "SELECT DISTINCT Test_Code FROM Table_Durability_Test WHERE Log_Date BETWEEN " & DtFrom.Text & " and " & DtTo.Text & ""
cmd = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
CmbTstNo.Items.Add(dr.Item(0))
End While
myConnection.Close()
CmbTstNo.SelectedIndex = 0
End If
End Sub
然而,当代码运行时,我收到错误“没有给出一个或多个必需参数的值”
请指导..
答案 0 :(得分:0)
您拥有的SQL字符串是"SELECT DISTINCT Test_Code FROM Table_Durability_Test WHERE Log_Date BETWEEN " & DtFrom.Text & " and " & DtTo.Text & ""
如果使用SQL输入文本或字符串到数据库,必须用单引号包围每个文本条目,这样您的SQL字符串将如下所示:
SELECT DISTINCT Test_Code FROM Table_Durability_Test WHERE Log_Date BETWEEN 'Value of DtFrom.Text' and 'Value of DtTo.Text'
请注意,我已使用单引号包围了Value of DtTo.Text
和Value of DtFrom.Text
。