public static IEnumerable <Railway> GetAllRailways() { var commandString = "SELECT Id, Name, TrainQuantity, RailwayLength, StaffNumber FROM [Table]"; SqlCommand getAllCommand = new SqlCommand(commandString, connection); connection.Open(); var reader = getAllCommand.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { var id = reader.GetInt32(0); var name = reader.GetSqlString(1); var trainQuantity = reader.GetInt32(2); var railwayLength = reader.GetInt32(3); var staffNumber = reader.GetInt32(4); var railway = new Railway { Id = id, Name = name.IsNull == true ? "No name" : name.Value, TrainQuantity = trainQuantity, RailwayLength = railwayLength, StaffNumber = staffNumber }; yield return(railway); } } ; connection.Close(); }
private void ButtonDeleteClick(object sender, RoutedEventArgs e) { var result = MessageBox.Show("Are you sure?", "Deleting record...", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { Railway railway = dataGrid.SelectedItem as Railway; context.Railways.Remove(railway); context.SaveChanges(); } }
private void ButtonAddClick(object sender, RoutedEventArgs e) { var railway = new Railway(); EditWindow ew = new EditWindow(railway); var result = ew.ShowDialog(); if (result == true) { context.Zkhs.Add(railway); context.SaveChanges(); ew.Close(); } }
private void ButtonEditClick(object sender, RoutedEventArgs e) { Railway railway = dataGrid.SelectedItem as Railway; EditWindow ew = new EditWindow(railway); var result = ew.ShowDialog(); if (result == true) { context.SaveChanges(); ew.Close(); } else { context.Entry(railway).Reload(); dataGrid.DataContext = null; dataGrid.DataContext = context.Zkhs.Local; } }
public EditWindow(Railway railway) : this() { this.railway = railway; grid.DataContext = railway; }