RelationalStorage.ReadAsync<TResult> 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
執行指定的 語句。 特別要搭配 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>>
override this.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))
類型參數
- TResult
結果類型。
參數
- query
- String
執行指定的 語句。 特別要搭配 SELECT 語句使用。
- parameterProvider
- Action<IDbCommand>
將參數新增至查詢。 參數名稱必須符合查詢中定義的名稱。
- selector
- Func<IDataRecord,Int32,CancellationToken,Task<TResult>>
此函式會轉換原始 IDataRecord 結果,以輸入 Int32 做為結果集編號的參數。
- cancellationToken
- CancellationToken
取消語彙基元。 預設值為 None。
- commandBehavior
- CommandBehavior
應該使用的命令列為。 預設值為 Default。
傳回
Task<IEnumerable<TResult>>
物件清單,作為 的結果 。
實作
範例
此範例示範如何進行手動調整的資料庫呼叫。
//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);