0

我在从两个表中显示产品详细信息时遇到了一些问题。

我正在使用 VS 2010 和 MS Access 数据库。

我的数据库表结构如下:

  • 产品 ( #Product_ID, Category_ID, Product_Name, Product_Cost, Product_Price)

  • 类别 ( #Category_ID, Category_Name)

当我从组合框中选择时,我想要显示ProductCost,ProductPriceCategoryName进入文本框ProductName。我能够显示ProductCost&ProductPrice但无法显示CategoryName,因为我不确定如何将这两个表链接在一起。

我用来用 ProductName 填充 Combobox 的代码是:

Public Sub fillProductCombobox(ByVal sender As Object)
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Try
        conn.Open()
        da.SelectCommand = New OleDbCommand("SELECT * FROM Product", conn)
        da.Fill(dt)
        sender.DataSource = dt
        sender.DisplayMember = "Product_Name"
        sender.ValueMember = "Product_ID"

        'best method?
        frmAddSalesProduct.txtProductCost.DataBindings.Add("Text", dt, "Product_Cost")
        frmAddSalesProduct.txtPerPrice.DataBindings.Add("Text", dt, "Product_Price")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        conn.Close()
    End Try
End Sub

然后我在表单加载时以这种方式调用该函数:

fillProductCombobox(ProductComboBox)

这是我的表格的样子:Form example

请指导我如何显示CategoryName

Also is that the way I use to fill Product_Cost and Product_Price the best method?

P/S : For some reason I need to have everything done dynamically

4

1 回答 1

1

You can use the join query like

 SELECT Product_ID, p.Category_ID, Product_Name, Product_Cost, Product_Price, Category_Name
 FROM Product p
 INNER JOIN Category c ON p.Category_ID = c.Category_ID

So, you will get the category name using this query, just bind the text box as you are binding for others.

I hope it will help you. :)

于 2013-06-17T12:07:17.143 回答