1

I have written some C# to update a MySql table but I get an exception every time I call the method ExecuteNonQuery(). I have researched this on the web and every solution I find produces the same error. I have an open connection to the database and the update query to the database is written correctly. The code that I have so far come up with is :

public int executeUpdate()
{
  int result = 0;
  if (isConnected)
  {
    try
    {
      MySqlConnection cn = new  MySqlConnection(connection.ConnectionString);
      MySqlCommand cmd = new MySqlCommand();

      cmd.Connection = cn; 
      cmd.CommandText = "UPDATE test SET status_id = 1 WHERE test_id = 1";
      int numRowsUpdated = cmd.ExecuteNonQuery(); 
    }
    catch (MySqlException exSql)
    {
      Console.Error.WriteLine("Error - SafeMySql: SQL Exception: " + query);
      Console.Error.WriteLine(exSql.StackTrace);
    }
    catch (Exception ex)
    {
      Console.Error.WriteLine("Error - SafeMySql: Exception: " + query);
      Console.Error.WriteLine(ex.StackTrace);
    }
  }
  else
    Console.Error.WriteLine("Error - SafeMySql: executeQuery failed. Not connected to DB");
}
0

2 Answers 2

4

Change your try section to the code below:

try
{
    using(MySqlConnection cn = new  MySqlConnection(connection.ConnectionString))
    {        
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = cn; 
        cmd.CommandText = "UPDATE test SET status_id = 1 WHERE test_id = 1";
        cn.Open();
        int numRowsUpdated = cmd.ExecuteNonQuery();
        cmd.Dispose(); 
     }
}

The connection must be opened before you execute a command. In the example above the command object will immediately be disposed and the connection object will implcitly be closed and disposed when you leave the using section.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for posts guys its much apprciated. I have changed my code to what Espen Burud suggested and the code still throws an exception the exception is: '{"Access denied for user 'root'@'localhost' (using password: NO)"}' . This exception is thrown when I pass over the cn.Open(); line. Again I have had a good play and look on the web but can't find a solution. Any help would be much appreciated thanks
It's many situations that can cause access denied errors, so I think this dev.mysql.com/doc/refman/5.1/en/access-denied.html page might help.
@user498213 This question isnt about access problems. You should pick out an answer according to your question.
3

I don't see the connection being opened.

Here is an example from MSDN: even inside a using block, they open the connection explicitly

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

Edit: The principle is the same for MySQL as it is for SQL Server:

public void CreateMySqlCommand(string myExecuteQuery, MySqlConnection myConnection) 
{
  MySqlCommand myCommand = new MySqlCommand(myExecuteQuery, myConnection);
  myCommand.Connection.Open();
  myCommand.ExecuteNonQuery();
  myConnection.Close();
}

1 Comment

It is mysql instead of MSSQL.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.