public static async Task WriteLogsToDataBase(Forest forest) { Console.WriteLine($"forest : {forest.ID} writes log - Start"); SqlConnection connection = GetConnection(); string query = "INSERT INTO dbo.Logs (woodID, monkeyID, message) VALUES (@woodID, @monkeyID, @message)"; foreach (Monkey monkey in forest.MonkeysInTheForest) { foreach (Tree tree in monkey.VisitedTrees) { string message = $"{monkey.Naam} is now in tree {tree.ID} at location ({tree.PositionX},{tree.PositionY})"; using (SqlCommand command = connection.CreateCommand()) { try { await Task.Run(() => connection.Open()); command.Parameters.Add(new SqlParameter("@woodID", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@monkeyID", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@message", SqlDbType.NVarChar)); command.CommandText = query; command.Parameters["@woodID"].Value = forest.ID; command.Parameters["@monkeyID"].Value = monkey.ID; command.Parameters["@message"].Value = message; await Task.Run(() => command.ExecuteNonQuery()); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { connection.Close(); } } } } Console.WriteLine($"forest : {forest.ID} writes log - End"); }
public static async Task WriteForestToDataBase(Forest forest) { Console.WriteLine($"Write forest {forest.ID} to database - start"); SqlConnection connection = GetConnection(); string query = "INSERT INTO dbo.WoodRecords (woodID, treeID, x, y) VALUES (@woodID, @treeID, @x, @y)"; foreach (Tree tree in forest.Trees) { using (SqlCommand command = connection.CreateCommand()) { try { await Task.Run(() => connection.Open()); command.Parameters.Add(new SqlParameter("@woodID", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@treeID", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@x", SqlDbType.Int)); command.Parameters.Add(new SqlParameter("@y", SqlDbType.Int)); command.CommandText = query; command.Parameters["@woodID"].Value = forest.ID; command.Parameters["@treeID"].Value = tree.ID; command.Parameters["@x"].Value = tree.PositionX; command.Parameters["@y"].Value = tree.PositionY; await Task.Run(() => command.ExecuteNonQuery()); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { connection.Close(); } } } Console.WriteLine($"Write forest {forest.ID} to database - end"); }
public BitmapGenerator(int width, int height, Forest forest) { _width = width; _height = height; _forest = forest; }