Repository navigation
Expand file tree
/
Copy pathRankForm.cs
More file actions
131 lines (113 loc) · 4.37 KB
/
Copy pathRankForm.cs
File metadata and controls
131 lines (113 loc) · 4.37 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SnakeGame
{
public partial class RankForm : Form
{
private string connStr = @"Data Source=DESKTOP-R570AKJ;Initial Catalog=LTTQ_Project;Integrated Security=True;Encrypt=False";
// biến cho DataBase
DateTime pTime;
string pID = "", pName = "";
int score;
public RankForm()
{
InitializeComponent();
titleTxt.BackColor = Color.Transparent;
LoadData();
}
public RankForm(DateTime pTime, string pID, string pName, int score)
{
InitializeComponent();
titleTxt.BackColor = Color.Transparent;
this.pTime = pTime;
this.pID = pID;
this.pName = pName;
this.score = score;
addintoDatabase();
LoadData();
}
// Lấy data từ CSDL
private DataSet getdata()
{
DataSet dataSet = new DataSet();
string query = "SELECT rank() over(order by score desc) as 'Xếp hạng', * FROM Player";
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
adapter.Fill(dataSet);
connection.Close();
}
return dataSet;
}
// Lưu thông tin người chơi vào database
// Thêm dữ liệu người chơi vào trong CSDL
private void addintoDatabase()
{
string query = @"
if (exists(select 1 from player where id = @id_value))
begin
declare @HIGHER_score int,
@PRE_score int,
@PRESENT_score int
set @PRESENT_score = @present_score_value
select @PRE_score = score
from player
where id = @id_value
if (@PRE_score > @PRESENT_score)
begin
set @HIGHER_score = @PRE_score
end
else
begin
set @HIGHER_score = @PRESENT_score
end
update player
set score = @HIGHER_score, time_played = @time_value
where id = @id_value
end
else
begin
insert into player(id,player_name,score,time_played) values (@id_value,@name_value,@present_score_value,@time_value)
end
";
using (SqlConnection connection = new SqlConnection(connStr))
{
connection.Open();
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@id_value", pID);
command.Parameters.AddWithValue("@name_value", pName);
command.Parameters.AddWithValue("@present_score_value", score);
command.Parameters.AddWithValue("@time_value", pTime.ToString());
command.ExecuteNonQuery();
connection.Close();
}
}
// Load thông tin của database vào
private void LoadData()
{
try
{
dataGridView1.DataSource = getdata().Tables[0];
}
catch (Exception ex)
{
MessageBox.Show($"Lỗi: {ex.Message}");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Hide();
}
}
}