Friday 15 August 2014

C# Too many connections in MySQL -


I try to run MySQL on a table at SELECT And I get this error:

  Server error in the 'MyApp' application too much Connection details: An uncontrolled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where the code has originated from it. Exception Description: MySql.Data.MySqlClient.MySqlException: Too many connection source error: An unrestricted exception occurred during the execution of the current web request. The information about the origin and location of the exception can be identified using the exception stack trace below. Stack trace: [MySqlException (0x80004005): Too many connections] MySql.Data.MySqlClient.MySqlStream.ReadPacket () +517 MySql.Data.MySqlClient.NativeDriver.Open () +702 MySql.Data.MySqlClient.Driver.Open () +245 MySql.Data.MySqlClient.Driver.Create (MySqlConnectionStringBuilder Settings) +297 MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection () +18 MySql.Data.MySqlClient.MySqlPool.GetPooledConnection () +403 MySql.Data.MySqlClient .MySqlPool .TryToGetDriver () +228 MySql.Data.MySqlClient.MySqlPool.GetConnection () 106 MySql.Data.MySqlClient.MySqlConnection.Open () 1468 version information: Microsoft .NET Framework version: 4.0.30319; ASP.NET version: 4.0.30319.1   

I run it on IIS with .NET and C #. Any idea how I can fix this problem

This is how I select by:

MySqlDataReader MSDR;

  MySqlConnection Connect = New MySqlConnection (connectionStringMySql); MySqlCommand CMD = New MySqlCommand (); String commandline = "select from the table * where active = 1; command line = commandLine.Remove (commandLine.Length - 3); cmd.CommandText = commandline; cmd.Connection = connect; cmd.Connection.Open (); MSDR = Cmd.executeReader (); while (msdr.Read ()) {// read data} msdr.Close (); cmd.Connection.Close ();   

How does this remove me :

  MySqlConnection = new MySqlConnection (connectionStringMySql) connect cmd.Connection = connected; cmd.Connection.Open (); string Commands = @ "Where the table Delete id = @ id, "; cmd.CommandText = command line; cmd.Parameters.AddWithValue (" @ ID ", SlotId); cmd.ExecuteNonQuery (); cmd.Connection.Close ();   

This is how I insert:

  MySqlConnection connect = new MySqlConnection (connectionStringMySql); MySqlCommand cmd = new MySqlCommand (); cmd.Connection = connected; cmd.Connection.Open (); string Commands = @ "table (including ID, day of the week, start, end) values" + " (@ Id, @ wake day, @ a art, @ and); "; Cmd.CommandText = commandLine; Cmd.Parameters.AddWithValue (" @ id ", id); Cmd.Parameters.AddWithValue (" @ Days ", item.weekday); Cmd.Parameters.AddWithValue (" @ Start ", the new Taimspen (item.starthour, item.startmin, 0)); Cmd.Parameters.AddWithValue ( "@ end", new Taimspen (item.endhour, item.endmin, 0)); cmd.ExecuteNonQuery (); long id = CMD .listInsteadID; cmd.Connection.Close (); Return ID;    

All examples above The same weakness shows that you do not use it so that it will ensure that the connection is closed and the connection to the connection Knowledge and other disposable objects If one or more of your statements throw exceptions, the code-closing code is not executed and you can end up with too many connection errors

for example

  string Commands = "Select the table active where 1 =" Commands = Commands from. Remove (Commands. crossed - 3); (using MySqlConnection connect = new MySqlConnection (ConnectionStringMySql)) (MySqlCommand CMD = New MySqlCommand (command Ain, connect)) {connect.Open (); (Using SqlDataReader msdr = cmd.ExecuteReader ()) {while (msdr.Read ()) {// read data}}} // Here the connection will be closed and the settlement will be done. (And also commands)    

No comments:

Post a Comment