【问题标题】:fill data to combo box depending upon another combo box value根据另一个组合框值将数据填充到组合框
【发布时间】:2015-03-14 14:56:40
【问题描述】:

我正在使用 VB 将 Access 数据库连接到我的程序,并且我有两个组合,我希望第二个组合数据源取决于第一个组合选定项,这是我到目前为止所做的代码:

 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

它没有错误但没有结果,希望你能帮助我

【问题讨论】:

    标签: vb.net ms-access combobox


    【解决方案1】:

    两件事:

    首先,我认为您的问题是在您的查询中,您将所选值连接在两个空格之间:

    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 查询的方法。

    【讨论】:

    • 我修复了查询,但代码仍然无法正常工作,你能告诉我如何参数化我的查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2016-01-29
    相关资源
    最近更新 更多