ExecuteReader
This method executes a raw SQL statement directly against the database and returns a DbDataReader object. It supports all RDBMS data providers.
Code Snippets
The following example reads all rows from the [dbo].[Person] table.
using (var connection = new SqlConnection(connectionString))
{
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person];"))
{
// Process the reader here
}
}
Passing of Parameters
Parameters can be passed via any of the following types:
- IDbDataParameter
- Anonymous Types
- ExpandoObject
- Dictionary<string, object>
- QueryField/QueryGroup
IDbDataParameter
using (var connection = new SqlConnection(connectionString))
{
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person] WHERE Id = @Id;", new { Id = new SqlParameter("_", 10045) }))
{
// Process the reader here
}
}
The parameter name is not required. The library replaces it with the actual property name from the object.
Anonymous Types
using (var connection = new SqlConnection(connectionString))
{
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person] WHERE Id = @Id;", new { Id = 10045 }))
{
// Process the reader here
}
}
ExpandoObject
using (var connection = new SqlConnection(connectionString))
{
var param = new ExpandoObject() as IDictionary<string, object>;
param.Add("Id", 10045);
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person] WHERE Id = @Id;", param))
{
// Process the reader here
}
}
Dictionary<string, object>
using (var connection = new SqlConnection(connectionString))
{
var param = new Dictionary<string, object>
{
{ "Id", 10045 }
};
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person] WHERE Id = @Id;", param))
{
// Process the reader here
}
}
QueryField/QueryGroup
using (var connection = new SqlConnection(connectionString))
{
var param = new []
{
new QueryField("Id", 10045)
};
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person] WHERE Id = @Id;", param))
{
// Process the reader here
}
}
Or via QueryGroup.
using (var connection = new SqlConnection(connectionString))
{
var param = new QueryGroup(new []
{
new QueryField("Id", 10045)
});
using (var reader = connection.ExecuteReader("SELECT * FROM [dbo].[Person] WHERE Id = @Id;", param))
{
// Process the reader here
}
}
Array Parameters (for the IN keyword)
Pass an array of values when using the IN keyword.
using (var connection = new SqlConnection(connectionString))
{
var param = new
{
Keys = new [] { 10045, 10102, 11004 }
};
using (var reader = connection.ExecuteReader("SELECT * FROM dbo].[Person] WHERE Id IN (@Keys);", param))
{
// Process the reader here
}
}
Any of the parameter types listed in Passing of Parameters can also be used here.
Executing a Stored Procedure
There are two ways to execute a stored procedure. Pass the stored procedure name and set the command type to CommandType.StoredProcedure:
using (var connection = new SqlConnection(connectionString))
{
using (var reader = connection.ExecuteReader("[dbo].[sp_GetPerson]", new { Id = 10045 }, commandType: CommandType.StoredProcedure))
{
// Process the reader here
}
}
Or use a native SQL EXEC call:
using (var connection = new SqlConnection(connectionString))
{
using (var reader = connection.ExecuteReader("EXEC [dbo].[sp_GetPerson](@Id);", new { Id = 10045 }))
{
// Process the reader here
}
}
In the second call, the command text ends with a semicolon and no command type is set.