无法连接到任何指定的MySql主机

时间:2018-07-18 06:16:50

标签: c# mysql database xampp

//这是我的连接数据库(xampp)的代码

private void runQuery()
{
    String ConnectionString = "Server=myServerAddress;Database=myDatabase;Uid=myUsername;Pwd=myPassword;";

    MySqlConnection connection = new MySqlConnection(ConnectionString);
    connection.Open();
    try
    {
        MySqlCommand cmd = connection.CreateCommand();
        cmd.CommandText = "Select * from stockcard";
        MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0].DefaultView;

    }
    catch (Exception e)
    {
        throw;
    }
    finally
    {
        if(connection.State == ConnectionState.Open)
        {
            connection.Clone();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试SslMode=None;。如果您可以提供抛出的异常,将很有用。

确定您指定了在服务器上运行的MySql实例的端口吗?它是否在默认端口3306上运行?