update a mySQL table using C#

前端 未结 2 1700
粉色の甜心
粉色の甜心 2021-01-24 04:29

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 fin

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 05:07

    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();
    }
    

提交回复
热议问题