Using Database.Query has has made a huge improvement on readability in my code:
String Select = 'sp_getAllUsers';
WebMatrix.Data.Database DB = WebMatrix.Data.Database.Open(MyConString);
var data = DB.Query(Select);
I'd like to switch to a non-stored procedure query. The MSDN says there's an optional parameter to the Query Method, Object[]
, that can be passed as SQL parameters, however they don't have any further information about it.
So I have two questions:
- How can I create a
Object[]
? - Will adding parameters in this way prevent hacking threats, such as SQL Injection?
Here's an abridged version of what I have tried:
Select = "Select * From Users Where first_name = "Foo" AND last_name = "Bar"
// Like Javascript
Object[] params = {"first_name" : "Foo"}, {"last_name" : "Bar"};
// More Like What I think it will be
Object[] Params = (String Name = "first_name", String First_Name = "Foo");
var data = DB.Query(Select, params);
All the sources I've looked at only seem to reference the old way. This is close, but he doesn't use the parameter parameter of the Query method.