Remote MySql Access denied for user with C# code b

2019-08-02 15:14发布

I can connect to local MySql server in my C#/.Net winform app but when I try to connect to a remote MySql server. I got a "Access denied for user @'%' to database" error message. However, if the remote server is denying the connection. How come I can connect to the remote MySql database with DbVisualizer? I'm pretty sure the problem is not with code but rather the server settings.

2条回答
做自己的国王
2楼-- · 2019-08-02 15:30

compare this code with yours.

private void button1_Click(object sender, System.EventArgs e)
{
        string MyConString = "SERVER=localhost;" +
            "DATABASE=mydatabase;" +
            "UID=testuser;" +
            "PASSWORD=testpassword;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand command = connection.CreateCommand();
        MySqlDataReader Reader;
        command.CommandText = "select * from mycustomers";
        connection.Open();
        Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            string thisrow = "";
            for (int i= 0;i<Reader.FieldCount;i++)
                    thisrow+=Reader.GetValue(i).ToString() + ",";
            listBox1.Items.Add(thisrow);
        }
        connection.Close();
}
查看更多
做个烂人
3楼-- · 2019-08-02 15:36

I had the same problem with Java. Solution to my problem was not specifying the catalog. I'm not sure if this is the case with C#, give it a try.

查看更多
登录 后发表回答