public int InserirExtra(Extra extra) { int newId = -1; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; string sqlInsert = "INSERT INTO [Extra] ([Nome], [TipoExtraId], [CompanhiaId], [Valor]) VALUES ('" + extra.Nome + "', '" + extra.TipoExtraId.ToString() + "', '" + extra.CompanhiaId.ToString() + "', '" + extra.Valor + "')"; cmd.CommandText = sqlInsert; // string sqlSelect = "SELECT [Nome] FROM TipoJato WHERE (TipoJatoId = SCOPE_IDENTITY())"; int regs = 0; try { con.Open(); regs = cmd.ExecuteNonQuery(); /* SqlDataReader dr; * cmd.CommandText = sqlSelect; * dr = cmd.ExecuteReader(); * * if (dr.Read()) * newId = (int)dr["ExtraId"]; */ } catch (Exception exc) { MessageBox.Show(exc.Message); } finally { con.Close(); } //MessageBox.Show(regs + " extra adicionado (" + newId + ")"); return(newId); }
private void onEliminarExtrasClick(object sender, RoutedEventArgs e) { if (ListBoxExtras.SelectedItem == null) { return; } Extra extraActual = ListBoxExtras.SelectedItem as Extra; if (extraActual == null) { return; } if (MessageBox.Show("Quer apagar o ExtraActual?", "Apagar o extra?", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { App.AirUberDB.EliminarExtra(extraActual.TipoExtraId); ListBoxExtras.Items.MoveCurrentToFirst(); onVerTodosExtrasClick(null, null); } }
//problem.. public void EditarExtra(Extra extra) { SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; string sql = "UPDATE [Extra] " + "SET [Nome] ='" + extra.Nome + "' " + "SET [TipoExtraId] ='" + extra.TipoExtraId.ToString() + "' " + "SET [CompanhiaId] ='" + extra.CompanhiaId.ToString() + "' " + "SET [Valor] ='" + extra.Valor.ToString() + "' " + "WHERE (ExtraId] = " + extra.ExtraId.ToString() + ")"; cmd.CommandText = sql; int regs = 0; try { con.Open(); regs = cmd.ExecuteNonQuery(); } catch (Exception exc) { MessageBox.Show(exc.Message); } finally { con.Close(); } //MessageBox.Show(regs + " tipo de extra actualizado"); }
public ObservableCollection <Extra> GetExtras() { ObservableCollection <Extra> extras = new ObservableCollection <Extra>(); SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; string sql = "SELECT e.ExtraId AS 'ExtraId', e.Nome AS 'NomeExtra', e.Valor AS 'Valor', " + "te.TipoExtraId AS 'TipoExtraId', te.Nome AS 'TipoExtraNome', " + "c.CompanhiaId AS 'CompId', c.Nome AS 'NomeComp' " + "FROM Extra e " + "JOIN TipoExtra te ON e.TipoExtraId = te.TipoExtraId " + "JOIN Companhia c ON e.CompanhiaId = c.CompanhiaId"; cmd.CommandText = sql; try { con.Open(); SqlDataReader dr; dr = cmd.ExecuteReader(); while (dr.Read()) { Extra extra = new Extra(); extra.ExtraId = (int)dr["ExtraId"]; extra.Nome = (string)dr["NomeExtra"]; extra.Valor = (decimal)dr["Valor"]; TipoExtra tipoExtra = new TipoExtra(); tipoExtra.TipoExtraId = (int)dr["TipoExtraId"]; tipoExtra.Nome = (string)dr["TipoExtraNome"]; extra.TipoExtraId = (int)dr["TipoExtraId"]; extra.TipoExtra = tipoExtra; Companhia companhia = new Companhia(); companhia.CompanhiaId = (int)dr["CompId"]; companhia.Nome = (string)dr["NomeComp"]; extra.CompanhiaId = (int)dr["CompId"]; extra.Companhia = companhia; extras.Add(extra); } } catch (Exception exc) { MessageBox.Show(exc.Message); } finally { con.Close(); } return(extras); }