简体   繁体   English

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

[英]Non-Stored Procedure Query (CommandType=Text) with Dapper and parameters

I am trying to call a piece of SQL with parameters in Dapper. 我试图在Dapper中调用带有参数的SQL。 It is NOT a stored procedure (I have that working fine with parameters) 它不是存储过程(我可以很好地使用参数)

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

The relevant part of the SQL is SQL的相关部分是

"    ...

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

I keep getting "Invalid type owner for DynamicMethod". 我不断收到“ DynamicMethod的无效类型所有者”。 (I got this also when using DynamicParameters instead of the anomymous object). (我在使用DynamicParameters而不是anommous对象时也得到了此信息)。

The SQL runs fine on the database itself (given I declare and @populate @customerName and stationName). SQL在数据库本身上运行良好(假设我声明并@populate @customerName和stationName)。

I suspect I have done something quite simple wrong - can anyone enlighten me? 我怀疑我做了一些很简单的错误-有人可以启发我吗?

The answer was, in the end, an issue in code not included in the question - for which I thoroughly apologise. 最终,答案是问题中未包含的代码问题-我对此深表歉意。

The issue was that T was an Interface, and that was what was causing Dapper to blow up. 问题是T是一个接口,这就是导致Dapper崩溃的原因。 There's probably something under the hood that means you can only use classes, not interfaces as the type param to Query. 可能有些暗示,您只能使用类,而不能将接口用作Query的类型参数。 Though I have never seen that restriction explicitly stated. 尽管我从未见过明确指出该限制。

Shame, (but I can imagine a few reasons why) 丢脸,(但我可以想象出几个原因)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM