Exemplo n.º 1
0
        public static void CreateNew(Player player)
        {
            string[] sql = new string[3] {
                "", "", ""
            };

            sql[0] = $"SELECT COUNT(1) FROM high_scores WHERE player = '{player.UserName}';";

            sql[1] = $"INSERT INTO high_scores (player, score) " +
                     $"VALUES ('{player.UserName}',{player.Score});";

            sql[2] = $"UPDATE high_scores SET score =" +
                     $" {player.Score} WHERE player = '{player.UserName}';";

            try
            {
                var dt = DataBaseConnection.ExecuteQuery(sql[0]);

                if (dt.Rows[0][0].ToString() == "1")
                {
                    DataBaseConnection.ExecuteNonQuery(sql[2]);
                }
                else
                {
                    DataBaseConnection.ExecuteNonQuery(sql[1]);
                }
            }
            catch (NpgsqlException ex)
            {
                MessageBox.Show("An error has occured in the database.\n" + ex.Message,
                                "Server Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occured.\n" + ex.Message,
                                "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }