gpt4 book ai didi

c# - MySQL 连接器打开连接时超时

转载 作者:行者123 更新时间:2023-11-30 00:11:21 24 4
gpt4 key购买 nike

我有以下类(class):

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 秒并没有帮助。我的调试数据库可以工作(它在本地)。我仅在 Release模式下遇到此异常。但我确信服务器名称、数据库、用户名和密码都是正确的,因为我使用的是另一个用 php 编写的应用程序中的相同数据库,一切都工作正常。

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

最佳答案

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

关于c# - MySQL 连接器打开连接时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24021459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com