2014-06-16 75 views
0

我想從多個SqlDatabase表中選擇多個列來綁定具有唯一數據值字段的DropDownLists。從多個Sql Tables中綁定下拉列表與唯一數據值字段?

這裏是類SqlQuery我的C#代碼從表1選擇多列:

// Create a dropdownlist inside the dynamically created panel and insert in the 1st row 1st cell of dynamic html table 
var DDL = new WebForms.DropDownList(); 
DDL.ID = "MainDDL"; 
SqlDataSource da = new SqlDataSource(); 
da.ConnectionString = connection; 
string item = "--Select--"; 
da.SelectCommand = "SELECT DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE))as DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'MyTable1' AND COLUMN_NAME IN ('Column1','Column2','Column3','Column4','Column5'))"; 
DDL.DataSource = da; 
DDL.DataTextField = "COLUMN_NAME"; 
DDL.DataValueField = "DATA_TYPE"; 
DDL.DataBind(); 

解決方案所需:從多個SQL表選擇 多列:

如何從Mytable2中選擇column1也在下面的Sql查詢中

SELECT DATA_TYPE +'_'+ convert(varchar(10),ROW_NUMBER()OVER(ORDER BY DATA_TYPE))as DATA_TYPE,COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE(TABLE_NAME ='MyTable1'AND COLUMN_NAME IN('Column1','Column2 」, '欄3', 'Column4', 'Column5'))

回答

1

問: 「如何包括Mytable2及其下面的SQL查詢COLUMN1」

答:

SELECT 
    DATA_TYPE + '_' + convert(varchar(10), ROW_NUMBER() OVER(ORDER BY DATA_TYPE))as DATA_TYPE, 
    COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE 
    (TABLE_NAME = 'MyTable1' AND COLUMN_NAME IN ('Column1','Column2','Column3','Column4','Column5')) 
    OR 
    (TABLE_NAME = 'MyTable2' AND COLUMN_NAME IN ('Column1')) 
+0

謝謝你做到了!接受你的答案 – Shrivatsan

相關問題