I want to protect my app from SQL injection. I want to use OleDbParameter in a SQL query for the table name ({1}).
The problem is that it doesn't work (error in FROM or something like that). I can pass the OleDbParameter in {3} thought. Example:
IDbCommand cmd = m_oConnection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = String.Format("SELECT {0} FROM {1} WHERE {2}={3}",
"ParentID",
"?",
sWhere,
"?"
);
cmd.Parameters.Add(new OleDbParameter("@sTable", sTable));
cmd.Parameters.Add(new OleDbParameter("@id", id));
What can I do? Am I forced to write a function which escapes some SQL characters by hand? If yes, where can I find a perfect function?
Thanks