1

I created a simple class for using MySQL like this:

public class DatabaseConnect
{
    public DatabaseConnect()
    {
    }

    public object Query(string s)
    {
        try
        {
        string connectionString = "SERVER=servername;DATABASE=dbname;UID=user;PASSWORD=password;";
        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            connection.Open();
            using (MySqlCommand cmd = new MySqlCommand(s, connection))
            {
                return cmd.ExecuteScalar();
            }
        }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            return false;
        }
    }
}

But sometimes when I call Query() it fails and throws the error specified in the title. I have no idea why it is happening.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Ladas125
  • 265
  • 3
  • 14
  • 1
    I can't see how this would happen from this code, since you are correctly / explicitly opening and closing / disposing the connection each call. What might be happening is that a connection elsewhere is being returned to the connection pool with an open reader. Try adding `pooling=false` to the end of your connection string? – StuartLC May 02 '15 at 06:05
  • So far it looks like no more datareader error. Thanks! – Ladas125 May 02 '15 at 08:58
  • Obviously without pooling, connections are more expensive to obtain. [A similar issue here](http://stackoverflow.com/questions/25128321/there-is-already-an-open-datareader-even-though-it-is-not), although this more obviously caused by returning a connection with an open reader to the pool. I can't seem to find any report as to whether this is a systemic issue with connection pooling on .Net MySql driver. – StuartLC May 02 '15 at 09:06
  • Is this the only query within the application? – Caramiriel May 02 '15 at 14:58

0 Answers0