2012-08-08 177 views
0

重新連接到MySQL數據庫我可以成功登錄到數據庫與此:故障從C#

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 
+0

不確定,可能是連接字符串錯誤。嘗試使用'host = localhost; ...'而不是'server = localhost; ...'。 – Devart 2012-08-08 11:15:53

+0

@Devart看起來(根據VS調試器)何時進行賦值:connection.ConnectioString =(String)...它將「主機」更改爲「服務器」。此外,'server = localhost; ...'是MySqlConnectionStringBuilder反正給我的。 – nobody 2012-08-08 11:28:56

回答

0

添加堅持安全信息=真正的連接字符串,我認爲。

如果是我雖然我不會把連接字符串和密碼放在裏面。如果你曾經調用Save,它將會暴露在配置文件中。

+0

堅持安全信息=真正有效。謝謝。 – nobody 2012-08-13 09:07:16