C# 在DataGridView C中显示MySQL数据库值#

C# 在DataGridView C中显示MySQL数据库值#,c#,mysql,C#,Mysql,我与数据库建立了连接,但当我试图查看1个表中的某些数据时,dataGridView中没有显示这些数据。当我按下按钮时,几秒钟后似乎会显示值,然后在dataGridView 有人能解释一下我的代码是否出错了吗? 我在Visual Studio 2013工作 我的代码: private void button1_Click(object sender, EventArgs e) { string connectionString = "datasource=localhost;databas

我与数据库建立了连接,但当我试图查看1个表中的某些数据时,
dataGridView
中没有显示这些数据。当我按下按钮时,几秒钟后似乎会显示值,然后在
dataGridView

有人能解释一下我的代码是否出错了吗? 我在Visual Studio 2013工作

我的代码:

private void button1_Click(object sender, EventArgs e)
{
    string connectionString = "datasource=localhost;database=microweb;port=3306;username=root;password=pass";

    MySqlConnection conn = new MySqlConnection(connectionString);
    MySqlCommand cmd = new MySqlCommand("select * from reservations", conn);

    try
    {
        MySqlDataAdapter sda = new MySqlDataAdapter();
        sda.SelectCommand = cmd;
        DataTable dta = new DataTable();
        sda.Fill(dta);
        BindingSource bdsour = new BindingSource();

        bdsour.DataSource = dta;
        dataGridView1.DataSource = bdsour;
        sda.Update(dta);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }    
}
你应该设置为true


您的数据源真的命名为“localhost”吗?如果是,填充后dta中包含多少(任何?)条记录?是,命名为localhost。我在connectionString中做了一些更改,比如另一个数据库名、pass、datasource,并显示了一些错误,比如没有名为。。。或者密码不正确…我尝试在没有BindingSource的情况下使用,但没有任何更改…System.DataCommon.DbDataAdapter.Fill返回4Did您为datagrid创建列了吗?@AlexV很高兴能提供帮助
bdsour.DataSource = dta;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = bdsour;