public List <RateDM> ReadAll() { List <RateDM> result = new List <RateDM>(); try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string queryString = DatabaseResources.SelectRates; using (SqlCommand command = new SqlCommand(queryString, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var row = (IDataRecord)reader; string from = row["from"].ToString(); string to = row["to"].ToString(); decimal rate = Decimal.Parse(row["rate"].ToString()); RateDM model = new RateDM(from, to, rate); result.Add(model); } } } } } #region Exceptions catch (InvalidCastException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (IOException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (ObjectDisposedException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (InvalidOperationException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (SqlException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (ArgumentNullException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (FormatException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (OverflowException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } #endregion return(result); }
public RateDM ReadById(int id) { RateDM result = null; try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string queryString = DatabaseResources.SelectRatesById; using (SqlCommand command = new SqlCommand(queryString, connection)) { command.Parameters.AddWithValue("@Id", id); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { string from = reader["From"].ToString(); string to = reader["To"].ToString(); decimal rate = Decimal.Parse(reader["Rate"].ToString()); result = new RateDM(from, to, rate); } } } } } #region Exceptions catch (InvalidCastException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (IOException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (ObjectDisposedException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (InvalidOperationException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (SqlException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (ArgumentNullException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (FormatException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (OverflowException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } #endregion return(result); }
public RateDM Create(RateDM model) { RateDM result; int id; try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); string queryString = DatabaseResources.InsertRate; using (SqlCommand command = new SqlCommand(queryString, connection)) { command.Parameters.AddWithValue("@From", model.From); command.Parameters.AddWithValue("@To", model.To); command.Parameters.AddWithValue("@Rate", model.Rate); id = (int)command.ExecuteScalar(); } } result = ReadById(id); } #region Exceptions catch (InvalidCastException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (IOException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (ObjectDisposedException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (InvalidOperationException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (SqlException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (ArgumentNullException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (FormatException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } catch (OverflowException e) { Log.Error(e.Message); Log.Warning(e.StackTrace); throw new VuelingExamInfrastructureException(e.Message, e.InnerException); } #endregion return(result); }