从多个Sql表中绑定具有唯一数据值字段的下拉列表

本文关键字:数据 唯一 字段 下拉列表 Sql 绑定 | 更新日期: 2023-09-27 18:08:49

我想从多个SqlDatabase表中选择多个列来绑定具有唯一数据值字段的下拉列表。

下面是我的c#代码与SqlQuery从表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

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'))

从多个Sql表中绑定具有唯一数据值字段的下拉列表

Q: "如何在Sql查询中包含Mytable2和它的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'))