Advertisement
gladyssann

UserDataAccess.cs

Jan 5th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 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.Data.Sql;
  8. using OT_lib.Entity;
  9. using OT_lib;
  10.  
  11. namespace OT_lib.DataAccess
  12. {
  13. public class UsersDataAccess {
  14.  
  15. sqlConnection _connection = null;
  16.  
  17. public UsersDataAccess(string key)
  18. {
  19. _connection = new sqlConnection(key);
  20. }
  21.  
  22. public int CreateUser(UserEntity user){
  23.  
  24. SqlConnection oCon = null;
  25. SqlCommand oCmd = null;
  26. try
  27. {
  28.  
  29. oCon = _connection.openConn();
  30. string sSql = string.Format("INSERT INTO Users VALUES ('{0}','{1}','{2}')", user.username, user.email, user.password);
  31. oCmd = new SqlCommand(sSql, oCon);
  32.  
  33. oCmd.ExecuteNonQuery();
  34.  
  35. }
  36. catch (Exception ex)
  37. {
  38.  
  39. throw ex;
  40. }
  41. finally
  42. {
  43. //clean
  44. oCon.Close();
  45. oCmd.Dispose();
  46.  
  47. oCmd = null;
  48. oCon = null;
  49. }
  50. return 0;
  51. }
  52.  
  53. public int EditUser(UserEntity userEntity) {
  54.  
  55. SqlConnection oCon = null;
  56. SqlCommand oCmd = null;
  57.  
  58. try
  59. {
  60. oCon = _connection.openConn();
  61. oCmd.Parameters.Add(new SqlParameter("@usr_Id", userEntity.usr_Id));
  62. oCmd.Parameters.Add(new SqlParameter("@username", userEntity.username));
  63. oCmd.Parameters.Add(new SqlParameter("@email", userEntity.email));
  64. oCmd.Parameters.Add(new SqlParameter("@password", userEntity.password));
  65.  
  66. string sqlCmd = string.Format("UPDATE Users SET username = @username, email = @email, @password = password WHERE usr_Id = @usr_Id");
  67. oCmd.ExecuteNonQuery();
  68.  
  69. }
  70. catch (Exception ex)
  71. {
  72.  
  73. throw ex;
  74. }
  75. finally
  76. {
  77. //clean
  78. oCon.Close();
  79. oCmd.Dispose();
  80.  
  81. oCmd = null;
  82. oCon = null;
  83. }
  84.  
  85. return 0;
  86. }
  87.  
  88. public int GetUserId() {
  89.  
  90.  
  91. return 0;
  92. }
  93.  
  94. public List<UserEntity> GetAllUsers(){
  95.  
  96. SqlConnection oCon = null;
  97. SqlCommand oCmd = null;
  98. List<UserEntity> Users = null;
  99. SqlDataReader dr;
  100.  
  101. try
  102. {
  103. oCon = _connection.openConn();
  104. string sql = string.Format("SELECT * FROM Users");
  105. oCmd.CommandText = sql;
  106. dr = oCmd.ExecuteReader();
  107.  
  108. if (dr.HasRows)
  109. {
  110. Users = new List<UserEntity>();
  111.  
  112. while (dr.Read())
  113. {
  114. UserEntity user = new UserEntity();
  115. user.email = dr["email"].ToString();
  116. user.password = dr["password"].ToString();
  117. user.username = dr["username"].ToString();
  118. user.usr_Id = Convert.ToInt32(dr["usr_Id"]);
  119. Users.Add(user);
  120. }
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. throw ex;
  126. }
  127.  
  128. return Users;
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement