I am working on an application where certain SQL Server queries will be limited to specific users based on a group identity.
It seems to make sense to store these within the application database where I can associate them with group names and it is easy to add/remove in future.
The queries are quite complex though and take a number of parameters such as JQuery to and from date fields, current username etc. So the query is constructed like:
MyQuery = "SELECT * FROM Table1 WHERE Username = '" + System...CurrentUser +"' AND Somedate > '" + from.text +'";
Now I'm unsure how to take this code and create an equivalent representation in the database. I thought about using specific identifiers such as %USERNAME%, %FROMDATE% etc then use a String Replace function but I'm not sure that would carry over the single quotes.
Would something like Replace("%USERNAME%", "'" + CurrentUser + "'") work?
Any better ideas!
Two things are:
- Restrict some queries based on user groups.
- Reasonably easy to add/remove/edit queries in the future (not hardcoded into the C#?)