Table of Contents

Class ExpressiveSqlCommand

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.MSSqlClient.dll

A deferred, fluent command for running SQL through a SqlConnection, SqlTransaction, connection factory, or connection pool, with parameters typed by SqlDbType.

public class ExpressiveSqlCommand : ExpressiveDbCommandBase<SqlConnection, SqlCommand, SqlDataReader, SqlDbType, ExpressiveSqlCommand>, IExecuteCommand<SqlCommand>, IExecuteCommand, IExecuteReaderAsync<SqlDataReader>, IExecuteReaderAsync, IExecuteReader<SqlDataReader>, IExecuteReader
Inheritance
ExpressiveSqlCommand
Implements
Inherited Members

Examples

List<Post> posts = await connection
    .Command("SELECT Id, Title FROM Posts WHERE AuthorId = @authorId")
    .AddParam("authorId", authorId, SqlDbType.Int)
    .Map<Post>()
    .ToListAsync(cancellationToken);

Remarks

Nothing touches the database until an execute method is called. Create one with the Command and StoredProcedure extension methods in SqlConnectionExtensions, 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 the same "get in, get out" model as ExpressiveDbCommand: 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

ExpressiveSqlCommand(SqlConnection, CommandType, string, IEnumerable<Param>?)

Initializes a new instance that executes on an existing connection.

ExpressiveSqlCommand(SqlConnection, IDbTransaction?, CommandType, string, IEnumerable<Param>?)

Initializes a new instance that executes on an existing connection, optionally within a transaction.

ExpressiveSqlCommand(IDbConnectionFactory<SqlConnection>, CommandType, string, IEnumerable<Param>?)

Initializes a new instance that creates a new connection from a connection factory for each execution.

ExpressiveSqlCommand(IDbConnectionPool<SqlConnection>, CommandType, string, IEnumerable<Param>?)

Initializes a new instance that takes its connections from a connection pool.

ExpressiveSqlCommand(IDbTransaction, CommandType, string, IEnumerable<Param>?)

Initializes a new instance that executes within a transaction, on the transaction's connection.

Methods

AddParam(string, DbType, ParameterDirection)

Adds a parameter of a DbType with no value, for example an output parameter: AddParam("@id", DbType.Int32, ParameterDirection.Output).

AddParamIf(bool, string, DbType, ParameterDirection)

Conditionally adds a parameter of a DbType with no value.

AddParams(SqlCommand)

Adds the command's parameters to a new SqlCommand.

GetProcedureParameterNames(SqlConnection, string)

Gets the parameter names a stored procedure declares by reading sys.all_parameters, so parameters added with AddParam(value) find their real names.

GetProcedureParameterNamesAsync(SqlConnection, string, CancellationToken)

Asynchronously gets the parameter names a stored procedure declares by reading sys.all_parameters.

See Also