16

I have a web application that uses old school SqlHelper class.

I want to create my custom SqlHelper which uses Dapper underneath. So, how can I get SqlDataReader from Dapper?

2
  • I'm not dapper expert, but dapper is created to work on object layer(it's Micro ORM) and not worried about usual ado.net stuff including SqlDataReader. Commented Sep 21, 2014 at 7:59
  • 1
    @reptildarat, I know. But I need this for a reason. Commented Sep 21, 2014 at 8:04

1 Answer 1

27

There is an ExecuteReader method that hands you back the data-reader that the connection generated: you can cast this if you know it is actually a SqlDataReader. In this scenario, dapper only processes parameters and literal injection.

using(var reader = (DbDataReader)
    conn.ExecuteReader(sql, args))
{
    // use reader here
}

I am, however, more than a little intrigued as to what you want SqlHelper to do that dapper doesn't already do (but better). Genuine question: I like improving the library. If there is a gap, let me know.

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks. Is there is ExecuteReaderAsync as well?
@user yes - in the .NET 4.5 build
@user I am familiar with it; I was curious if I'd simply omitted some huge feature set. It doesn't sound like it...
@user960567 the way to do that would be to write a class that implements ICustomParameters or ICustomParameter - and wrap that. It should work fine. I'm not at a PC, but let me know if you need an example adding later. Dapper works fine with TVPs.
@MarcGravell this broke in Dapper 2.0.4, in 1.60.6 you could cast it to a SqlDataReader, now it fails. :( Need to use DbDataReader
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.