2014-02-14 84 views
0

我有一個奇怪的MySQL連接問題。我的環境是MySQL連接在調試模式下失敗

操作系統 - 微軟Windows家庭基本
IDE - 的SharpDevelop 4.3.3.9663

MySQL服務器 - 5.5
MySQL連接 - 6.8.3

我創建了一個示例程序連接到我的機器中的MySQL服務器。

using System; 
using MySql.Data.MySqlClient; 

namespace TestBed 
{ 
    class Program 
    { 
     private static MySql.Data.MySqlClient.MySqlConnection conn; 
     public static void Main(string[] args) 
     { 
      Console.WriteLine("Hello World!"); 

      connect(); 

      Console.Write("Press any key to continue . . . "); 
      Console.ReadKey(true); 
     } 

     public static void connect() 
     { 

      string myConnectionString; 

      //myConnectionString = "Server=localhost; Port=3306; Database=test; Uid=root; Pwd=Welcome01;"; //works fine in rel mode 
      myConnectionString = "Server=127.0.0.1; Port=3306; Database=test; Uid=root; Pwd=Welcome01;"; 

      try 
      { 
       conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString); 
       conn.Open(); 
       Console.WriteLine("opened"); 
      } 
      catch (MySql.Data.MySqlClient.MySqlException ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 
} 

我在開始時得到了下面的錯誤。

MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the specified MySQL hosts. 
    at System.Void MySql.Data.MySqlClient.NativeDriver.Open() 
    at System.Void MySql.Data.MySqlClient.Driver.Open() 
    at static Driver MySql.Data.MySqlClient.Driver.Create(MySql.Data.MySqlClient.MySqlConnectionStringBuilder settings) 
    at Driver MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() 
    at Driver MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() 
    at Driver MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() 
    at Driver MySql.Data.MySqlClient.MySqlPool.GetConnection() 
    at System.Void MySql.Data.MySqlClient.MySqlConnection.Open() 
    at static System.Void TestBed.Program.method() in ...\TestBed\Program.cs:line 36 
    at static System.Void TestBed.Program.Main(System.String[] args) in ...\Program.cs:line 19 

我從此鏈接(Cant connect to MySQL when using Debug mode)得到一個提示,並嘗試使用發佈模式。令人驚訝的是它能夠打開連接!

我分析了一下,發現了下面的東西。

  1. 只有當我使用Debug-> Step Over(使用F10鍵逐步調試)時,它纔會拋出錯誤!
  2. 如果我只是在調試模式下啓動應用程序,它會連接。
  3. 我可以在「connect()」方法的上下方分步跳過點
  4. 我也可以使用step(用F10鍵)調試應用程序的其他部分,但如果我使用step into this方法,它需要很長時間並拋出相同的錯誤。

我不確定自己是否做錯了什麼,或者它是MySQL或SharpDevelop的錯誤。有沒有人遇到過這種問題?如果可能的話,有人請點亮這個?

感謝, Ganesh神Periasamy

回答

0

試試這個..

"Persist Security Info=False;server=127.0.0.1;database=xx;uid=yy;password=zz" 
+0

你應該遵循一些標準格式:http://www.connectionstrings.com/mysql-connector-net-mysqlconnection/ – Kapil

+0

我試過「堅持......」但沒有運氣。一次更新 - 我卸載了MySql 5.5並安裝了Wamp,並使用mysql服務器進行連接。現在我得到 System.ArgumentOutOfRangeException:長度不能小於零。 在字符串System.String.InternalSubStringWithChecks(System.Int32的startIndex,System.Int32長度,System.Boolean fAlwaysCopy) .... 可悲:( –

+0

再次,如果我啓動程序爲「運行而不Degbugger(Ctrl + F5 )「在sharpdevelop,它工作正常! –