/// <summary> /// 异步执行查询并将第一个结果集包装成 DataTable 返回 /// </summary> /// <param name="query">要执行的查询对象</param> /// <param name="token">取消指示</param> /// <returns>查询结果</returns> public static async Task <DataTable> ExecuteDataTableAsync(this IDbExecutable query, CancellationToken token = default(CancellationToken)) { using (var context = await query.ExecuteAsync(token)) { return(await context.LoadDataTableAsync(0, 0, token)); } }
/// <summary> /// 异步执行没有结果的查询 /// </summary> /// <param name="query">要执行的查询对象</param> /// <param name="token">取消指示</param> /// <returns>查询所影响的行数</returns> public static async Task <int> ExecuteNonQueryAsync(this IDbExecutable query, CancellationToken token = default(CancellationToken)) { using (var context = await query.ExecuteAsync(token)) { return(context.RecordsAffected); } }
/// <summary> /// 异步执行查询并将所有结果集包装成 DataTable 返回 /// </summary> /// <param name="query">要执行的查询对象</param> /// <param name="token">取消指示</param> /// <returns>查询结果</returns> public static async Task <DataTable[]> ExecuteAllDataTablesAsync(this IDbExecutable query, CancellationToken token = default(CancellationToken)) { List <DataTable> dataTables = new List <DataTable>(); using (var context = await query.ExecuteAsync(token)) { do { dataTables.Add(context.LoadDataTable(0, 0)); } while (await context.NextResultAsync()); } return(dataTables.ToArray()); }
/// <summary> /// 异步执行查询并返回首行首列 /// </summary> /// <param name="query">要执行的查询对象</param> /// <param name="token">取消指示</param> /// <returns>查询结果</returns> public static async Task <object> ExecuteScalarAsync(this IDbExecutable query, CancellationToken token = default(CancellationToken)) { using (var context = await query.ExecuteAsync(token)) { var record = await context.ReadRecordAsync(); if (record != null && record.FieldCount > 0) { return(record[0]); } else { return(null); } } }
/// <summary> /// 异步执行查询并返回首行 /// </summary> /// <param name="query">要执行的查询对象</param> /// <param name="token">取消指示</param> /// <returns>查询结果</returns> public static async Task <DataRow> ExecuteFirstRowAsync(this IDbExecutable query, CancellationToken token = default(CancellationToken)) { //UNDONE using (var context = await query.ExecuteAsync(token)) { var data = context.LoadDataTable(0, 1); if (data.Rows.Count > 0) { return(data.Rows[0]); } else { return(null); } } }