2012-09-14 50 views
1

在我的數據庫中,有兩個表格。兩者都有一個用戶名列。 Table A是存儲用戶信息,而Table B是存儲用戶的預訂。在我的頁面中,會出現一個下拉菜單,它將爲當前登錄的用戶檢索BookingID。他們應該如何檢索?在數據庫中顯示當前用戶的不同表格

爲了幫助您理解,這應該會讓您知道我的意思。

Table B

用戶X登錄, If User X(Table A) = User X(Table B) 然後dropdown1節目BookingID用戶X的。

對不起,我不提供任何代碼,因爲我真的不知道該怎麼做。歡迎任何答案。提前致謝。

+0

你使用像實體框架的東西嗎?如果是這樣,有一些非常簡單的解決方案。否則,您可以從您的數據中手動創建一個數據表。 – yougotiger

回答

2

這將列出所有BookingID對用戶有這兩個表中:

select b.BookingID 
from tableA a 
inner join tableB b on a.username = b.username 
+0

在這種情況下,我應該爲連接創建一個新表嗎? –

+0

連接在SQL中完全正常。除非您有嚴重的性能問題,否則您不必避開它們。 –

1

使用SQL

SELECT 
    //TABLE_A.required_cols 
    //TABLE_B.required_cols 
FROM 
    TABLE_A 
JOIN TABLE_B ON TABLE_A.USER_ID = TABLE_B.USER_ID 

注::在這裏,我認爲這兩個表有一欄稱爲USER_ID,匹配相同的用戶,說X

0

創建一個數據表,然後填充它創建表爲f ollows:

Private Function CreateDataSource() As DataTable 
    'creates the columns for the datatable 
    Dim dt As New DataTable 'create new datatable 

    'add appropriate columns to the table 
    Dim colImage As New DataColumn("Field1", GetType(Boolean)) 
    colImage.DefaultValue = bShowExtraInfo 
    dt.Columns.Add(colImage) 
    dt.Columns.Add("Field2", GetType(String)) 

    Return dt 'return the table 
End Function 

,然後用它在你的代碼如下所示:

Dim dt As DataTable = CreateDataSource() 'create the data table 
    Dim dr As DataRow 
     For Each x In y 'cycle through x and add to table 
       dr("Field1") = tableAvalue 
       dr("Field2") = tableBvalue 
       dt.Rows.Add(dr) 
      End If 
     Next 
    gvEverything.DataSource = dt 
    gvEverything.DataBind() 
+0

可以在C#中提供嗎?將不勝感激:) –

+0

C#是不是我的特長',但基本上,只是改變周圍的東西。將前面的模糊處理掉,然後將該類型放在變量名稱的前面,例如,將dim dt替換爲datatable,它是datatable dt;我認爲(我可能是錯的)改變了大多數()的[],但你可能必須做一些比較互聯網上的東西。 – yougotiger

相關問題