Expressive commands
An expressive command is a small object you build with chained calls: pick the SQL (or stored procedure), add parameters, then call a terminal that runs it.
Two starting points
The factory form (connectionFactory.Command(sql) or connectionFactory.StoredProcedure(name)) and the connection form (connection.Command(sql) or connection.StoredProcedure(name)) look the same after the first call. In the samples author is a variable (int author = 42;). Side by side, row against row:
Factory form
// nothing: the factory creates, opens and disposes the connection
List<Post> posts = await connectionFactory
.Command(ListSql)
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToListAsync(cancellationToken);
Connection form
await using var connection = new SqliteConnection(connectionString);
List<Post> posts = await connection
.Command(ListSql)
.AddParam(author) // name and type inferred from the variable
.Map<Post>()
.ToListAsync(cancellationToken);
In the factory form the command creates, opens and disposes a connection for every terminal. In the connection form the connection is yours: the command opens it only when it is closed, closes only what it opened, and never disposes it. See who owns the connection.
A connection pool (IDbConnectionPool) is also a valid source, but it is there for a pooling scheme of your own: a pool a particular database needs, or one kept in memory and reused locally. Providers already pool their connections (Microsoft SQL Server included), so with those a factory is the right source and a pool of your own adds nothing.
A command is not safe to share across threads: one built from a single connection throws InvalidOperationException if two terminals run at once.
Which command type you get
| You call it on | You get | Reader type |
|---|---|---|
IDbConnection, IDbTransaction, IDbConnectionFactory |
ExpressiveCommand |
IDataReader |
DbConnection, DbTransaction, IDbConnectionFactory<TConnection> |
ExpressiveDbCommand |
DbDataReader (async reads) |
SqlConnection or a SqlConnectionFactory (MSSqlClient package) |
ExpressiveSqlCommand |
SqlDataReader |
Choose by what you need: only the last two rows have Async terminals, so a service that awaits starts from a DbConnection or an IDbConnectionFactory<TConnection> (for example IDbConnectionFactory<DbConnection>). Pass a CommandType as the second argument of Command if you need TableDirect.
Defining a query once
When the same query runs from several places, define it once, with no connection, and bind it where it runs. A DbQuery is immutable, so it is safe in a static readonly field and shared by every caller. Using(source) binds it to a connection, a transaction, a pool or a factory; each AddParam returns a new immutable call and leaves the one before it as it was, so a held call can branch.
static readonly DbQuery PostsByAuthor
= DbQuery.Procedure("dbo.PostsByAuthor");
// The names come from the variables, as AddParam(author) does on a command.
List<Post> posts = await PostsByAuthor.Using(db)
.AddParam(authorId)
.AddParam(day)
.Map<Post>()
.ToListAsync(cancellationToken);
A procedure's parameter names are read through the ADO.NET schema collection ProcedureParameters (DbConnection.GetSchema) the first time a call needs them, and remembered; a provider that answers nothing is remembered as unknown for a short while, so a repeat call does not ask again.
To have the values checked at compile time, declare their type once. It is a struct that implements IEquatable<TParams> (a readonly record struct is the intended shape, and does) whose public properties and fields are the parameters, given in one call that the compiler checks:
readonly record struct AuthorDayParams(int AuthorId, DateTime Day);
static readonly DbQuery<AuthorDayParams> PostsByAuthorTyped
= DbQuery.Procedure("dbo.PostsByAuthor").Params<AuthorDayParams>();
List<Post> posts = await PostsByAuthorTyped.Using(db)
.AddParams(new(AuthorId: authorId, Day: day))
.Map<Post>()
.ToListAsync(cancellationToken);
Map the definition too
Map<T>() also works on the definition, before any connection exists. The result, a MappedDbQuery<T>, is immutable like the DbQuery under it, so it too lives in a static readonly field, and the map, with the reader compiled from it, lives with it: there is no separate RowMap<T> field to declare. (DbQuery<TParams> has the same Map, giving a MappedDbQuery<TParams, T>.) A class that holds its queries as definitions, then the same class building each command on every call:
public class PostQueriesAfter(IDbConnectionFactory<SqliteConnection> connectionFactory)
{
// Recommended. Declared once: immutable, no connection, safe in a static field.
static readonly MappedDbQuery<Post> ByAuthor
= DbQuery.Text(ListSql).Map<Post>();
public ValueTask<List<Post>> ByAuthorAsync(
int author,
CancellationToken cancellationToken)
=> ByAuthor
.Using(connectionFactory)
.AddParam(author)
.ToListAsync(cancellationToken);
public ValueTask<Post?> FirstByAuthorAsync(
int author,
CancellationToken cancellationToken)
=> ByAuthor
.Using(connectionFactory)
.AddParam(author)
.FirstOrDefaultAsync(cancellationToken);
}
public class PostQueriesBefore(IDbConnectionFactory<SqliteConnection> connectionFactory)
{
public ValueTask<List<Post>> ByAuthorAsync(
int author,
CancellationToken cancellationToken)
=> connectionFactory
.Command(ListSql)
.AddParam(author)
.Map<Post>()
.ToListAsync(cancellationToken);
public ValueTask<Post?> FirstByAuthorAsync(
int author,
CancellationToken cancellationToken)
=> connectionFactory
.Command(ListSql)
.AddParam(author)
.Map<Post>()
.FirstOrDefaultAsync(cancellationToken);
}
Using(source) takes the same four sources and returns the ordinary MappedQuery<T> view, with every terminal, Skip and Take. The parameters can come after Using, as above, or before it, and the command is the same either way. The values before the source are held without a connection, so the held call has no terminal: it cannot be read until Using gives it a source.
// Costs the same as adding the values after Using: binding the source allocates nothing.
MappedDbQuery<Post> byAuthor
= DbQuery.Text(ListSql).Map<Post>();
return byAuthor
.AddParam(author)
.Using(connectionFactory)
.ToListAsync(cancellationToken);
What the definition fixes is what never changes from one call to the next: the command text, the map, and SingleResult(), AsyncReads() and UsePool(pool, clearOnReturn), each of which returns a new definition. What each call chooses is the values and, on the view, Skip and Take:
public static ValueTask<List<Post>> PagedAsync(
IDbConnectionFactory<SqliteConnection> connectionFactory,
MappedDbQuery<Post> byAuthor,
int author,
CancellationToken cancellationToken)
=> byAuthor
.Using(connectionFactory)
.AddParam(author)
.Skip(1).Take(1)
.ToListAsync(cancellationToken);
A parameter added after Using is added to a query that Using made from a definition. A query built the other way (connection.Command(sql).Map<T>()) takes its parameters on the command, before Map: the mapped view has no AddParam. A default value of one of these types throws the same exception, as the other structs do.
Everything after Using is what a command does: Map<T>() is the same extension, every terminal and SingleResult() apply, the connection is opened late and closed again only if the call opened it, a factory's connection is disposed and a pool's is given back. SetTimeout(seconds) on a definition returns a new definition. A DbQuery takes the same sources as Using does for a command, but only the typed ones: a connection (DbConnection), a transaction, an IDbConnectionPool<TConnection> or an IDbConnectionFactory<TConnection>; a bare Func<TConnection> is not one (wrap it in new DbConnectionFactory<TConnection>(func)).
A call allocates less than the command builder and within 7 bytes of ADO.NET written by hand (within 23 bytes at a single value). Names are resolved as a command resolves them (see Name a parameter from its argument): AddParam(authorId) finds @author_id, and the typed form matches each member of the values type the same way and, when the names are known, does not send a member the query does not declare. A text definition finds its @name markers once, when it is made; a procedure's names are remembered for five minutes per connection source (the factory, the pool or the connection, also for a transaction) and per server and database, and read again only when they do not fit.
Writes and scalars
A DbQuery reads. For one value, Scalar<T>() on the definition gives a ScalarDbQuery<T>, run with ExecuteScalarAsync: the first column of the first row, converted as Map<T>() converts a column. A NULL, or no row, is null for a nullable or reference T, and throws ValueMappingException for a non-nullable value type.
A write is a DbNonQuery, made with Text or Procedure and run with ExecuteNonQueryAsync, which returns the number of rows affected as the provider reports it (it can be -1, for example for a procedure that sets NOCOUNT ON).
public class PostWrites(IDbConnectionFactory<SqliteConnection> connectionFactory)
{
static readonly DbNonQuery SetTitle
= DbNonQuery.Text("update posts set title = @title where id = @id");
static readonly ScalarDbQuery<int> PostCount
= DbQuery.Text("select count(*) from posts where author = @author")
.Scalar<int>();
// A nullable type: null when there is no post.
static readonly ScalarDbQuery<long?> LastId
= DbQuery.Text("select max(id) from posts where author = @author")
.Scalar<long?>();
// The new id is a value to read, so the insert is a scalar.
static readonly ScalarDbQuery<long> Add
= DbQuery.Text(
"insert into posts (author, title, views) values (@author, @title, 0); "
+ "select last_insert_rowid()")
.Scalar<long>();
// Returns the number of rows affected.
public ValueTask<int> SetTitleAsync(
int id,
string title,
CancellationToken cancellationToken)
=> SetTitle
.Using(connectionFactory)
.AddParam(id)
.AddParam(title)
.ExecuteNonQueryAsync(cancellationToken);
public ValueTask<int> CountAsync(
int author,
CancellationToken cancellationToken)
=> PostCount
.Using(connectionFactory)
.AddParam(author)
.ExecuteScalarAsync(cancellationToken);
public ValueTask<long?> LastIdAsync(
int author,
CancellationToken cancellationToken)
=> LastId
.Using(connectionFactory)
.AddParam(author)
.ExecuteScalarAsync(cancellationToken);
public ValueTask<long> AddAsync(
int author,
string title,
CancellationToken cancellationToken)
=> Add
.Using(connectionFactory)
.AddParams(new { author, title })
.ExecuteScalarAsync(cancellationToken);
}
Both take the same sources as a query, and their values before or after Using; Params<TParams>() declares typed values. Set a timeout with SetTimeout on a DbNonQuery, or on the DbQuery before Scalar<T>(). Output and return parameters are not supported here: use a command for them (see Return values and output parameters).
Parameters
Name a parameter in the way that fits what you have:
- A variable or a member holds the value:
AddParam(author)orAddParam(search.Author). The name comes from the argument. This is the simplest form. - A literal, or any other expression, or a compiler older than C# 10: name it yourself,
AddParam("author", 42). - An object holds several values:
AddParams(search).
A name you write is sent exactly as written: the library never adds or removes an @. Write it without one ("author" for @author), as the samples do. A name derived from an argument has none either; for a stored procedure it is replaced by the provider's real name, which has one (@title_contains). SQLite accepts a name with or without the @, as the samples check.
Name a parameter from its argument
AddParam(value) takes the name from the argument, so you do not spell it twice. The argument must be a variable or a chain of members (properties and fields, members of members): author, search.Author, order.Customer.Id. A type-qualified static or constant is a chain too: DateTime.UtcNow derives the name UtcNow and string.Empty derives Empty, so name such a value yourself unless that is the name you want. Deriving needs C# 10 or newer; an older compiler gets an ArgumentException and must pass the name.
// The parameter is named from the argument:
// search.Author adds a parameter named Author.
// A text command has no procedure to ask,
// so the name is used as written.
Post[] posts = await connectionFactory
.Command("select * from posts where author = @Author order by id")
.AddParam(search.Author) // name and type inferred from the member
.Map<Post>()
.ToArrayAsync(cancellationToken);
For a stored procedure the name is settled when the command runs. The provider reports the procedure's real parameter names (SQL Server does), and each argument is matched to one of them:
- Exactly, else ignoring case, else ignoring case and underscores:
TitleContainsfinds@title_contains. - Else with one common parameter prefix dropped from the real name (
p_,in_,i_,prm_,par_,v_, or a lonepbefore a capital letter):Authorfinds@p_authorand@pAuthor. A field prefix on the argument (m_author,s_author,_author) is also tried without it. - A chain tries its longer endings first, each through all of those steps before the next:
order.Customer.IdtriesorderCustomerId,CustomerId, thenId, so@customer_idand@p_customer_idare chosen over a bare@id.
For example AddParam(search.Author) sends @p_author when the procedure declares that, @Author or @author when it declares one of those, and throws when it declares none of them. The prefix step (rule 2) can bind a parameter whose name only starts like a prefix: Stock finds @in_stock, Value finds @par_value and Progress finds @in_progress. When that is not the parameter you mean, name it yourself: AddParam("stock", stock).
When the real names are not available, as with a text command or another provider, the last member is used as written, without an @: Author. The names are read once and then remembered: see Remembered names.
A null value is sent as a database NULL. A NULL also carries the database type of its argument (string? as String, int? as Int32, byte[]? as Binary), which SQL Server needs to accept a NULL for a typed parameter such as varbinary: AddParam(note) takes it from the argument, and AddParam("note", (int?)null) from the overload. A value of type object, or a call that gives the direction by position, has no type to use, so pass one:
// A value of type object has no type to use, so pass one.
Post[] posts = await connectionFactory
.StoredProcedure("[dbo].[SearchPosts]")
.AddParam(
"title_contains",
titleContains ?? (object)DBNull.Value,
SqlDbType.NVarChar)
.Map<Post>()
.ToArrayAsync(cancellationToken);
// The procedure declares @author and @title_contains.
// Each argument is matched to a real parameter,
// so search.TitleContains finds @title_contains.
Post[] byArguments = await connectionFactory
.StoredProcedure("[dbo].[SearchPosts]")
.AddParam(search.Author) // name and type inferred from the member
.AddParam(search.TitleContains)
.Map<Post>()
.ToArrayAsync(cancellationToken);
// Or give it the whole object:
// the members the procedure declares are sent, the rest are ignored.
Post[] posts = await connectionFactory
.StoredProcedure("[dbo].[SearchPosts]")
.AddParams(search)
.Map<Post>()
.ToArrayAsync(cancellationToken);
AddParams(source) sends the public properties and fields of an object. When the procedure's names are known, only the members it declares are sent, under the procedure's real names; a parameter you add with AddParam, by name or from an argument, wins over a member. A text command that has @name markers is matched against them in the same way, so a member with no marker is not sent. When the names are not known, every member is sent, named as written: a stored procedure on a provider that cannot report its parameter names, or a text command with no @name marker. A procedure parameter that no member fills is left alone: it takes its default, or you add it with AddParam; a misspelled member is simply not sent. Pass only an object you trust: every declared member is sent, so an object bound from a request can fill a parameter you meant to decide on the server (a tenant, a deleted-rows flag). Add such a value with AddParam("name", value), which wins.
The members are those of the static type of the argument, not of the object's runtime type: a variable typed object has no members and sends nothing, an interface sends only its own members, and a Dictionary<,> sends its own properties (Count, Keys) rather than its entries. Pass a variable of the type you mean. Every public readable property is read when you call AddParams, even one the procedure does not declare, so a getter that throws or loads lazily runs then. An anonymous object works: AddParams(new { search.Author, TitleContains = "ode" }).
An argument that is not a plain member chain (a call, an index, an operator, a literal) throws ArgumentException where you wrote it: name the parameter with AddParam("name", value). A name that matches no parameter, or more than one, or two arguments that resolve to the same parameter, throws InvalidOperationException that says what you wrote and, when the names are known, what the procedure declares.
Remembered names
Known names are read once and remembered for five minutes (an empty answer for 15 seconds, see below), per server, database and procedure name, on the connection factory (or on the connection itself when you start from a connection). Register the factory once: a connection or a factory you create for each call starts with nothing remembered, so its names are read for every command. Names read inside a transaction are remembered too, even if the transaction rolls back.
A procedure that changes can show its old names for up to five minutes. Two cases read again before they fail or drop anything: an argument from AddParam(value) that matches none of the remembered names, and a member of an AddParams object that the remembered names do not declare. Either makes the library read the names once more. A read that follows a miss is itself remembered together with the fact that it was a second look, so a member the procedure never declares costs one extra read per five minutes, not one per run. Everything else uses the remembered names as they are until they expire:
- a member of an
AddParamsobject that the second read still does not find is skipped, so a parameter added to the procedure after that read is not sent until the names expire; - a less specific match is kept while a more specific new parameter exists (
@idfororder.Customer.Idalthough@customer_idnow exists); - an ambiguity, or two arguments that resolve to the same parameter, is reported from the remembered names;
- when the second read finds nothing (the procedure is not visible to the login, or it now declares no parameters), the argument is sent as written, and the old names are dropped.
A read that finds nothing (an unreadable procedure, one that does not exist, or one that declares no parameters) is remembered too, but for 15 seconds only, so a procedure that cannot be read costs one lookup in 15 seconds, not one in every run, and a procedure that appears or changes is picked up soon. Until then its derived names are sent as written.
What was sent?
command.Params always lists what you added: a name you wrote as written, an argument-derived name as its last member, and the members of an AddParams object under their member names. A run never changes it. The real names are worked out in every run, for the connection that run uses, so one command object can run against two databases that name the parameters of a procedure differently. To see what the run sent, give Execute(command => ...) a function that reads the provider's command: its CommandText, CommandType and Parameters (name and value of each) already have the real names. The library keeps no log of the statement and has no other hook.
A derived name stays as written when no real names were read: a text command, another provider, a procedure name with a database part (otherdb.dbo.proc), a procedure the login cannot see or that does not exist, or one that declares no parameters. Name the parameters yourself when you call a procedure in another database.
Name a parameter yourself
AddParam(name, value) adds an input parameter under the name you give. Overloads accept a database type and a ParameterDirection. AddParamIf(condition, value) (or AddParamIf(condition, name, value)) adds the parameter only when the condition is true, which keeps the SQL text constant. A false condition adds nothing and does not look at the argument, so a name that could not be derived is an error only when the condition holds:
// AddParamIf adds the parameter only when the condition is true:
// use it when the SQL text varies with the same condition.
string sql = author.HasValue
? "select * from posts where author = @author order by id"
: "select * from posts order by id";
Post[] posts = await connectionFactory
.Command(sql)
.AddParamIf(author.HasValue, author)
.Map<Post>()
.ToArrayAsync(cancellationToken);
A generated id
To get the id an insert generated, select it in the same command and read it with ExecuteScalarAsync<T>() (the statement that returns the id depends on your database; SQLite is shown). ExecuteReturnAsync<T>() is only for a procedure's RETURN code.
// The insert and a select of the new id in one command;
// ExecuteScalarAsync<T>() reads the id
// (SQLite: last_insert_rowid()).
long newId = await connectionFactory
.Command("""
insert into posts (author, title, body, views)
values (@author, 'New', null, 0);
select last_insert_rowid()
""")
.AddParam(author) // name and type inferred from the variable
.ExecuteScalarAsync<long>(cancellationToken);
Return values and output parameters
ExecuteReturnAsync<T>() runs the command and returns the value of its return-value parameter, which it adds itself (you do not call AddReturnParam for it). As with ExecuteScalarAsync<T>(), the value is converted to T the way Map<T>() converts a column: a NULL is null for a nullable T and throws ValueMappingException for a non-nullable value type. A token that is already cancelled fails the call with OperationCanceledException before anything runs. A SQL Server stored procedure returns an int, its RETURN code:
// ExecuteReturnAsync<T>() adds the return-value parameter itself,
// runs the procedure and returns its value converted to T
// (a SQL Server procedure returns an int, its RETURN code).
int returned = await connectionFactory
.StoredProcedure("[dbo].[TouchPosts]")
.AddParam(author)
.ExecuteReturnAsync<int>(cancellationToken);
Output values cannot be read back. You can send an output parameter (AddParam(name, ParameterDirection.Output), with a type and size on SQL Server) but not read what the procedure put in it. A procedure that hands its results back through output parameters needs plain ADO.NET; a result set and the return value are readable.
Terminals
| Terminal | Returns |
|---|---|
ExecuteNonQueryAsync(token) |
Rows affected |
ExecuteScalarAsync<T>(token), ExecuteScalarAsync(value => ..., token), ExecuteScalarAsync(token) |
The first column of the first row: which form |
ExecuteReturnAsync<T>(token) |
The value of the return-value parameter: see above |
ExecuteAsync(command => ...) |
Whatever your function returns, given the raw command with its text, parameters and timeout applied: use it for a call the other terminals do not wrap |
ExecuteReaderAsync(reader => ..., token) |
Whatever your function returns, given the live reader |
Map<T>() then a terminal |
Mapped objects, or one value per row such as int read from the first column: see Mapping rows to objects and One value per row |
Map<T>() then ToChannelReader() or AsSourceBlock() |
See Channels and Dataflow |
The blocking forms (the same names without Async) are in the Open.Database.Extensions.Synchronous namespace: add using Open.Database.Extensions.Synchronous; to call them. The Async terminals need no extra using.
Map<T>() reads one column as easily as a model; the token goes last:
// Map<int>() reads the first column of each row.
int[] ids = await connectionFactory
.Command("select id from posts order by id")
.Map<int>()
.ToArrayAsync(cancellationToken);
The Async terminals honor the command's cancellation token (UseCancellationToken(token)); ExecuteNonQueryAsync and ExecuteScalarAsync are on ExpressiveDbCommand and ExpressiveSqlCommand. SetTimeout(seconds) sets the command timeout. The default is 60 seconds. On SQL Server the lookup of a stored procedure's parameter names uses the same timeout. A failed lookup throws the provider's own exception (a SqlException stays a SqlException, so a retry policy still sees it), with the procedure's name in Exception.Data["Procedure"].
UseCancellationToken(token) sets a token for the command. A token passed to an async terminal such as ToListAsync(token) is combined with it, and either one cancels the call. The Async terminals of the command itself (ExecuteNonQueryAsync, ExecuteScalarAsync, ExecuteReaderAsync, ExecuteReturnAsync) take a CancellationToken too, and it is combined with this one. The call then stops with OperationCanceledException, except that a provider may report a cancellation that arrives while the command is executing as its own exception (SQL Server's SqlException, "Operation cancelled by user"); the library does not translate it. The token covers opening the connection, looking up stored-procedure parameter names, executing the command and reading the rows.
Which scalar form
ExecuteScalarAsync<T>()for the value asT; it converts the wayMap<T>()does (abigintread asint). No rows and aNULLare bothnullfor a type that takes null (int?,string), and both throwValueMappingExceptionfor a non-nullable value type, so ask forint?and give the default yourself:(await command.ExecuteScalarAsync<int?>(token)) ?? -1.ExecuteScalarAsync(value => ...)to convert in your own way. The function takes the provider's value as anobject?(Func<object?, T>):nullfor aNULLor no rows, neverDBNull.ExecuteScalarAsync()for the raw value:DBNull.Valuefor aNULL,nullfor no rows.Map<T>()thenToListAsync(token)(orToArrayAsync(token), ...) for many rows of one column: it converts like a property, asExecuteScalarAsync<T>()does. See One value per row.
ExecuteScalarAsync<T>() converts the value the provider returns the way Map<T>() converts a column. A value that is already a T is returned as it is. An integer converts to another integer type with a range check (a bigint read as int, an int read as long), and any other value is converted by the same code Map<T>() uses (a numeric, which is a decimal, read as long). A value that cannot be converted, or does not fit, throws ValueMappingException with the conversion's own error inside. A NULL or no row into a non-nullable value type throws ValueMappingException; ask for the nullable type, int? or long?, when a NULL or no row is expected.
// SQLite returns count(*) as a long (a bigint).
// ExecuteScalarAsync<int> converts it, as Map<int>() does:
// a value that does not fit in an int throws ValueMappingException.
int count = await connectionFactory
.Command("select count(*) from posts")
.ExecuteScalarAsync<int>(cancellationToken);
// ExecuteScalarAsync<T> converts the value the provider returns,
// as Map<T>() does: a count can be read as long or as int
// (SQLite counts as long; SQL Server counts as int).
// Use the Func overload to convert in your own way.
long count = await connectionFactory
.Command("select count(*) from posts where author = @author")
.AddParam(author) // name and type inferred from the variable
.ExecuteScalarAsync<long>(cancellationToken);
int updated = await connectionFactory
.Command("update posts set views = views + 1 where author = @author")
.AddParam(author)
.ExecuteNonQueryAsync(cancellationToken);
int count = await connectionFactory
.Command("select count(*) from posts")
.ExecuteScalarAsync(
static value
=> Convert.ToInt32(value),
cancellationToken);