Guest User

Untitled

a guest
Nov 20th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9.  
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14.  
  15. EmployeeDbase dbase = new EmployeeDbase("un", "pword", "12121", "2332");
  16. dbase.Open();
  17. EmployeeDbase.Department systems = dbase.GetDepartmentByName("Systems");
  18. EmployeeDbase.Employee brian = dbase.CreateNewEmployee("Brian", systems, DateTime.Parse("10/01/1987"));
  19. brian.name = "Brian Willis";
  20. brian.Save();
  21.  
  22.  
  23. }
  24.  
  25. class EmployeeDbase
  26. {
  27. string db_username;
  28. string db_pword;
  29. string db_address;
  30. string db_name;
  31. public SqlConnection connection;
  32. public EmployeeDbase db;
  33.  
  34. public EmployeeDbase(string db_username, string db_pword, string db_address, string db_name)
  35. {
  36. this.db_username = db_username;
  37. this.db_pword = db_pword;
  38. this.db_address = db_address;
  39. this.db_name = db_name;
  40. connection = new SqlConnection("user id=" + db_username +
  41. ";password=" + db_pword +
  42. ";server=" + db_address +
  43. ";database=" + db_name);
  44. EmployeeDbase db = this;
  45. }
  46. public void Open()
  47. {
  48. connection.Open();
  49. }
  50. public void Close()
  51. {
  52. connection.Close();
  53. }
  54. public SqlConnection GetConnection()
  55. {
  56. if (this.connection.State.Equals(ConnectionState.Closed) || this.connection.State.Equals(ConnectionState.Broken))
  57. {
  58. this.connection.Open();
  59. return this.connection;
  60. }
  61. else
  62. {
  63. return this.connection;
  64. }
  65. }
  66. public Employee GetEmployeeByID(int id)
  67. {
  68. SqlConnection connection = GetConnection();
  69. SqlCommand myCommand = new SqlCommand("SELECT employee_name, employee_dob, department_id, employee_id " +
  70. "FROM employees WHERE employee_id = '" + id + "'", connection);
  71. myCommand.ExecuteNonQuery();
  72. SqlDataReader reader = myCommand.ExecuteReader();
  73. Employee employee;
  74. Department department = GetDepartmentByID(reader.GetInt32(2));
  75. employee = new Employee(reader.GetString(0), department, reader.GetDateTime(1), this.db);
  76. employee.id = reader.GetInt16(3);
  77. return employee;
  78. }
  79. public Employee CreateNewEmployee(string name, Department department, DateTime dob)
  80. {
  81. Employee employee = new Employee(name, department, dob, db);
  82. return employee;
  83. }
  84. public Department CreateNewDepartment(string department_name)
  85. {
  86. Department department = new Department(department_name, db);
  87. return department;
  88. }
  89. public Department GetDepartmentByName(string department_name)
  90. {
  91. Department department = new Department(department_name,this.db);
  92. SqlConnection connection = db.GetConnection();
  93. SqlCommand myCommand = new SqlCommand("SELECT department_id, department_name FROM departments WHERE " +
  94. "department_name = '" + department_name + "'",connection);
  95. SqlDataReader reader = myCommand.ExecuteReader();
  96. department.id = reader.GetInt32(0);
  97. department.name = reader.GetString(1);
  98. return department;
  99. }
  100. public Department GetDepartmentByID(int department_id)
  101. {
  102. SqlConnection connection = db.GetConnection();
  103. SqlCommand myCommand = new SqlCommand("SELECT department_id, department_name FROM departments WHERE " +
  104. "department_id = '" + department_id + "'", connection);
  105. SqlDataReader reader = myCommand.ExecuteReader();
  106. Department department = new Department(reader.GetString(1), db);
  107. department.id = reader.GetInt32(0);
  108. return department;
  109. }
  110. public class Employee : IEmployeeCRUD
  111. {
  112. public int id;
  113. public string name;
  114. public Department department;
  115. public DateTime dob;
  116. public EmployeeDbase db;
  117.  
  118. public Employee(string name, Department department, DateTime dob, EmployeeDbase db)
  119. {
  120. this.name = name;
  121. this.department = department;
  122. this.dob = dob;
  123. this.db = db;
  124. }
  125. public void Save()
  126. {
  127. SqlConnection connection = db.GetConnection();
  128. if (this.id == null)
  129. {
  130. SqlCommand insertCommand = new SqlCommand("INSERT INTO employees (employee_name, employee_dob, department_id) " +
  131. "Values ('" + this.name + "', '" + this.dob.Date.ToString() + "', '" +
  132. this.department.ToString() + "')", connection);
  133. int new_id = (Int32)insertCommand.ExecuteScalar();
  134. this.id = new_id;
  135. }
  136. else
  137. {
  138. SqlCommand updateCommand = new SqlCommand("UPDATE employees SET employee_name = '" + name +
  139. "', department_id = '" + department.id + "', employee_dob = '" + dob.Date.ToString() + "'" +
  140. " WHERE employee_ID = '" + this.id + "'", connection);
  141. updateCommand.ExecuteNonQuery();
  142. }
  143. }
  144. public void Delete()
  145. {
  146. SqlConnection connection = db.GetConnection();
  147. SqlCommand deleteCommand = new SqlCommand("DELETE FROM employees WHERE employee_id = '" + this.id + "'", connection);
  148. deleteCommand.ExecuteNonQuery();
  149. }
  150. }
  151. public class Department : IEmployeeCRUD
  152. {
  153. public int id;
  154. public string name;
  155. public EmployeeDbase db;
  156. public Department(string name, EmployeeDbase db)
  157. {
  158. this.name = name;
  159. this.db = db;
  160. }
  161. public void save()
  162. {
  163. SqlConnection connection = db.GetConnection();
  164. if (this.id == null)
  165. {
  166. SqlCommand insertCommand = new SqlCommand("INSERT INTO departments (department_id, department_name) " +
  167. "Values ('" + this.id.ToString() + "', " + this.name + "')", connection);
  168. int new_id = (Int32)insertCommand.ExecuteScalar();
  169. this.id = new_id;
  170. }
  171. else
  172. {
  173. SqlCommand updateCommand = new SqlCommand("UPDATE departments SET department_name = '" + name +
  174. "' WHERE department_id = '" + this.id + "'", connection);
  175. updateCommand.ExecuteNonQuery();
  176. }
  177. }
  178. public void Delete()
  179. {
  180. SqlConnection connection = db.GetConnection();
  181. SqlCommand deleteCommand = new SqlCommand("DELETE FROM departments WHERE department_id = '" + this.id + "'", connection);
  182. deleteCommand.ExecuteNonQuery();
  183. }
  184. }
  185. interface IEmployeeCRUD
  186. {
  187. void Save();
  188. void Delete();
  189. }
  190. }
  191. }
Add Comment
Please, Sign In to add comment