Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 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 System.Data.SqlClient;
  7. using System.Configuration;
  8. using System.Data.Sql;
  9. using System.Data;
  10.  
  11. namespace KillerApp
  12. {
  13. class Database
  14. {
  15. private static string connectionString = "Data Source=127.0.0.1;Initial Catalog=KillerApp;User ID=PT12admin;Password=jestin123";
  16. private SqlConnection connection = new SqlConnection(connectionString);
  17.  
  18. /// <summary>
  19. /// Method for checking correct login credentials (logging in)
  20. /// </summary>
  21. /// <param name="userName"></param>
  22. /// <param name="password"></param>
  23. /// <returns></returns>
  24. public bool Login(string userName, string password)
  25. {
  26. //Query to check if the filled-in username corresponds with the filled-in password
  27. bool correctLogin = false;
  28. string query = "SELECT COUNT(*) as counter, User_ID FROM USERS WHERE UserName='" + userName + "' AND Password='" + password + "' GROUP BY User_ID";
  29. SqlDataAdapter sda = new SqlDataAdapter(query, connection);
  30. DataTable dt = new DataTable();
  31. sda.Fill(dt);
  32. if (dt.Rows[0][0].ToString() == "1")
  33. {
  34. int _userID = Convert.ToInt32(dt.Rows[0][1]);
  35. User newUser = new User(_userID, userName);
  36. Globaal.NewuserLoggedin(newUser);
  37. correctLogin = true;
  38. }
  39. else
  40. {
  41. correctLogin = false;
  42. }
  43.  
  44. return correctLogin;
  45.  
  46. }
  47.  
  48. /// <summary>
  49. /// Method for registering a new user
  50. /// </summary>
  51. /// <param name="FirstName"></param>
  52. /// <param name="Infix"></param>
  53. /// <param name="LastName"></param>
  54. /// <param name="Street"></param>
  55. /// <param name="HouseNumber"></param>
  56. /// <param name="City"></param>
  57. /// <param name="ZipCode"></param>
  58. /// <param name="Province"></param>
  59. /// <param name="UserName"></param>
  60. /// <param name="Password"></param>
  61. /// <param name="Email"></param>
  62. /// <param name="CountryCode"></param>
  63. public void AddUser(string FirstName, string Infix, string LastName, string Street, int HouseNumber, string City, string ZipCode, string Province, string UserName, string Password, string Email, string CountryCode)
  64. {
  65.  
  66. using (connection)
  67. {
  68. //Query to insert all information needed to create a user
  69. string query = "insert into USERS (City, Email, FirstName, HouseNumber, Infix, LastName, Password, Province, Street, UserName, ZipCode, CountryCode) values(@City, @Email, @FirstName, @HouseNumber, @Infix, @LastName, @Password, @Province, @Street, @UserName, @ZipCode, @CountryCode)";
  70. connection.Open();
  71. using (SqlCommand cmd = new SqlCommand(query, connection))
  72. {
  73. cmd.Parameters.AddWithValue("@City", City);
  74. cmd.Parameters.AddWithValue("@Email", Email);
  75. cmd.Parameters.AddWithValue("@FirstName", FirstName);
  76. cmd.Parameters.AddWithValue("@HouseNumber", HouseNumber);
  77. cmd.Parameters.AddWithValue("@Infix", Infix);
  78. cmd.Parameters.AddWithValue("@LastName", LastName);
  79. cmd.Parameters.AddWithValue("@Password", Password);
  80. cmd.Parameters.AddWithValue("@Province", Province);
  81. cmd.Parameters.AddWithValue("@Street", Street);
  82. cmd.Parameters.AddWithValue("@UserName", UserName);
  83. cmd.Parameters.AddWithValue("@ZipCode", ZipCode);
  84. cmd.Parameters.AddWithValue("@CountryCode", CountryCode);
  85.  
  86.  
  87. cmd.ExecuteNonQuery();
  88.  
  89. connection.Close();
  90. }
  91. }
  92.  
  93. }
  94.  
  95. /// <summary>
  96. /// Writes the created orderlist to the database
  97. /// </summary>
  98. /// <param name="OrderListProduct_ID"></param>
  99. /// <param name="OrderListUser_ID"></param>
  100. public void WriteOrderlistToDatabase(int OrderListProduct_ID, int OrderListUser_ID)
  101. {
  102. string connectionString = "Data Source=127.0.0.1;Initial Catalog=KillerApp;User ID=PT12admin;Password=jestin123";
  103. SqlConnection connection = new SqlConnection(connectionString);
  104. using (connection)
  105. {
  106. //Query to insert Product_ID and User_ID into junction table ORDERLISTS
  107. string query = "insert into ORDERLISTS (OrderListProduct_ID, OrderListUser_ID) values (@OrderListProduct_ID, @OrderListUser_ID)";
  108. connection.Open();
  109. using (SqlCommand cmd = new SqlCommand(query, connection))
  110. {
  111. cmd.Parameters.AddWithValue("@OrderListProduct_ID", OrderListProduct_ID);
  112. cmd.Parameters.AddWithValue("@OrderListUser_ID", OrderListUser_ID);
  113.  
  114. cmd.ExecuteNonQuery();
  115. connection.Close();
  116. }
  117. }
  118. }
  119.  
  120. /// <summary>
  121. /// Method for getting the correct country code / language
  122. /// </summary>
  123. /// <returns></returns>
  124. public List<Country> GetCountry()
  125. {
  126. //Query to get country information from the given fields
  127. List<Country> countries = new List<Country>();
  128. string query = "SELECT CountryCode, Dutch, German, English FROM COUNTRIES";
  129.  
  130. connection.Open();
  131.  
  132. SqlCommand cmd = new SqlCommand(query, connection);
  133. using (SqlDataReader reader = cmd.ExecuteReader())
  134. {
  135. while (reader.Read())
  136. {
  137. countries.Add(new Country(reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)));
  138. }
  139. }
  140. connection.Close();
  141.  
  142. return countries;
  143. }
  144.  
  145. /// <summary>
  146. /// Method to get the right product with it's correct information
  147. /// </summary>
  148. /// <param name="BarCode"></param>
  149. /// <returns></returns>
  150. public Product GetProduct(string BarCode)
  151. {
  152. //Query to grab the corresponding product with the filled-in BarCode
  153. string query = "SELECT Product_ID, ProductName, BarCode, Price, Category FROM PRODUCTS WHERE BarCode = " + BarCode;
  154.  
  155. connection.Open();
  156.  
  157. try
  158. {
  159. SqlCommand cmd = new SqlCommand(query, connection);
  160. using (SqlDataReader reader = cmd.ExecuteReader())
  161. {
  162. if (reader.Read())
  163. {
  164. return new Product(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetDecimal(3), reader.GetString(4));
  165.  
  166. }
  167. }
  168. }
  169. finally
  170. {
  171. connection.Close();
  172. }
  173.  
  174. return null;
  175. }
  176.  
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement