I expect there's a bug in EscFunction()
. You should remove that function from your code base completely; it's entirely the wrong way to approach the issue. It is not correct to sanitize your database inputs!
Rather, the only correct approach is to QUARANTINE your database inputs using parameterized queries, as demonstrated below:
string SQL = "
UPDATE Company_Table
SET Company_Table_Default_Storage= @DefaultStorage
WHERE ID= @ID";
using (var conn = new MySqlConnection(ra.conn_String1))
using (var cmd = new MySqlCommand(SQL, conn))
{
cmd.Parameters.AddWithValue("@DefaultStorage", DefaultStorage);
cmd.Parameters.AddWithValue("@ID", cp.id);
conn.Open();
cmd.ExecuteNonQuery();
} // No need to even call conn.Close(); The using block takes care of it.
This is different than "sanitizing", because the quarantined values are never merged back into the SQL command text, even on the server.
Also note the use of using
blocks to manage the connection lifetime.
Finally, I want to address this comment:
I have been trying to move to a more multi-platform environment so I have started moving my app to MySQL.
I'm getting outside the realms of the both the question scope and the fact-based verifiable-answers we like to write for Stack Exchange here, and more into an expression of my personal opinion on a specific technology. Nevertheless, I didn't want to leave that comment alone.
I do get wanting to support a broader set of database or hosting technologies, but you should know the MySql product spent many years (from ~2005 to ~2018) almost completely stagnant. It has fallen significantly behind other options and is the least standards compliant of the major products in the space. The good news is it seems to be progressing again, but right now if I needed to move an app to a new open source DB today I'd pick Postgresql instead, and it wouldn't be a close decision.
Of course, your use case may be different, but I think MySql has a reputation as the default option that no longer reflects that actual state of the technologies, and hasn't for some time now. At the same time, SQL Server is now perfectly happy to run in linux, meaning it's not required to switch to MySql or anything else in order to enable multiplatform hosting.