Search code examples
ado.netdapper

How to get SqlDataReader with Dapper?


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?


Solution

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