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

Peta Poco: How to pass multiple parameters to a Fetch<T>

我的代码:

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......

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

我对petapoco不是很熟悉,但是从报错信息来看,好像是在尝试绑定参数到你对象的属性传入 Fetch 调用。如果是这样,请尝试这样的事情:

var results = db.Fetch<Person>(sql, new {t1 = t1, t2 = t2});

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 
        }
    );