public static void ExercicioF(string cs, string isin, decimal valor, DateTime date) { try { using (var ts = new TransactionScope()) { IMapperTriplo triplosMapper = new MapperTriplo(); IMapperRegisto registoMap = new MapperRegisto(); Triplo triplo = new Triplo() { Identificacao = isin, Dia = date, Valor = valor }; RegistoKey key = new RegistoKey(isin, date); triplosMapper.Create(triplo); Console.WriteLine("\nChamada ao stored procedure:\n"); P_atualizaValorFunc(new SqlConnection(cs)); Console.WriteLine("Registo depois da chamada ao stored procedure:"); Console.WriteLine(registoMap.Read(key).ToString()); ts.Complete(); } } catch (Exception ex) { Console.WriteLine("Excepção apanhada : " + ex.Message); } }
public static void Exercicio1B_Update(string isin, DateTime date, int val) { try { using (var ts = new TransactionScope()) { RegistoKey key = new RegistoKey(isin, date.Date); IMapperRegisto mapperRegisto = new MapperRegisto(); IMapperValoresMercado mapperValoresMercado = new MapperValoresMercado(); IMapperInstrumento mapperInstrumento = new MapperInstrumento(); Instrumento instrumento = mapperInstrumento.Read(isin); Console.WriteLine("Informação de valores de mercado antes do update:"); Console.WriteLine(mapperValoresMercado.Read(new ValoresMercadoKey(instrumento.CodigoMercado, date)).ToString()); //Update Registo registo = mapperRegisto.Read(key); registo.ValorAbertura = val; mapperRegisto.Update(registo); Console.WriteLine("\nUpdate\n"); Console.WriteLine("Informação de valores de mercado depois do update:"); Console.WriteLine(mapperValoresMercado.Read(new ValoresMercadoKey(instrumento.CodigoMercado, date)).ToString() + "\n"); ts.Complete(); } } catch (Exception ex) { Console.WriteLine("Excepção apanhada : " + ex.Message); } }
public Registo Read(RegistoKey id) { Registo res = null; using (var ts = new TransactionScope(TransactionScopeOption.Required)) { SqlCommand command = new SqlCommand { CommandText = "SELECT * FROM Registo AS R WHERE R.ISIN = @isin AND R.Dia = @dia;" }; SqlParameter isin = new SqlParameter("@isin", id.Isin); SqlParameter dia = new SqlParameter("@dia", id.Dia.Date); command.Parameters.Add(isin); command.Parameters.Add(dia); using (var con = new SqlConnection(cs)) { command.Connection = con; con.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { res = new Registo() { Isin = reader.GetString(0), Dia = reader.GetDateTime(1), ValorAbertura = (decimal)reader.GetSqlMoney(2), ValorFecho = (decimal)reader.GetSqlMoney(3), ValorMaximo = (decimal)reader.GetSqlMoney(4), ValorMinimo = (decimal)reader.GetSqlMoney(5), HoraFecho = reader.GetDateTime(6) }; } reader.Close(); } ts.Complete(); } return(res); }