Class DbNonQuery
- Namespace
- Open.Database.Extensions
- Assembly
- Open.Database.Extensions.Core.dll
A write defined once: the command text (or stored procedure name) and its timeout, with no connection. Immutable and safe to keep in a static field and share.
public sealed class DbNonQuery : IEquatable<DbNonQuery>
- Inheritance
-
DbNonQuery
- Implements
- Inherited Members
Remarks
Bind it with Using, add the values with AddParam (or
AddParams), then run it with ExecuteNonQueryAsync, which
returns the number of rows affected:
static readonly DbNonQuery SetTitle
= DbNonQuery.Text("update Posts set Title = @title where Id = @id");
int rows = await SetTitle.Using(db)
.AddParam(id)
.AddParam(title)
.ExecuteNonQueryAsync(cancellationToken);
For rows use Map<T>(), and for one value Scalar<T>(). The connection is opened late and released as DbQuery releases it. Output and return parameters are not supported.
Methods
- AddParam(string, string?)
Adds an input parameter with a string value.
- AddParam<TValue>(string, TValue)
Adds an input parameter named as you write it.
- AddParam<TValue>(TValue, string?)
Adds an input parameter named for the argument:
AddParam(authorId)addsauthorId,AddParam(filter.AuthorId)addsAuthorId.
- AddParams<TValues>(TValues)
Adds the public properties and fields of
valuesas parameters, each under its own name.
- Equals(DbNonQuery?)
Indicates whether the current object is equal to another object of the same type.
- Equals(object?)
Determines whether the specified object is equal to the current object.
- GetHashCode()
Serves as the default hash function.
- Params<TParams>()
Declares the type of the values: a struct (a readonly record struct is the intended shape) whose public properties and fields are the parameters.
- Procedure(string)
Defines a write that calls a stored procedure.
- SetTimeout(ushort)
Returns a new definition with the command timeout set; this one is not changed.
- Text(string)
Defines a write from SQL text.
- Using(DbConnection)
Binds the query to a connection. The connection is opened late and closed again only if this call opened it; it is never disposed.
- Using(DbTransaction)
Binds the query to a transaction, which runs it on the transaction's connection.
- Using<TConnection>(IDbConnectionFactory<TConnection>)
Binds the query to a factory: each run creates a connection, opens it, and disposes it when finished.
- Using<TConnection>(IDbConnectionPool<TConnection>)
Binds the query to a pool: each run takes a connection, opens it if needed, and gives it back.