0

我可以通过以下方式成功登录数据库:

MySqlConnection conn = new MySqlConnection();
MySqlConnectionStringBuilder connString = new MySqlConnectionStringBuilder();

connString.Server = textEditServer.Text;
connString.UserID = "root";
connString.Password = textEditServerPassword.Text;
connString.Database = "geysercontrol";
conn.ConnectionString = connString.ConnectionString;

try
{
     conn.Open();
     Properties.Settings.Default.Properties["ConnectionString"].DefaultValue = conn.ConnectionString;
     conn.Close();
}
catch (MySqlException)
{
      XtraMessageBox.Show("No connection could be established");
}

但是当我尝试使用 ConnectionString 属性重新连接不同的类时,我得到一个 MySQLException 说

Access denied for user 'root'@'localhost' (using password: NO)

这可能是什么原因?MySQL网站上关于可能原因的页面不包括我的情况。

我用来重新连接的代码是:

 connection = new MySqlConnection();
 connection.ConnectionString = (String)Properties.Settings.Default.Properties["ConnectionString"].DefaultValue;
 connection.Open();

在这两种情况下,connectionString 肯定是相同的。这是:

server=localhost;User Id=root;database=geysercontrol;password=password
4

1 回答 1

0

Add persist security info = true to the connection string I think.

If it were me though I wouldn't put connection string with a password in it in there. If you ever call Save, it will be exposed in the config file.

于 2012-08-08T11:35:31.680 回答