-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hscore.cs
79 lines (66 loc) · 2.25 KB
/
Hscore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace SecurityQuiz
{
class Hscore
{
protected string rankedPlayer;
protected int rankedScore;
List<Hscore> highScores = new List<Hscore>();
public Hscore[] highscoreArr = new Hscore[5];
DB connect = new DB();
public string getrankedPlayer()
{
return rankedPlayer;
}
public int getrankedScore()
{
return rankedScore;
}
//add new highscore to highscores datatable
public void addHStoDB(string playerName, int playerScore)
{
try
{
SqlConnection con = connect.connection;
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO GAMESEC_APP.HIGHSCORES (playername, score) values (@value1, @value2)", con);
cmd.Parameters.AddWithValue("@value1", playerName);
cmd.Parameters.AddWithValue("@value2", playerScore);
cmd.ExecuteNonQuery();
con.Close();
}catch(Exception)
{
System.Windows.Forms.MessageBox.Show("Could not insert high score");
}
}
//get the top 5 high scores from database
public void fetchHighScores()
{
SqlConnection con = connect.connection;
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT TOP 5 playername, score FROM GAMESEC_APP.HIGHSCORES ORDER BY score DESC", con);
SqlDataReader sdl = cmd.ExecuteReader();
while (sdl.Read())
{
Hscore player = new Hscore();
player.rankedPlayer = sdl[0].ToString();
player.rankedScore = (int)sdl[1];
highScores.Add(player);
}
highscoreArr = highScores.ToArray();
}
catch (Exception)
{
System.Windows.Forms.MessageBox.Show("Could not get high scores");
}
}
}
}