This problem is weird and I'm not sure what exactly is causing it. The update query shown in the sample code below works 60% of the time but sometimes it doesn't work at all. There is no exception or error. I tried debugging and when the query doesn't work it simply returns 0 meaning that no rows were updated. However, the row exists and if I try it on phpmyadmin or run this code again it somehow works. I also tried using a simple UPDATE query instead of INSERT ON DUPLICATE KEY UPDATE but still getting the same behavior.
If anyone can suggest me a better approach it'd be very much appreciated.
MySqlConnection remoteConnection;
string connectionString = "SERVER=" + server + ";" + "DATABASE=" + db + ";" + "UID=" + username + ";" + "PASSWORD=" + pass + ";";
remoteConnection = new MySqlConnection(connectionString);
remoteConnection.Open();
string query = $"INSERT INTO WebServers (Ip,Status,Error,Visible) VALUES('{serverStatus.Ip}', '{serverStatus.Status}', '{serverStatus.Error}','0') ON DUPLICATE KEY UPDATE Status='{serverStatus.Status}', Error='{serverStatus.Error}', Visible='0'";
MySqlCommand mySqlCommand = new MySqlCommand(query, remoteConnection);
int i = mySqlCommand.ExecuteNonQuery();
remoteConnection.Close();