Class ExpressiveDbCommand
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
A deferred, fluent command for running SQL through any DbConnection, DbTransaction, connection factory, or connection pool. Unlike ExpressiveCommand, it exposes the DbCommand and DbDataReader types to the callbacks, so the provider-agnostic asynchronous ADO.NET APIs are available without casting.
public class ExpressiveDbCommand : ExpressiveDbCommandBase<DbConnection, DbCommand, DbDataReader, DbType, ExpressiveDbCommand>, IExecuteCommand<DbCommand>, IExecuteCommand, IExecuteReaderAsync<DbDataReader>, IExecuteReaderAsync, IExecuteReader<DbDataReader>, IExecuteReader
- Inheritance
-
ExpressiveDbCommand
- Implements
- Inherited Members
- Extension Methods
Examples
List<Customer> customers = await connection
.Command("SELECT Id, Name FROM Customers WHERE Region = @region")
.AddParam("region", "West")
.UseCancellationToken(cancellationToken)
.Map<Customer>()
.ToListAsync();
Remarks
Create one with the Command and StoredProcedure extension methods in ConnectionExtensions, add parameters with AddParam, optionally set a timeout and cancellation token, then run it with one of the execute methods.
Connections follow the same "get in, get out" model as ExpressiveCommand: each execution takes a connection, opens it only if needed, runs the command, and releases the connection. A connection supplied directly is closed again only if this execution opened it and is never disposed; a connection from a factory is disposed, and one from a pool is given back.
Constructors
- ExpressiveDbCommand(IDbConnectionFactory<DbConnection>, CommandType, string, IEnumerable<Param>?)
Constructs a command that creates a new connection from a connection factory for each execution.
- ExpressiveDbCommand(IDbConnectionPool<DbConnection>, CommandType, string, IEnumerable<Param>?)
Constructs a command that takes its connections from a connection pool.
- ExpressiveDbCommand(DbConnection, CommandType, string, IEnumerable<Param>?)
Constructs a command that executes on an existing connection.
- ExpressiveDbCommand(DbConnection, IDbTransaction?, CommandType, string, IEnumerable<Param>?)
Constructs a command that executes on an existing connection, optionally within a transaction.
- ExpressiveDbCommand(IDbTransaction, CommandType, string, IEnumerable<Param>?)
Constructs a command that executes within a transaction, on the transaction's connection.
- ExpressiveDbCommand(Func<DbConnection>, CommandType, string, IEnumerable<Param>?)
Constructs a command that invokes a function to create a new connection for each execution.
Methods
- AddParams(DbCommand)
Handles adding the list of parameters to a new command.