C#-无法连接到任何指定的MySQL主机

C#-无法连接到任何指定的MySQL主机,c#,mysql,login,C#,Mysql,Login,我正在用C#和MySQL创建一个系统,但我一直遇到这个令人沮丧的错误,“无法连接到任何指定的MySQL主机”。我试过很多不同的方法,但都没有效果,有人能帮我吗 public bool tryLogin(string username, string password) { MySqlConnection con = new MySqlConnection("host=hostremoved;user=user_removed;password=passwordremove

我正在用C#和MySQL创建一个系统,但我一直遇到这个令人沮丧的错误,“无法连接到任何指定的MySQL主机”。我试过很多不同的方法,但都没有效果,有人能帮我吗

public bool tryLogin(string username, string password)
    {
        MySqlConnection con = new MySqlConnection("host=hostremoved;user=user_removed;password=passwordremoved;database=databaseremoved;");
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM login WHERE user_name = '" + username + "' AND user_pass = '" + password + "';");
        cmd.Connection = con;
        con.Open();
        MySqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read() != false)
        {

            if (reader.IsDBNull(0) == true)
            {
                cmd.Connection.Close();
                reader.Dispose();
                cmd.Dispose();
                return false;
            }
            else
            {
                cmd.Connection.Close();
                reader.Dispose();
                cmd.Dispose();
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    private void btnlogin_Click(object sender, EventArgs e)
    {
        if (tryLogin(txtuser.Text, txtpass.Text) == true)
        {
            MessageBox.Show("Login worked!");
        }
        else
        {
            MessageBox.Show("Login failed!");

这是防火墙问题吗?检查运行MySql实例的计算机是否能够通过防火墙接受连接。

如果不是防火墙,并且数据库设置为侦听远程连接,则应检查用户凭据是否已配置,以便指定的数据库将接受来自给定IP的连接。如果用户只能从localhost进行连接,则需要以root用户身份登录并发出GRANT语句,如:

GRANT ALL ON yourDbName.* TO 'yourUser'@'yourIP' IDENTIFIED BY "yourPassword";
有关使用GRANT的更多信息,请参阅:


谢谢大家的帮助!我不得不在我的防火墙上把我的IP列入白名单,它成功了

不要将密码存储在纯文本中您有SQL注入漏洞。好的,我会修复这两个问题,但您知道如何帮助解决我需要帮助的实际问题吗?可能是,但我不确定如何访问防火墙设置,因为服务器不是由我自己的计算机托管的。有没有办法通过PhpMyAdmin或其他什么来实现这一点?好吧,没关系,我已经把我的IP列入了白名单,但它仍然不起作用,有什么建议吗?这个数据库是由第三方公司托管的还是由你控制的?您的数据库是否配置为仅侦听本地端口?