mysql中的连接错误太多

时间:2013-09-16 10:13:09

标签: c# mysql

我有一个连接mysql数据库服务器的C#桌面应用程序。 应用程序运行良好,但经过一段时间或某些天后,它会出现例外情况:

9/16/2013 12:56:55 PM: 9/16/2013 12:56:55 PM: Exception Occurred. Too many    
connections.StackTrace:    at MySql.Data.MySqlClient.MySqlStream.OpenPacket()
at MySql.Data.MySqlClient.NativeDriver.Authenticate411()
at MySql.Data.MySqlClient.NativeDriver.Authenticate()
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at ReckonHelper.MySqlDatabaseConnection.ConnectDB().

我不知道为什么会这样?

任何解决方案吗?

1 个答案:

答案 0 :(得分:0)

  1. 在编译完SQL语句后关闭连接。

  2. 使用try..catch..finally

     try
     {
       ....... 
       con.Open();
       //execute sql statement
    
      }
      catch (Exception ex)
      {
            throw ex;
            //throw or dispaly your exception
      }
     finally
     {
       con.close;
      }
    
  3. 在连接字符串中添加池属性
            “Server = servername; Port = 3306; Database = dbname; Uid = username; Pwd = password; pooling = true; Allow Zero Datetime = True; Min Pool Size = 5; Max Pool Size = 100;”