我正在使用VB将Access数据库连接到我的程序,我有两个组合,我希望第二个组合数据源取决于第一个组合选择的项目,这里是我到目前为止所做的代码: / p>
Try
Dim dbcon As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\inventorysys.accdb;Persist Security Info=False;")
dbcon.Open()
Dim sqlquery As String = ("SELECT DISTINCT Brand FROM inventory WHERE Category = ' " & catogerycombotxt.SelectedItem & " ' ")
Dim comm As New OleDb.OleDbCommand(sqlquery, dbcon)
Dim rs As OleDb.OleDbDataReader = comm.ExecuteReader
Dim dt As DataTable = New DataTable
dt.Load(rs)
' as an example set the ValueMember and DisplayMember'
' to two columns of the returned table'
brandcombotxt.DataSource = dt
brandcombotxt.ValueMember = "Brand"
brandcombotxt.DisplayMember = "Brand"
dbcon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
它没有错误,但没有结果,希望你可以帮助我
答案 0 :(得分:0)
两件事:
首先,我相信您的问题是在查询中您将两个空格之间的选定值连接起来:
Dim sqlquery As String = ("SELECT DISTINCT Brand FROM inventory WHERE Category = ' " & catogerycombotxt.SelectedItem & " ' ")
应该是
Dim sqlquery As String = ("SELECT DISTINCT Brand FROM inventory WHERE Category = '" & catogerycombotxt.SelectedItem & "'")
第二:了解parameterized queries。由于许多原因,这是创建和执行sql查询而不是字符串连接的更好方法。