Stats

605 Contributors: 9 Monday, July 25, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap
Download eBook

Basic Querying

Download dapper eBook

Syntax

  • public static IEnumerable<T> Query<T>(this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)
  • public static IEnumerable<dynamic> Query (this IDbConnection cnn, string sql, object param = null, SqlTransaction transaction = null, bool buffered = true)

Parameters

ParameterDetails
cnnYour database connection, which must already be open.
sqlCommand to execute.
paramObject to extract parameters from.
transactionTransaction which this query is a part of, if any.
bufferedWhether or not to buffer reading the results of the query. This is an optional parameter with the default being true. When buffered is true, the results are buffered into a List<T> and then returned as an IEnumerable<T> that is safe for multiple enumeration. When buffered is false, the sql connection is held open until you finish reading allowing you to process a single row at time in memory. Multiple enumerations will spawn additional connections to the database. While buffered false is highly efficient for reducing memory usage if you only maintain very small fragments of the records returned it has a sizeable performance overhead compared to eagerly materializing the result set. Lastly if you have numerous concurrent unbuffered sql connections you need to consider connection pool starvation causing requests to block until connections become available.

Related Examples