-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL stuff.txt
More file actions
99 lines (85 loc) · 3.61 KB
/
SQL stuff.txt
File metadata and controls
99 lines (85 loc) · 3.61 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
CREATE TABLE leaderboard (
id INT AUTO_INCREMENT PRIMARY KEY,
player_name VARCHAR(50) NOT NULL,
score INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
string connection = "server=127.0.0.1;user=root;database=leaderboard;password=;";
MySqlConnection MySqlConnection = new MySqlConnection(connection);
try
{
MySqlConnection.Open();
MessageBox.Show("Successful!");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MySqlConnection.Close();
}
private void SQL_Write()
{
string connectionString = "server=127.0.0.1;user=root;database=leaderboard;password=;";
try
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
connection.Open();
string query = "SELECT id, player_name, score, created_at FROM leaderboard";
using (MySqlCommand command = new MySqlCommand(query, connection))
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read()) // Iterates through each row in the result set
{
int id = reader.GetInt32("id"); // Access by column name
string playerName = reader.GetString("player_name");
int score = reader.GetInt32("score");
DateTime createdAt = reader.GetDateTime("created_at");
// Output the row's data
Console.WriteLine($"ID: {id}, Player: {playerName}, Score: {score}, Created At: {createdAt}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
// Establish a connection using MySQL
using (MySqlConnection connection = new MySqlConnection(connection_sql))
{
connection.Open(); // Open the connection
// SQL Query to insert data into the leaderboard table
string query = "INSERT INTO leaderboard (player_name, score) VALUES (@player_name, @score)";
// Create a command to execute the query
using (MySqlCommand cmd = new MySqlCommand(query, connection))
{
// Add parameters to the query to avoid SQL injection
cmd.Parameters.AddWithValue("@player_name", "Tommy"); // Replace with the actual player name
cmd.Parameters.AddWithValue("@score", 100); // Replace with the actual score
// Execute the query to insert data into the database
int rowsAffected = cmd.ExecuteNonQuery();
// Check if the data was successfully inserted
if (rowsAffected > 0)
{
Console.WriteLine("Data inserted successfully!");
}
else
{
Console.WriteLine("No data was inserted.");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}