In some C# code, table names passed in via an enum are being inserted into SQL queries using string.Format
like so:
const string ADD_SQL = "INSERT INTO {0} (ColumnOne) VALUES (@valueOne)";
const string CLEAR_SQL = "DELETE FROM {0}";
var commandText = string.Format(ADD_SQL , _tableName);
But when I run the Veracode tool it shows this query has possibility of SQL injection when executing.
command.ExecuteNonQuery();
I want to avoid the possibility of SQL injection with the above code. I tried adding a tag (@tablename
), but that did not work.
const string ADD_SQL = "INSERT INTO @tablename (Data) VALUES (@valueOne)";
var commandText = ADD_MESSAGE_SQL.Replace("@tablename", _tableName);
How do I avoid this?