internal static TrainsIdToName SelectIdsAndNames(string query) { connection.Open(); TrainsIdToName tbl = new TrainsIdToName() { _id = new List <int>(), Name = new List <string>() }; SQLiteCommand command = new SQLiteCommand(query, connection); SQLiteDataReader reader = command.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { tbl._id.Add((int)reader[i++]); tbl.Name.Add((string)reader[i]); } } reader.Close(); connection.Close(); return(tbl); }
internal static void UpdateTableRoutes(DataGridView DGV_ToUdpate, string selectedOperation) { int rowCount; if (selectedOperation == "Add") { rowCount = DGV_ToUdpate.RowCount - 1; } else { rowCount = DGV_ToUdpate.RowCount; } TrainsIdToName tbl = Database.SelectIdsAndNames("SELECT _id, Название FROM Поезда"); for (int i = 0; i < rowCount; i++) { bool exist = false; for (int k = 0; k < tbl._id.Count; k++) { if (DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.TrainName].Value.ToString() == tbl.Name[k]) { exist = true; DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.TrainName] = cell; DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.TrainName].Value = tbl._id[k]; } } if (!exist) { MessageBox.Show("Неудачный ввод", "Ошибка"); return; } } tbl = Database.SelectIdsAndNames("SELECT _id, Название FROM Остановки"); for (int i = 0; i < rowCount; i++) { bool fistExist = false; bool secondExist = false; for (int k = 0; k < tbl._id.Count; k++) { if (DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.FistStopName].Value.ToString() == tbl.Name[k]) { DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.FistStopName] = cell; DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.FistStopName].Value = tbl._id[k]; fistExist = true; } if (DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.LastStopName].Value.ToString() == tbl.Name[k]) { DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.LastStopName] = cell; DGV_ToUdpate.Rows[i].Cells[(int)RoutesColumnsIndexes.LastStopName].Value = tbl._id[k]; secondExist = true; } } if (!secondExist || !fistExist) { MessageBox.Show("Неудачный ввод", "Ошибка"); return; } } for (int i = 0; i < rowCount; i++) { RoutesToUpdate routesToUpdate = new RoutesToUpdate() { _Id = new int(), Trains_Id = new int(), FirstStop_Id = new int(), LastStop_Id = new int(), DepartmentTime = string.Empty, ArrivalTime = string.Empty }; for (int j = 0; j < DGV_ToUdpate.ColumnCount; j++) { routesToUpdate._Id = Convert.ToInt32(DGV_ToUdpate.Rows[i].Cells[j++].Value); routesToUpdate.Trains_Id = Convert.ToInt32(DGV_ToUdpate.Rows[i].Cells[j++].Value); routesToUpdate.FirstStop_Id = Convert.ToInt32(DGV_ToUdpate.Rows[i].Cells[j++].Value); routesToUpdate.LastStop_Id = Convert.ToInt32(DGV_ToUdpate.Rows[i].Cells[j++].Value); routesToUpdate.DepartmentTime = DGV_ToUdpate.Rows[i].Cells[j++].Value.ToString(); routesToUpdate.ArrivalTime = DGV_ToUdpate.Rows[i].Cells[j].Value.ToString(); } Database.DeleteRow("Маршруты", routesToUpdate._Id); Database.UpdateRoutes(routesToUpdate); } }