comboBox停止显示SQL表

时间:2017-08-28 12:04:21

标签: c# sql-server winforms

之前正在使用的代码,从数据库中提取表格突然没有显示在我的comboBox中。可以通过comboBox看到数据被拉出但项目不可见。如下图所示:

enter image description here

这是我提取表格的代码:

private void fillOutputFile()
    {

        try
        {

            string connectionString = "Data Source=bid;Initial Catalog=BI;Integrated Security=True";
            using (SqlConnection con2 = new SqlConnection(connectionString))
            {
                con2.Open();
                string query = "Select table_name from INFORMATION_SCHEMA.columns";
                SqlCommand cmd2 = new SqlCommand(query, con2);

                SqlDataReader dr2 = cmd2.ExecuteReader();
                while (dr2.Read())
                {
                    int col = dr2.GetOrdinal("TABLE_NAME");
                    comboBox5.Items.Add(dr2[col].ToString());
                    //   con2.Close();
                }

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Your Windows Credentials does not have the rights to access/write to this table./nPlease contact your DB Administrator");
        }


    }

2 个答案:

答案 0 :(得分:2)

我知道这可能看起来很奇怪,但实际上可能会有效。将项目副本复制到PC上的其他位置。完成后,删除旧项目。将新项目复制到之前删除项目所在的位置。打开VS并尝试再次调试您复制的项目。它可能有用

答案 1 :(得分:1)

检查ComboBox的DrawMode属性,确保其未设置为OwnerDrawFixedOwnerDrawVariable。您需要将其设置为Normal

将其设置为OwnerDrawFixedOwnerDrawVariable会导致此问题:

enter image description here