Peta Poco:如何将多个参数传递给Fetch <t>

时间:2015-12-11 19:58:15

标签: c# .net-4.5 petapoco

我的代码:

string sql = "SELECT * FROM people where birthday >= @t1 AND birthday <= @t2"
DateTime t1 = DateTime.Parse("01-01-2000");
DateTime t2 = DateTime.Parse("01-01-2001");
var results = db.Fetch<Person>(sql, t1,t2);

此代码产生错误:

additional information: Parameter '@t1' specified but none of the passed arguments  have a property with this name (in 'SELECT......

我想知道正确的语法是什么?

2 个答案:

答案 0 :(得分:6)

我对petapoco并不熟悉,但是通过错误消息,它似乎试图将参数绑定到传递给{{1的对象的属性调用。如果是这样,请尝试这样的事情:

Fetch

答案 1 :(得分:2)

documentation并不过分直接,但对于位置参数,您需要在查询中使用零索引占位符名称@0@1 ....尝试:

string sql = "SELECT * FROM people where birthday >= @0 AND birthday <= @1"

如果使用命名占位符,PetaPoco会查找具有该属性名称的对象,因此会显示您收到的错误消息。例如:

sql.Append("AND date_created>=@start AND date_created<=@end", 
    new 
        { 
            start=DateTime.UtcNow.AddDays(-2), 
            end=DateTime.UtcNow 
        }
    );