2011-04-09 99 views
0

我收到提示:连接带MySQL.NET连接器

Could not establish a connection to database

当我与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!"); 
} 
+0

它migh TBE有用的打印ex.Message不是忽略它。如果您没有使用捕获的异常,请不要指定名称(例如,使用_catch(Exception)_ – NKCSS 2011-04-09 14:33:40

回答

1

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

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; 
} 
0

确保数据库是你正在运行的代码从机器访问。

使用Telnet

telnet <server> 3306 

或MySQL命令行客户端

mysql -u <user> -h <server> -P <port> -p