Dapper - Execute Reader

Description

The ExecuteReader is an extension method that can be called from any object of type IDbConnection. This is typically used when the results of a query are not processed by Dapper. For example, to fill a DataTable or DataSet.

  1. using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
  2. {
  3. var reader = connection.ExecuteReader("SELECT * FROM Customers;");
  4. DataTable table = new DataTable();
  5. table.Load(reader);
  6. FiddleHelper.WriteTable(table);
  7. }

Try it: .NET Core | .NET Framework

Parameters

The following table shows the different parameters of an ExecuteReader method.

NameDescription
sqlThe command text to execute.
paramThe command parameters (default = null).
transactionThe transaction to use (default = null).
commandTimeoutThe command timeout (default = null)
commandTypeThe command type (default = null)

Example - Stored Procedure

The ExecuteReader extension method can also execute a stored procedure.

  1. using(var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
  2. {
  3. var reader = connection.ExecuteReader("SelectAllCustomers @IsActive = @isActive", new {IsActive = true} );
  4. DataTable table = new DataTable();
  5. table.Load(reader);
  6. FiddleHelper.WriteTable(table);
  7. }

Try it: .NET Core | .NET Framework