Class ExpressiveCommand
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
A deferred, fluent command for running SQL through any IDbConnection, IDbTransaction, connection factory, or connection pool. Nothing touches the database until an execute method is called.
public class ExpressiveCommand : ExpressiveCommandBase<IDbConnection, IDbCommand, IDataReader, DbType, ExpressiveCommand>, IExecuteCommand<IDbCommand>, IExecuteCommand, IExecuteReader<IDataReader>, IExecuteReader
- Inheritance
-
ExpressiveCommand
- Implements
- Inherited Members
- Extension Methods
Examples
int west = connection
.Command("SELECT COUNT(*) FROM Customers WHERE Region = @region")
.AddParam("region", "West")
.ExecuteScalar<int>();
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 (for example ExecuteNonQuery, ExecuteScalar, ExecuteReader, or Map<T>()).
Connections follow a "get in, get out" model. Each execution takes a connection, opens it only if it is not already open, builds the database command, runs it, and releases the connection. A connection supplied directly is closed again only if this execution opened it and is never disposed. A connection that comes from a factory is disposed after the execution, and one that comes from a pool is given back to the pool.
A command built on a single connection supports one execution at a time; overlapping executions throw InvalidOperationException. Commands built on a factory or pool use a separate connection for each execution.
Constructors
- ExpressiveCommand(IDbConnectionFactory, CommandType, string, IEnumerable<Param>?)
Constructs a command that creates a new connection from a connection factory for each execution.
- ExpressiveCommand(IDbConnectionPool, CommandType, string, IEnumerable<Param>?)
Constructs a command that takes its connections from a connection pool.
- ExpressiveCommand(IDbConnection, CommandType, string, IEnumerable<Param>?)
Constructs a command that executes on an existing connection.
- ExpressiveCommand(IDbConnection, IDbTransaction?, CommandType, string, IEnumerable<Param>?)
Constructs a command that executes on an existing connection, optionally within a transaction.
- ExpressiveCommand(IDbTransaction, CommandType, string, IEnumerable<Param>?)
Constructs a command that executes within a transaction, on the transaction's connection.
- ExpressiveCommand(Func<IDbConnection>, CommandType, string, IEnumerable<Param>?)
Constructs a command that invokes a function to create a new connection for each execution.
Methods
- AddParams(IDbCommand)
Handles adding the list of parameters to a new command.