2017-08-17 84 views
0

我想在一個下拉列表中顯示來自2個數據庫表的數據,其活動位置爲no。如何在一個下拉列表中編譯選定的數據?如何在一個下拉列表中顯示多個數據列?

我使用SqlDataSource從工具箱

SELECT [Desktop_ID], [DTComputerName] FROM [Desktop] WHERE ([DTActive] LIKE '%' + @ DTActive + '%')` 

SELECT [Notebook_ID], [NBComputerName] FROM [Notebook] WHERE ([NBActive] LIKE '%' + @ NBActive + '%') 

我想表明從兩個查詢DropDownList中選定的值。但是,我只能使用1個數據源來顯示。我正在使用Visual Studio 17和SQL Server Management Studio

回答

0

您可以在sql中使用union或union all將多個select查詢的結果集連接到單個結果集。

SELECT [Desktop_ID], [DTComputerName] FROM [Desktop] 
     WHERE ([DTActive] LIKE '%' + @ DTActive + '%')` 
UNION ALL 
SELECT [Notebook_ID], [NBComputerName] FROM [Notebook] 
     WHERE ([NBActive] LIKE '%' + @ NBActive + '%') 

一點要注意

  • 在所有的選擇集數列的應該是平等的。
  • 可能很難區分哪個值來自哪個表作爲結果組合到一個結果集中。
+0

如果您union語句使這兩個字段名稱相同或者 '[桌面ID],[DTComputerName]' 或「[桌面ID]作爲 '身份證',[DTComputerName]作爲 '價值' –

+0

@Saneeshkunjunni沒有需要在聯合選擇語句中具有相同的列名稱。最終結果集將使用第一個選擇語句列名稱來呈現結果。 –

0

您可以使用合併方法在C#。這裏是it.It工作相當精細的下拉列表項的例子。

 MySqlDataAdapter da = new MySqlDataAdapter("SELECT distinct year FROM daman_reading_2016 ", con); 
     MySqlDataAdapter ad = new MySqlDataAdapter("Select distinct year from daman_reading_2015 ", con); 
     DataSet ds1 = new DataSet(); 
     DataSet ds = new DataSet(); 
DataSet ds2 = new DataSet(); 
     ad.Fill(ds1); 
     da.Fill(ds); 
da1.Fill(ds2); 
     ds.Merge(ds1); 
ds2.Merge(ds); 

     ddlyear.DataSource = ds2; 
     ddlyear.DataValueField = "YEAR"; 
     ddlyear.DataTextField = "YEAR"; 
     ddlyear.DataBind(); 
     ddlyear.Items.Insert(0, new ListItem("Select Year", "0")); 
     con.Close(); 
相關問題