0

我收到错误:

无法建立与数据库的连接

当我连接 MySQL .NET 连接器时。我完全确定用户名正确,密码正确,数据库正确,服务器正确。这是代码:

mysqlCon.ConnectionString = "Server=(servername);Username=(username);Pwd=(rightpassword); 
Database=(rightdatabase)";
try
{
    mysqlCon.Open();
}
catch (Exception ex)
{
    MessageBox.Show("Could not establish a connection to database!");
}
4

3 回答 3

1

你不应该使用Uid而不是Username吗?至少connectionstrings.com是这么说的 :)
并且还要检查你的 MySQL 在哪个端口上运行。如果您不使用默认端口号 - ,则可能需要将端口号添加到连接字符串中 3306

于 2011-04-09T14:22:41.090 回答
1

我用:

public MySqlConnection NewConnection(string host, int port, string db, string user, string pwd)
{
    string cstr = String.Format(
        "SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}",
        host, port, db, user, pwdd);
    MySqlConnection conn = new MySqlConnection(cstr);
    try { conn.Open(); }
    catch (Exception ex)
    {
        conn = null;
    }
    return conn;
}
于 2011-04-09T14:27:24.547 回答
0

确保可以从运行代码的机器访问数据库。

使用远程登录

telnet <server> 3306

或 MySQL 命令行客户端

mysql -u <user> -h <server> -P <port> -p
于 2011-04-09T15:07:04.450 回答