Advertisement
Guest User

Untitled

a guest
May 15th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data.MySqlClient;
  7. using System.Windows;
  8.  
  9. namespace PATPeriode3
  10. {
  11. public class Database
  12. {
  13. private static readonly string databaseNamestring = "Server=localhost; Database=patperiode3; Uid = root; Pwd=root;";
  14. private static MySqlConnection conn = new MySqlConnection(databaseNamestring);
  15. private static MySqlCommand cmd;
  16. /// <summary>
  17. /// returns true if login succeeds
  18. /// </summary>
  19. public bool CheckLogin(string sUsername, string sPassword)
  20. {
  21. bool bResult = false;
  22.  
  23. try
  24. {
  25. conn.Open();
  26. cmd = conn.CreateCommand();
  27. cmd.CommandText = "SELECT * FROM user WHERE Username = @username AND Password = @password;";
  28. cmd.Parameters.AddWithValue("@username", sUsername);
  29. cmd.Parameters.AddWithValue("@password", sPassword);
  30. MySqlDataReader dr = cmd.ExecuteReader();
  31. int count = 0;
  32.  
  33. while (dr.Read())
  34. {
  35. count++;
  36. }
  37.  
  38. if (count == 1)
  39. {
  40. bResult = true;
  41. }
  42. }
  43. catch (Exception)
  44. {
  45. }
  46. finally
  47. {
  48. conn.Close();
  49. }
  50.  
  51. return bResult;
  52. }
  53.  
  54. /// <summary>
  55. /// returns true if new user is added succesfully
  56. /// </summary>
  57. public bool NewUser(string sUsername, string sPassword)
  58. {
  59. bool bResult = false;
  60. try
  61. {
  62. conn.Open();
  63. cmd = conn.CreateCommand();
  64. cmd.CommandText = "INSERT INTO user(Username, Password, Highscore) VALUES (@username, @password, 0);";
  65. cmd.Parameters.AddWithValue("@username", sUsername);
  66. cmd.Parameters.AddWithValue("@password", sPassword);
  67. cmd.ExecuteNonQuery();
  68. bResult = true;
  69. conn.Close();
  70.  
  71. }
  72. catch (Exception)
  73. {
  74. }
  75.  
  76. return bResult;
  77. }
  78.  
  79. /// <summary>
  80. /// Gets Highscore from username
  81. /// </summary>
  82. public int GetCurrentHighScore(string sUsername)
  83. {
  84. int iCurrentHighScore = 0;
  85. try
  86. {
  87. conn.Open();
  88. cmd = conn.CreateCommand();
  89. cmd.CommandText = "SELECT Highscore FROM user WHERE Username = @username;";
  90. cmd.Parameters.AddWithValue("@username", sUsername);
  91. MySqlDataReader dr = cmd.ExecuteReader();
  92.  
  93. iCurrentHighScore = dr.GetInt32(0);
  94. }
  95. catch (Exception)
  96. {
  97. }
  98. finally
  99. {
  100. conn.Close();
  101. }
  102. return iCurrentHighScore;
  103. }
  104.  
  105. /// <summary>
  106. /// returns true if new highscore succesfully updated for username
  107. /// </summary>
  108. public bool SetNewHighScore(string sUsername, int iHighScore)
  109. {
  110. bool bResult = false;
  111. try
  112. {
  113. conn.Open();
  114. conn.Open();
  115. cmd = conn.CreateCommand();
  116. cmd.CommandText = "UPDATE users SET Highscore = @highscore WHERE Username = @username;";
  117. cmd.Parameters.AddWithValue("@username", sUsername);
  118. cmd.Parameters.AddWithValue("@highscore", iHighScore);
  119. cmd.ExecuteNonQuery();
  120.  
  121. }
  122. catch (Exception)
  123. {
  124. }
  125. finally
  126. {
  127. bResult = true;
  128. conn.Close();
  129. }
  130. return bResult;
  131. }
  132.  
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement