Bes Ley
Bes Ley

Reputation: 1797

How to convert Dapper DynamicParameter to DbParameter[]?

When using query method in Dapper.NET library, the funciton code is here:

public IEnumerable<dynamic> Query(IDbConnection conn, string sql, dynamic param = null, IDbTransaction trans = null, bool buffered = true)
{
    return SqlMapper.Query(conn, sql, param as object, trans, buffered);
}

Our customer use their own data access library, they dont use SqlMapper class in Dapper library. So they need to change param to DbParameter[]. There are many references with Query method, it is better to only change param object to DbParameter array, otherwise we have to modify every reference in our code.

Is there a good way to find a solution to fix this issue? Thanks.

Upvotes: 1

Views: 575

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062550

This is not an API that dapper exposes, because it never needs that. To do what you want, you could borrow liberally from the dapper source code to get what you need, but also consider: you cannot create a DbParameter in isolation - it is an abstract type. You need to either know in advance what type you actually want, or ask the connection to create the parameters for you.

Upvotes: 1

Related Questions