这是我选择的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
答案 0 :(得分: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
要进一步了解联接,请访问以下链接:
答案 1 :(得分: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
答案 2 :(得分:0)
Dim sql = "Select Authors.Au_ID, Authors.Author, [Title Author].ISBN FROM(Authors INNER JOIN [Title Author] ON Authors.Au_ID = [Title Author].Au_ID)"
答案 3 :(得分:0)
Try
con.Open()
Dim cmd As New OleDb.OleDbCommand
Dim dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
Dim sql = "Select Authors.Au_ID, Authors.Author, [Title Author].ISBN FROM(Authors INNER JOIN [Title Author] ON Authors.Au_ID = [Title Author].Au_ID)"
cmd.Connection = con
cmd.CommandText = sql
da.SelectCommand = cmd
da.Fill(dt)
DGV2.DataSource = dt
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try