RLH
RLH

Reputation: 15698

How do I use parameters with OrmLiteReadConnectionExtensions.Select(...) passing in a parameterized SQL statement?

I have a project that's using ServiceStack & ORMLite. I need to make use of the OrmLiteReadConnectionExtensions extension method List<T> Select<T>(this IDbConnection dbConn, string sqlFilter, params object[] filterParams).

The thing is, I can't find any documentation or references on how to use this method with a SQL select statement and parameters.

When I looked up the code in github, the following sample/hint was provided:

db.Select<Person>("SELECT * FROM Person WHERE Age > @age", new[] { db.CreateParam("age", 40) }) 

This would makes sense, but the problem is that my ORMLite c reated db (an IDbConnection object) doesn't have a db.CreateParam(...) method. How can I create and execute a parameterized select query for my ORMLite repository? To be clear, I am user Sql Server with this project.

Upvotes: 2

Views: 409

Answers (1)

mythz
mythz

Reputation: 143374

OrmLite does have the IDbConnection.CreateParam() extension method.

Have a look at the SelectParamTests for different examples for providing parameters in OrmLite:

db.Select<Person>(x => x.Age == 27)
db.Select(db.From<Person>().Where(x => x.Age == 27))
db.Select<Person>("Age = @age", new { age = 27 })
db.Select<Person>("Age = @age", new Dictionary<string, object> { { "age", 27 } })
db.Select<Person>("Age = @age", new[] { db.CreateParam("age", 27) })
db.Select<Person>("SELECT * FROM Person WHERE Age = @age", new { age = 27 })
db.Select<Person>("SELECT * FROM Person WHERE Age = @age", new Dictionary<string, object> { { "age", 27 } })
db.Select<Person>("SELECT * FROM Person WHERE Age = @age", new[] { db.CreateParam("age", 27) })

db.Select<Person>("Age = @age", new { age = 27 })

db.SelectNonDefaults(new Person { Age = 27 })

db.SqlList<Person>("SELECT * FROM Person WHERE Age = @age", new { age = 27 })
db.SqlList<Person>("SELECT * FROM Person WHERE Age = @age", new Dictionary<string, object> { { "age", 27 } })
db.SqlList<Person>("SELECT * FROM Person WHERE Age = @age", new[] { db.CreateParam("age", 27) })

Upvotes: 2

Related Questions