带有Dapper和参数的非存储过程查询(CommandType = Text)

时间:2017-05-04 11:20:20

标签: c# dapper

我试图用Dapper中的参数调用一段SQL。它不是一个存储过程(我可以使用参数工作正常)

inputCustomerName = "Our Customer";
inputStationName  = Null;

    var parameters = new
    {
        customerName = inputCustomerName ,
        stationName = inputStationName    
    };     

    ...

    using (var dbConn = dataProvider.CreateConnection)
    {
        dbConn.ConnectionString = connectionString;
        dbConn.Open();
        returnValue = dbConn.Query<T>(sql: sql, commandType: commandType, param: parameters);
        dbConn.Close();
    }

SQL的相关部分是

"    ...

     WHERE 
        Customer = ISNULL(@customerName,Customer)
        AND Station = ISNULL(@stationName,Station)
";

我一直收到“DynamicMethod的无效类型所有者”。 (当使用DynamicParameters而不是同名对象时,我也得到了这个。)

SQL在数据库本身运行正常(假设我声明并且@populate @customerName和stationName)。

我怀疑我做了一件非常简单的错事 - 有人能开导我吗?

1 个答案:

答案 0 :(得分:0)

答案最终是代码中没有包含在问题中的问题 - 我对此深表歉意。

问题是T是一个接口,这就是导致Dapper爆炸的原因。可能还有一些东西意味着你只能使用类,而不是接口作为Query的类型参数。虽然我从未见过明确规定的限制。

羞耻,(但我可以想象为什么会有几个原因)

相关问题