我正在尝试使用SQL数据库中表的名称填充下拉列表。 这是我的代码尝试,但是当我调用它并且下拉列表保持为空时没有发生任何事情。
public void FillDropDownList()
{
String connString = "myConnectionString";
//**STRING UPDATED**
String Query = "SELECT * FROM information_schema.tables";
using (var cn = new SqlConnection(connString))
{
cn.Open();
DataTable dt = new DataTable();
try
{
SqlCommand cmd = new SqlCommand(Query, cn);
SqlDataReader myReader = cmd.ExecuteReader();
dt.Load(myReader);
}
catch (SqlException e)
{
//to be completed
}
radDropDownList1.DataSource = dt;
radDropDownList1.ValueMember = "TABLES_NAME";
radDropDownList1.DisplayMember = "TABLES_NAME";
}
}
如何使用上述查询填充下拉列表?
答案 0 :(得分:2)
您的查询不正确,需要
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
答案 1 :(得分:0)
问题在于您使用ValueMember
和DisplayMember
的字符串,请替换下面的
radDropDownList1.ValueMember = "TABLES_NAME";
radDropDownList1.DisplayMember = "TABLES_NAME";
与
radDropDownList1.ValueMember = "TABLE_NAME";
radDropDownList1.DisplayMember = "TABLE_NAME";