2013-04-16 68 views
0

SQL語法這是我的選擇SQL語法中顯示的類別選擇, 但我需要做的是顯示相應的類別有什麼根據equipment_id內連接在vb.net

選擇我有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

假設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 

爲了進一步獲得更多的知識有關加入,請訪問以下鏈接:

+0

我會盡量先生,我真的不是用來使用的參數,但感謝您的信息 – ivandinglasan

+0

我在你前面的問題已經看到了你問參數':D'請不要用它。 –

+1

會做先生,即時通訊仍然試圖學習和了解它 – ivandinglasan

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 
+0

先生ID包含字符和整數前。 oeq-su-001什麼是相應的sqldbtype – ivandinglasan

+0

將sqldbtype與sql server中的列定義相匹配。看看這裏的可能性列表:http://msdn.microsoft.com/en-us/library/system.data.sqldbtype.aspx –