I want to provide the table name for a query as command parameters, like so:
public class Foo
{
private const String myTableName = "mytable";
public void Bar()
{
NpgsqlCommand command = new NpgsqlCommand("SELECT * from :tableName", connection);
command.Parameters.Add(new NpgsqlParameter("tableName", DbType.String));
command.Parameters[0].Value = myTableName;
}
}
This seems to result in this query: "SELECT * from E'mytable'"
which results in an error (mind the single quotes).
Do I really need to do string concatenation for this? It doesn't matter from a security standpoint, since the table name can not be changed by the user but string concatenation for creating SQL queries always gives me the creeps...
Thanks, Eric