public static void ExecuteSp(this DbCommand command, Action <SpResults> handle, CommandBehavior commandBehaviour = CommandBehavior.Default, bool isConnected = true) { if (handle == null) { throw new ArgumentNullException(nameof(handle)); } using (command) { if (isConnected && command.Connection.State == ConnectionState.Closed) { command.Connection.Open(); } try { using (var reader = command.ExecuteReader(commandBehaviour)) { var spResults = new SpResults(reader); handle(spResults); } } finally { if (isConnected) { command.Connection.Close(); } } } }
public static async Task ExecuteSpAsync(this DbCommand command, Action <SpResults> handle, CommandBehavior commandBehaviour = CommandBehavior.Default, CancellationToken token = default, bool isConnected = true) { if (handle == null) { throw new ArgumentNullException(nameof(handle)); } using (command) { if (isConnected && command.Connection.State == ConnectionState.Closed) { await command.Connection.OpenAsync(token).ConfigureAwait(false); } try { using (var reader = await command.ExecuteReaderAsync(commandBehaviour, token) .ConfigureAwait(false)) { var spResults = new SpResults(reader); handle(spResults); } } finally { if (isConnected) { command.Connection.Close(); } } } }