这是我用来显示类别选择的选择SQL语法,但是我需要做的是显示基于equipment_id选择的相应类别

我有2个表,tblOfficeEquipmentCategory和tblOfficeEquipmentProfile

我需要内部加入tblOfficeEquipmentProfile,以便可以添加WHERE equipment_id ='“ txtid.text”'

对应的SQL语法是什么

Public Sub DisplayCategory()
    'based on oe_id
    Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim sqlcommand As SqlCommand

    sqlconn.Open()
    Dim da As New SqlDataAdapter("select * from tblOfficeEquipmentCategory", sqlconn)
    Dim dt As New DataTable
    da.Fill(dt)
    cmbCategory.DataSource = dt
    cmbCategory.ValueMember = "CAT_Name"
    cmbCategory.DisplayMember = "CAT_ID"
    sqlconn.Close()
End Sub

===============>>#1 票数:1

假设equipment_id位于表tblOfficeEquipmentProfile并且列CAT_ID其链接到表tblOfficeEquipmentCategory

SELECT  a.CAT_Name
FROM    tblOfficeEquipmentCategory a
        INNER JOIN tblOfficeEquipmentProfile b
            ON a.CAT_ID = b.CAT_ID
WHERE   b.equipment_id = @ID

要进一步获得有关联接的知识,请访问以下链接:

===============>>#2 票数:0

最好开始考虑如何将数据访问与演示代码区分开。 您需要这样的东西:

Public Function DisplayCategory() 
    cmbCategory.ValueMember = "CAT_Name"
    cmbCategory.DisplayMember = "CAT_ID"
    cmbCategory.DataSource = GetEquipmentCategories(Convert.ToInt32(txtid.text))
End Sub

Public Function GetEquipmentCategories(ByVal EquipmentID As Integer) As DataTable
    'based on oe_id

    Dim sql As String = "SELECT a.CAT_ID, a.CAT_Name" & _ 
                       " FROM tblOfficeEquipmentCategory a" & _
                       " INNER JOIN tblOfficeEquipmentProfile b ON a.CAT_ID = b.CAT_ID" & _
                       " WHERE b.equipment_id = @ID"

    Dim result As New DataTable
    Using cn New SqlConnection("server=SKPI-APPS1;Database=EOEMS;integrated security=true"), _
          cmd As new SqlCommand(sql, cn)

        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = EquipmentID

        cn.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader
            result.Load(rdr)
        End Using
    End Using
    Return result
End Function

  ask by ivandinglasan translate from so

未解决问题?本站智能推荐: