public void Add() { bool flag = true; CheckAllTablesModel check = new CheckAllTablesModel(); if (check.CheckAllAirport(airport_name_from) == 1) { MessageBox.Show("Аэропорта от куда вылет с таким именем нет"); } else if (check.CheckAllAirport(airport_name_to) == 1) { MessageBox.Show("Аэропорта куда прилет с таким именем нет"); } else { int len; if (Int32.TryParse(length_of_route, out len)) { if (len < 100) { MessageBox.Show("маршрут слишком мал"); flag = false; } SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandText = "select * from route"; sqlCommand.Connection = DBConnection.DBConnection.SqlConnection; SqlDataReader reader = sqlCommand.ExecuteReader(); foreach (var i in reader) { if (reader.GetSqlString(1) == airport_name_from && reader.GetSqlString(2) == airport_name_to) { MessageBox.Show("Данный маршрут уже существует"); flag = false; reader.Close(); break; } } if (flag) { reader.Close(); sqlCommand.CommandText = $"insert into route values('{airport_name_from}','{airport_name_to}','{length_of_route}')"; sqlCommand.Connection = DBConnection.DBConnection.SqlConnection; sqlCommand.ExecuteNonQuery(); FlightsModel.Routs.Clear(); sqlCommand.CommandText = "select * from route"; sqlCommand.Connection = DBConnection.DBConnection.SqlConnection; SqlDataReader reader2 = sqlCommand.ExecuteReader(); foreach (var i in reader2) { FlightsModel.Routs.Add(new RouteModel { IdRoute = reader2.GetInt32(0), airport_From_To = $"{reader2.GetString(1)} - {reader2.GetString(2)}", length_of_route = Convert.ToString(reader2.GetInt32(3)) }); } reader.Close(); MessageBox.Show("Маршрут успешно добавлен"); } } else { MessageBox.Show("Введенный путь не корректный"); } } }
public void Add() { bool flag = true; CheckAllTablesModel check = new CheckAllTablesModel(); if (SelectedAirportFrom != null && SelectedAirportTo != null) { if (SelectedAirportFrom.nameAirport == SelectedAirportTo.nameAirport) { MessageBox.Show("Вы указали 2 одинаковых аэропорта"); } else { int len; if (Int32.TryParse(length_of_route, out len)) { if (len < 100) { MessageBox.Show("маршрут слишком мал"); flag = false; } SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandText = "select * from route"; sqlCommand.Connection = DBConnection.DBConnection.SqlConnection; SqlDataReader reader = sqlCommand.ExecuteReader(); foreach (var i in reader) { if (reader.GetSqlString(1) == SelectedAirportFrom.nameAirport && reader.GetSqlString(2) == SelectedAirportTo.nameAirport) { MessageBox.Show("Данный маршрут уже существует"); flag = false; reader.Close(); break; } } if (flag) { reader.Close(); sqlCommand.CommandText = $"insert into route (id_airport_from, id_airport_to, length_of_route ) values (@nameAirportFrom, @nameAirportTo,'{length_of_route}')"; sqlCommand.Connection = DBConnection.DBConnection.SqlConnection; SqlParameter nameAirportFromParam = new SqlParameter("@nameAirportFrom", SelectedAirportFrom.nameAirport); sqlCommand.Parameters.Add(nameAirportFromParam); SqlParameter nameAirportToParam = new SqlParameter("@nameAirportTo", SelectedAirportTo.nameAirport); sqlCommand.Parameters.Add(nameAirportToParam); sqlCommand.ExecuteNonQuery(); FlightsModel.Routs.Clear(); sqlCommand.CommandText = "select * from route"; sqlCommand.Connection = DBConnection.DBConnection.SqlConnection; SqlDataReader reader2 = sqlCommand.ExecuteReader(); foreach (var i in reader2) { FlightsModel.Routs.Add(new RouteModel { IdRoute = reader2.GetInt32(0), airport_From_To = $"{reader2.GetString(1)} - {reader2.GetString(2)}", length_of_route = Convert.ToString(reader2.GetInt32(3)) }); } reader2.Close(); MessageBox.Show("Маршрут успешно добавлен"); } } else { MessageBox.Show("Введенный путь не корректный"); } } } else { MessageBox.Show("Выбраны не все поля"); } }