private async Task <IWatcherCheckResult> ExecuteForQueryAsync(IDbConnection connection) { var queryResult = await _msSql.QueryAsync(connection, _configuration.Query, _configuration.QueryParameters, _configuration.QueryTimeout); var isValid = true; if (_configuration.EnsureThatAsync != null) { isValid = await _configuration.EnsureThatAsync?.Invoke(queryResult); } isValid = isValid && (_configuration.EnsureThat?.Invoke(queryResult) ?? true); var description = $"MSSQL check has returned {(isValid ? "valid" : "invalid")} result for " + $"database: '{_configuration.Database}' and given query."; return(MsSqlWatcherCheckResult.Create(this, isValid, _configuration.ConnectionString, _configuration.Query, queryResult, description)); }
public async Task <IWatcherCheckResult> ExecuteAsync() { try { using (var connection = _configuration.ConnectionProvider(_configuration.ConnectionString)) { connection.Open(); if (string.IsNullOrWhiteSpace(_configuration.Query)) { return(MsSqlWatcherCheckResult.Create(this, true, _configuration.ConnectionString, $"Database: {_configuration.Database} has been sucessfully checked.")); } var queryResult = await _msSql.QueryAsync(connection, _configuration.Query, _configuration.QueryParameters, _configuration.QueryTimeout); var isValid = true; if (_configuration.EnsureThatAsync != null) { isValid = await _configuration.EnsureThatAsync?.Invoke(queryResult); } isValid = isValid && (_configuration.EnsureThat?.Invoke(queryResult) ?? true); var description = $"MSSQL check has returned {(isValid ? "valid" : "invalid")} result for " + $"database: '{_configuration.Database}' and given query."; return(MsSqlWatcherCheckResult.Create(this, isValid, _configuration.ConnectionString, _configuration.Query, queryResult, description)); } } catch (SqlException exception) { return(MsSqlWatcherCheckResult.Create(this, false, _configuration.ConnectionString, _configuration.Query, Enumerable.Empty <dynamic>(), exception.Message)); } catch (Exception exception) { throw new WatcherException("There was an error while trying to access MSSQL database.", exception); } }