我以前工作的代码,从数据库中提取表突然没有显示在我的组合框中。通过comboBox可以看到数据正在被拉出,但是项目不可见。如下图所示:comboBox停止显示SQL表
这是我的代码拉表:
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");
}
}
调试代码并查看查询是否返回记录 – imsome1
确认您正在访问正确的数据库。除此之外,你的代码似乎没有问题 –