2014-02-14 155 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,它工作正常! –