简体   繁体   English

使用C#无法连接到MySQL服务器,但使用Workbench则没有问题

[英]Fail to connect to MySQL server using C# but no issues using Workbench

I'm busy pulling out my hair. 我正忙着拔头发。 It feels like the solution should be obvious or/and many others had the same issue. 感觉解决方案应该很明显或/和许多其他问题都存在相同的问题。 But I cant find a solution (maybe the lack of asking the right question). 但是我找不到解决方案(也许缺少提出正确的问题)。

PROBLEM: I'm unable to connect with a MySQL server using C# code. 问题:我无法使用C#代码连接到MySQL服务器。 I dont have issues connecting to another server using the same code (different credentials obviously). 我没有使用相同代码(显然是不同的凭据)连接到另一台服务器的问题。 And I dont have issues creating a connection or server instance of the 'problem' Server using Workbench. 而且我没有使用Workbench创建“问题”服务器的连接或服务器实例的问题。 So it would suggest its not a port or rights issue on the server. 因此,这表明它不是服务器上的端口或权限问题。 Also, it cant be a code or MysqlConnector issue as there is no issues with the other server. 另外,它不能是代码或MysqlConnector问题,因为其他服务器没有问题。 error 1042 错误1042

CODE: 码:

MySqlConnection conn = null;
string newCnnStr = "server=Win10Server;User Id=root;password=LamePassword;Persist Security Info=True;database=payrolldb;convert zero datetime=true";
        try
        {
            conn = new MySqlConnection(newCnnStr);
            conn.Open();
            txtTmpStatus.Text = "Connected";
            return true;
        }
        catch (ArgumentException)
        {
            txtTmpStatus.Text = "Check the Server String";
        }
        catch (MySqlException ex)
        {
            switch (ex.Number)
            {
                case 1042:
                    txtTmpStatus.Text = "Unable to connect to the Server";
                    break;
                case 0:
                    txtTmpStatus.Text = "Access denied";
                    break;
                default:
                    break;
            }
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        return false;

I have also try adding the port 3306 to connection string - no change 我也尝试将端口3306添加到连接字符串-不变

Ok, so I replaced the IP address (which I should have tried hours ago) with the hostname and it work. 好的,所以我用主机名替换了IP地址(我应该在几个小时前尝试过),它可以正常工作。 So for some reason C# is not resolving the hostname. 因此,出于某些原因,C#无法解析主机名。 Suspect it could be related to the "-" in "Van-PC" as the other server does not have a "-" in its hostname. 怀疑它可能与“ Van-PC”中的“-”相关,因为另一台服务器的主机名中没有“-”。 And yes, I know in my code sample the name is different. 是的,我知道我的代码示例中的名称是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM