Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I am experimenting with mySQL for use with a c# application. I have come across a problem where connections that are closed aren't closing properly. I did some googling and found there is an option in the connecting string where I can set pooling to false, which does close the connection properly. However if I try this with multiple connections, the application crashes. I came across this article;

http://www.primaryobjects.com/CMS/Article69.aspx

which describes my problem better and has a method of killing the process for each connection, however it will not work for an active connection I just opened. Does anyone know a workaround for this problem or something really obvious that I'm missing.

This is the code I am using:

using (MySqlConnection connection = new MySqlConnection())
                {
                    MySqlDataAdapter data = new MySqlDataAdapter();
                    connection.ConnectionString = "server=127.0.0.1;" +
                        "database=database;" +
                        "uid=root;" +
                        "password=password;";

                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "select * from T_SCHEDULE";
                    MySqlCommandBuilder thisBuilder = new MySqlCommandBuilder(data);
                    data.SelectCommand = command;
                    DataSet dataset = new DataSet();
                    data.Fill(dataset, "T_SCHEDULE");

                    DataRow thisRow = dataset.Tables[0].NewRow();
                    thisRow[1] = "4000";
                    thisRow[2] = DateTime.Now;
                    thisRow[3] = 12;
                    dataset.Tables["T_SCHEDULE"].Rows.Add(thisRow);

                    DataRow thisRow2 = dataset.Tables[0].NewRow();
                    thisRow2[1] = "4001";
                    thisRow2[2] = DateTime.Now;
                    thisRow2[3] = 12;
                    dataset.Tables["T_SCHEDULE"].Rows.Add(thisRow2);

                    data.Update(dataset, "T_SCHEDULE");

                    connection.Close();
                    connection.Dispose();
                }
Posted
Updated 24-Aug-11 0:33am
v3

1 solution

Try Disposing your connections (and your commands, etc.) that may well dispose of your problem!
The easiest way is to use a using block:
C#
using (MySqlConnection con = new MySqlConnection(strCon))
    {
    con.Open();
    using (MySqlCommand ver = new MySqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
        {
        ver.Parameters.AddWithValue("@FN", filename);
        object o = ver.ExecuteScalar();
        if (o != null && o != System.DBNull.Value)
            {
            // Exists already.
            version = (int) o + 1;
            }
        }
    using (MySqlCommand ins = new MySqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version) " +
                                           "VALUES (@ID, @FN, @DS, @DT, @VS)", con))
        {
        ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
        ins.Parameters.AddWithValue("@FN", filename);
        ins.Parameters.AddWithValue("@DS", "");
        ins.Parameters.AddWithValue("@DT", filedata);
        ins.Parameters.AddWithValue("@VS", version);
        ins.ExecuteNonQuery();
        }
    }



Won't help - it comes direct from my Web.Config (redacted version):
XML
<connectionstrings>
  <add name="LoginDatabase" connectionstring="Database=mydatabase;Data Source=localhost;User Id=XXXX;Password=XXXX"></add>
  <add name="LocalMySqlServer" connectionstring="server=localhost;database=XXXX;uid=XXXX;pwd=XXXX" providername="MySql.Data.MySqlClient"></add>
</connectionstrings>
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 24-Aug-11 6:29am    
Very good suggestion +5
Neil Cross 24-Aug-11 6:31am    
I tried disposing my connection but that does not work. If this is definitely working for you, could you show me your connection string please. That may affect how it disposes the connection. Thank you for your help.
OriginalGriff 24-Aug-11 6:41am    
Edited: moved to answer so the damn XML / HTML tags stuff won't get hidden! - OriginalGriff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900