Advertisement
Guest User

Untitled

a guest
Jun 9th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 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 BusinessLayer;
  8.  
  9. namespace ChatProject.Layers.Business
  10. {
  11. public class Connection
  12. {
  13.  
  14. private static string sql_query = null;
  15. private static string server_address = "ise172.ise.bgu.ac.il,1433\\DB_LAB";
  16. //private static string server_address = "LAPTOP-C1281MRK\\SQLEXPRESS";
  17. private static string database_name = "MS3";
  18. private static string user_name = "publicUser";
  19. private static string password = "isANerd";
  20. private static string connection_string = $"Data Source={server_address};Initial Catalog={database_name};User ID = {user_name};Password={password};";
  21. // private static string connection_string = $"Data Source={server_address};Initial Catalog={database_name};Persist Security Info=False;";
  22. SqlConnection connection = new SqlConnection(connection_string);
  23. SqlCommand command;
  24. SqlDataReader reader;
  25.  
  26. public Connection() { }
  27.  
  28. //insert a new user to the database
  29. public void writeUserToDatabase(int groupid, string username, string password)
  30. {//"SET IDENTITY_INSERT dbo.Users ON" +
  31. sql_query = $" INSERT INTO [dbo].[Users] (Group_Id,Nickname,Password) VALUES ('{groupid}','{username}','{password}')";
  32. command = new SqlCommand(sql_query, connection);
  33. try
  34. {
  35. connection.Open();
  36. command.ExecuteNonQuery();
  37. connection.Close();
  38. }
  39. catch (Exception e)
  40. {
  41. connection.Close();
  42. throw new Exception(e.Message);
  43. }
  44. }
  45.  
  46. //edit the message content by user id.
  47. public void editMsgById(int userid, string msg)
  48. {
  49. sql_query = $"UPDATE dbo.Messages SET Body = '{msg}' WHERE User_Id = {userid}";
  50. command = new SqlCommand(sql_query, connection);
  51. try
  52. {
  53. connection.Open();
  54. command.ExecuteNonQuery();
  55. connection.Close();
  56. }
  57. catch (Exception e)
  58. {
  59. connection.Close();
  60. throw new Exception(e.Message);
  61. }
  62. }
  63.  
  64. //check if the user exist by his nick, group id, password.
  65. public bool userExist(string nickname, int groupid, string password)
  66. {
  67. bool isExist = false;
  68. sql_query = $"SELECT IIF(Nickname='{nickname}' AND Group_Id='{groupid}' AND Password='{password}',1,0) as excist FROM dbo.Users;";
  69. command = new SqlCommand(sql_query, connection);
  70. try
  71. {
  72. connection.Open();
  73. reader = command.ExecuteReader();
  74. }
  75. catch (Exception e)
  76. {
  77. reader.Close();
  78. connection.Close();
  79. throw new Exception(e.Message);
  80. }
  81. while (reader.Read())
  82. {
  83. if (reader.GetInt32(0) == 1)
  84. isExist = true;
  85. }
  86. reader.Close();
  87. connection.Close();
  88. return isExist;
  89. }
  90.  
  91. //return the user by his nick, group id and password.
  92. public User getUser(string nickname, int groupid, string password)
  93. {
  94. User user = null;
  95. try
  96. {
  97. if (userExist(nickname, groupid, password))
  98. {
  99. connection.Open();
  100. sql_query = $"SELECT * FROM Users WHERE Nickname='{nickname}' AND Group_Id='{groupid}' AND Password='{password}'";
  101. command = new SqlCommand(sql_query, connection);
  102. reader = command.ExecuteReader();
  103. if (reader.Read())
  104. user = new User(reader.GetString(2), reader.GetInt32(1).ToString(), reader.GetString(3));
  105. reader.Close();
  106. connection.Close();
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. reader.Close();
  112. connection.Close();
  113. throw new Exception(e.Message);
  114. }
  115. return user;
  116. }
  117.  
  118. //return the user from the database by his id.
  119. public User userFromId(int id)
  120. {
  121. User user = null;
  122. sql_query = $"SELECT * FROM Users WHERE Id = '{id}'";
  123. command = new SqlCommand(sql_query, connection);
  124. try
  125. {
  126. connection.Open();
  127. reader = command.ExecuteReader();
  128. if (reader.Read())
  129. user = new User(reader.GetString(2), reader.GetString(1), reader.GetString(3));
  130. reader.Close();
  131. connection.Close();
  132. }
  133. catch (Exception e)
  134. {
  135. reader.Close();
  136. connection.Close();
  137. throw new Exception(e.Message);
  138. }
  139. return user;
  140. }
  141.  
  142. //retrieve last 200 msg.
  143. public List<Message> retrieveLastMsg() {
  144. int i = 0;
  145. List<Message> messages = new List<Message>();
  146. sql_query = "SELECT * FROM Messages INNER JOIN Users ON Messages.User_Id = Users.Id ORDER BY Messages.SendTime";
  147. command = new SqlCommand(sql_query, connection);
  148. try
  149. {
  150. connection.Open();
  151. reader = command.ExecuteReader();
  152. while (reader.Read() | i < 200)
  153. {
  154. Guid guid = Guid.Parse(reader.GetValue(0).ToString());
  155. string username = reader.GetString(6);
  156. DateTime date = reader.GetDateTime(2).ToLocalTime();
  157. string content = reader.GetString(3).Trim();
  158. string groupid = reader.GetInt32(5).ToString();
  159.  
  160. //the method should change by changes in the const requirements.
  161. Message msg = new Message(guid, username, date, content, groupid);
  162. messages.Add(msg);
  163. i++;
  164. }
  165. reader.Close();
  166. connection.Close();
  167. }
  168. catch (Exception e)
  169. {
  170. reader.Close();
  171. connection.Close();
  172. throw new Exception(e.Message);
  173. }
  174. return messages;
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement