Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MySql.Data.MySqlClient;
- using System.Windows;
- namespace PATPeriode3
- {
- public class Database
- {
- private static readonly string databaseNamestring = "Server=localhost; Database=patperiode3; Uid = root; Pwd=root;";
- private static MySqlConnection conn = new MySqlConnection(databaseNamestring);
- private static MySqlCommand cmd;
- /// <summary>
- /// returns true if login succeeds
- /// </summary>
- public bool CheckLogin(string sUsername, string sPassword)
- {
- bool bResult = false;
- try
- {
- conn.Open();
- cmd = conn.CreateCommand();
- cmd.CommandText = "SELECT * FROM user WHERE Username = @username AND Password = @password;";
- cmd.Parameters.AddWithValue("@username", sUsername);
- cmd.Parameters.AddWithValue("@password", sPassword);
- MySqlDataReader dr = cmd.ExecuteReader();
- int count = 0;
- while (dr.Read())
- {
- count++;
- }
- if (count == 1)
- {
- bResult = true;
- }
- }
- catch (Exception)
- {
- }
- finally
- {
- conn.Close();
- }
- return bResult;
- }
- /// <summary>
- /// returns true if new user is added succesfully
- /// </summary>
- public bool NewUser(string sUsername, string sPassword)
- {
- bool bResult = false;
- try
- {
- conn.Open();
- cmd = conn.CreateCommand();
- cmd.CommandText = "INSERT INTO user(Username, Password, Highscore) VALUES (@username, @password, 0);";
- cmd.Parameters.AddWithValue("@username", sUsername);
- cmd.Parameters.AddWithValue("@password", sPassword);
- cmd.ExecuteNonQuery();
- bResult = true;
- conn.Close();
- }
- catch (Exception)
- {
- }
- return bResult;
- }
- /// <summary>
- /// Gets Highscore from username
- /// </summary>
- public int GetCurrentHighScore(string sUsername)
- {
- int iCurrentHighScore = 0;
- try
- {
- conn.Open();
- cmd = conn.CreateCommand();
- cmd.CommandText = "SELECT Highscore FROM user WHERE Username = @username;";
- cmd.Parameters.AddWithValue("@username", sUsername);
- MySqlDataReader dr = cmd.ExecuteReader();
- iCurrentHighScore = dr.GetInt32(0);
- }
- catch (Exception)
- {
- }
- finally
- {
- conn.Close();
- }
- return iCurrentHighScore;
- }
- /// <summary>
- /// returns true if new highscore succesfully updated for username
- /// </summary>
- public bool SetNewHighScore(string sUsername, int iHighScore)
- {
- bool bResult = false;
- try
- {
- conn.Open();
- conn.Open();
- cmd = conn.CreateCommand();
- cmd.CommandText = "UPDATE users SET Highscore = @highscore WHERE Username = @username;";
- cmd.Parameters.AddWithValue("@username", sUsername);
- cmd.Parameters.AddWithValue("@highscore", iHighScore);
- cmd.ExecuteNonQuery();
- }
- catch (Exception)
- {
- }
- finally
- {
- bResult = true;
- conn.Close();
- }
- return bResult;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement