打开连接时MySQL连接器超时

时间:2014-06-03 17:31:44

标签: c# mysql mysql-connector

我有以下课程:

public class MySqlConnectionManager
{
    public MySqlConnection Connection { get; set; }

    public void OpenConnection()
    {
        Connection = new MySqlConnection(GetConnectionString());
        try
        {
            Connection.Open();
        }
        catch (MySqlException exc)
        {
            switch (exc.Number)
            {
                case 0:
                    throw new Exception("Cannot connect to MySQL-server. Please contact administrator.");
                case 1045:
                    throw new Exception("Invalid username/password to connect to MySQL-server. Please correct data.");
                default:
                    throw new Exception("Unknown MySQL-connection error. Code: " + exc.Number);
            }
        }
    }

    private string GetConnectionString()
    {
        string activeDatabaseServer;
        string activeDatabaseName;
        string activeDatabaseUsername;
        string activeDatabasePassword;

        #if DEBUG
            activeDatabaseServer = DebugDatabaseServer;
            activeDatabaseName = DebugDatabaseName;
            activeDatabaseUsername = DebugDatabaseUsername;
            activeDatabasePassword = DebugDatabasePassword;
        #else
            activeDatabaseServer = ReleaseDatabaseServer;
            activeDatabaseName = ReleaseDatabaseName;
            activeDatabaseUsername = ReleaseDatabaseUsername;
            activeDatabasePassword = ReleaseDatabasePassword;
        #endif

        string connectionString = "server=" + activeDatabaseServer + ";uid=" + activeDatabaseUsername + ";pwd=" +
                                  activeDatabasePassword + ";database=" + activeDatabaseName + ";";

        return connectionString;
    }

调用Connection.Open()时,我收到超时异常。将超时设置为120秒并没有帮助。我的调试数据库工作(它本地)。我在发布模式下只会遇到此异常。但我确定服务器,数据库,用户名和密码的名称是正确的,因为我使用来自另一个用php编写的应用程序中的相同数据库,其中一切正常。

密码包含特殊字符,例如'!','%',' *','。'或者' @'。这会造成任何麻烦吗?

1 个答案:

答案 0 :(得分:0)

我的代码在外部系统上运行。它无法从外部连接到1& 1托管的mysql数据库。 :(