internal static MazeData GetFieldData() { var maze = new MazeData(fieldSize); for (int i = 1; i < fieldSize + 1; i++) { for (int j = 1; j < fieldSize + 1; j++) { var n = field[i, j]; if (n > 0 && n < 4) { maze.cellsList.Add(new cellStruct(i, j, n)); } } } return(maze); }
internal static void SaveMazeToDB(MazeData maze, string name, string dt) { using (var connection = new SQLiteConnection(DatabaseSource)) using (var command = new SQLiteCommand(connection)) { connection.Open(); //сохраняем имя таблицы command.CommandText = $"INSERT INTO [Mazes]([NAME], [DT], [SIZE]) " + $"VALUES(\"{name}\", \"{dt}\", {maze.fieldSize});"; command.ExecuteNonQuery(); //считываем номер записанной таблицы int id = 0; command.CommandText = "SELECT MAX(ID) FROM [Mazes];"; using (var reader = command.ExecuteReader()) { if (reader.Read()) { id = reader.GetInt32(0); } } using (var transaction = connection.BeginTransaction()) { //вставляем в цикле (?) ячейки из списка string insertCmd = "INSERT INTO [Cells] (IDMAZE, X, Y, VALUE) VALUES(?, ?, ?, ?)"; foreach (var cell in maze.cellsList) { using (var cmd = new SQLiteCommand(insertCmd, connection)) { cmd.Parameters.AddWithValue("@IDMAZE", id); cmd.Parameters.AddWithValue("@X", cell.x); cmd.Parameters.AddWithValue("@Y", cell.y); cmd.Parameters.AddWithValue("@VALUE", cell.value); cmd.ExecuteNonQuery(); } } transaction.Commit(); } connection.Close(); // Close the connection to the database } }
internal static void SetFieldData(MazeData maze) { ResetField(maze.fieldSize, false); foreach (var cell in maze.cellsList) { field[cell.x, cell.y] = cell.value; if (cell.value == 2) { pStart = new Point(cell.x, cell.y); } if (cell.value == 3) { pEnd = new Point(cell.x, cell.y); } } }
internal static void LoadMazeFromDB(int id, int size) { using (var connection = new SQLiteConnection(DatabaseSource)) using (var command = new SQLiteCommand(connection)) { connection.Open(); command.CommandText = "Select [X], [Y], [VALUE] FROM [Cells] WHERE [IDMAZE]=" + id.ToString(); MazeData maze = new MazeData(size); using (var reader = command.ExecuteReader()) { while (reader.Read()) { maze.cellsList.Add(new cellStruct(reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2))); } } connection.Close(); // Close the connection to the database Helper.SetFieldData(maze); } }