IRelationalStorage.ReadAsync<TResult> Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Executes a given statement. Especially intended to use with SELECT statement.
public System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<TResult>> ReadAsync<TResult> (string query, Action<System.Data.IDbCommand> parameterProvider, Func<System.Data.IDataRecord,int,System.Threading.CancellationToken,System.Threading.Tasks.Task<TResult>> selector, System.Threading.CancellationToken cancellationToken = default, System.Data.CommandBehavior commandBehavior = System.Data.CommandBehavior.Default);
abstract member ReadAsync : string * Action<System.Data.IDbCommand> * Func<System.Data.IDataRecord, int, System.Threading.CancellationToken, System.Threading.Tasks.Task<'Result>> * System.Threading.CancellationToken * System.Data.CommandBehavior -> System.Threading.Tasks.Task<seq<'Result>>
Public Function ReadAsync(Of TResult) (query As String, parameterProvider As Action(Of IDbCommand), selector As Func(Of IDataRecord, Integer, CancellationToken, Task(Of TResult)), Optional cancellationToken As CancellationToken = Nothing, Optional commandBehavior As CommandBehavior = System.Data.CommandBehavior.Default) As Task(Of IEnumerable(Of TResult))
Type Parameters
- TResult
The result type.
Parameters
- query
- String
The query to execute.
- parameterProvider
- Action<IDbCommand>
Adds parameters to the query. The parameters must be in the same order with same names as defined in the query.
- selector
- Func<IDataRecord,Int32,CancellationToken,Task<TResult>>
This function transforms the raw IDataRecord results to type the Int32 parameter being the resultset number.
- cancellationToken
- CancellationToken
The cancellation token. Defaults to None.
- commandBehavior
- CommandBehavior
The command behavior that should be used. Defaults to Default.
Returns
A list of objects as a result of the .
Examples
This sample shows how to make a hand-tuned database call.
//This struct holds the return value in this example.
public struct Information
{
public string TABLE_CATALOG { get; set; }
public string TABLE_NAME { get; set; }
}
//Here are defined two queries. There can be more than two queries, in which case
//the result sets are differentiated by a count parameter. Here the queries are
//SELECT clauses, but they can be whatever, even mixed ones.
IEnumerable<Information> ret =
await storage.ReadAsync<Information>("SELECT * FROM INFORMATION_SCHEMA.TABLES; SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @tp1", command =>
{
//Parameters are added and created like this.
//They are database vendor agnostic.
var tp1 = command.CreateParameter();
tp1.ParameterName = "tp1";
tp1.Value = "some test value";
tp1.DbType = DbType.String;
tp1.Direction = ParameterDirection.Input;
command.Parameters.Add(tp1);
//The selector is used to select the results within the result set. In this case there are two homogenous
//result sets, so there is actually no need to check which result set the selector holds and it could
//marked with by convention by underscore (_).
}, (selector, resultSetCount) =>
{
//This function is called once for each row returned, so the final result will be an
//IEnumerable<Information>.
return new Information
{
TABLE_CATALOG = selector.GetValueOrDefault<string>("TABLE_CATALOG"),
TABLE_NAME = selector.GetValueOrDefault<string>("TABLE_NAME")
}
}).ConfigureAwait(continueOnCapturedContext: false);