C# 用数据库c中的表填充组合框#

C# 用数据库c中的表填充组合框#,c#,sql,combobox,C#,Sql,Combobox,如何用数据库中的表(而不是特定表中的列)填充组合框 我已经做了一些常见的事情,比如“数据绑定”,但是当我运行/调试系统时,combobox中没有显示任何内容 这是我的密码: private void Form1_Load(object sender, EventArgs e) { try { SqlConnection connection = new SqlConnection(@"Data Source=USER-PC;

如何用数据库中的表(而不是特定表中的列)填充
组合框

我已经做了一些常见的事情,比如“数据绑定”,但是当我运行/调试系统时,combobox中没有显示任何内容

这是我的密码:

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            SqlConnection connection = new SqlConnection(@"Data Source=USER-PC;Initial Catalog=StudentDB;Integrated Security=True");

            string selectQuery = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'";
            connection.Open();
            SqlCommand command = new SqlCommand(selectQuery, connection);
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                comboBox1.Items.Add(reader.GetString("tables"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

有经验的开发人员提供了帮助吗?

您的代码中的列名错误,READER[“tables”],正确的列名是TABLE\u name,请尝试以下代码:

using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES", con))
    {
    using (SqlDataReader reader = com.ExecuteReader())
        {
        myComboBox.Items.Clear();
        while (reader.Read())
            {
            myComboBox.Items.Add((string) reader["TABLE_NAME"]);
            }
        }
    }
}

@Sudsy1002已编辑。只是一条建议:不要使用动态SQL。即使只是针对这样一个简单的查询,这也是一个坏习惯。如果您可以访问服务器,请查看学习存储过程。