Compartir a través de


IRelationalStorage.ReadAsync<TResult> Método

Definición

Ejecuta una instrucción determinada. Especialmente diseñado para usar con la instrucción SELECT .

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))

Parámetros de tipo

TResult

El tipo de resultado.

Parámetros

query
String

Consulta que se va a ejecutar.

parameterProvider
Action<IDbCommand>

Agrega parámetros a la consulta. Los parámetros deben estar en el mismo orden con los mismos nombres definidos en la consulta.

selector
Func<IDataRecord,Int32,CancellationToken,Task<TResult>>

Esta función transforma los resultados sin procesar IDataRecord para escribir el Int32 parámetro que es el número del conjunto de resultados.

cancellationToken
CancellationToken

Token de cancelación. Tiene como valor predeterminado None.

commandBehavior
CommandBehavior

Comportamiento del comando que se debe usar. Tiene como valor predeterminado Default.

Devoluciones

Task<IEnumerable<TResult>>

Lista de objetos como resultado de .

Ejemplos

En este ejemplo se muestra cómo realizar una llamada de base de datos optimizada a mano.

 //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);                

Se aplica a