Do not use string parsing or regular expressions to handle this kind of thing. The SQL syntax is too complex for reliable parsing with regular expressions.
Instead, use parameterized queries with placeholders and avoid string concatenation. This will defeat the SQL injection at its root.
var command = new SqlCommand(connection); command.Text = "INSERT INTO foo (a, b, c) VALUES (@a, @b, @c)"; command.Parameters.AddWithValue("a", "this is invulnerable"); command.Parameters.AddWithValue("b", "to any sort of SQL injection"); command.Parameters.AddWithValue("c", "
source share