Employee.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace proba
  12. {
  13. public partial class Employee : Form
  14. {
  15. string id_employee;
  16. string surname;
  17. string name;
  18. string patronymic;
  19. string pol;
  20. string date_of_birth;
  21. string phone;
  22. string login;
  23. bool save;
  24. DataBase dataBase = new DataBase();
  25. public Employee()
  26. {
  27. InitializeComponent();
  28. }
  29. private void CreateColumns()
  30. {
  31. dataGridView1.Columns.Add("surname", "Фамилия");
  32. dataGridView1.Columns.Add("name", "Имя");
  33. dataGridView1.Columns.Add("patronymic", "Отчество");
  34. dataGridView1.Columns.Add("pol", "Пол");
  35. dataGridView1.Columns.Add("date_of_birth", "Дата рождения");
  36. dataGridView1.Columns.Add("phone", "Телефон");
  37. dataGridView1.Columns.Add("login", "Логин");
  38. dataGridView1.Columns.Add("", String.Empty);
  39. }
  40. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  41. {
  42. dgw.Rows.Add(record.GetString(0), record.GetString(1), record.GetString(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6), RowState.ModifiedNew);
  43. }
  44. private void RefresDataGird(DataGridView dgw)
  45. {
  46. dgw.Rows.Clear();
  47. dataGridView1.Columns[7].Visible = false;
  48. string queryString = $"Select E.surname, E.name, E.patronymic, P.pol, E.date_of_birth, E.phone, E.login From Employee E inner join Pol P ON E.id_pol = P.id_pol inner join Roles R ON E.id_role = R.id_role WHERE Not R.role = 'Администратор'";
  49. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  50. dataBase.openConnection();
  51. SqlDataReader reader = command.ExecuteReader();
  52. while (reader.Read())
  53. {
  54. ReadSingleRow(dgw, reader);
  55. }
  56. reader.Close();
  57. }
  58. public void global_FormClosed(object sender, EventArgs e)
  59. {
  60. Application.Exit();
  61. }
  62. private void Search(DataGridView dgw)
  63. {
  64. dgw.Rows.Clear();
  65. string searchString = $"Select E.surname, E.name, E.patronymic, P.pol, E.date_of_birth, E.phone, E.login From Employee E inner join Pol P ON E.id_pol = P.id_pol inner join Roles R ON E.id_role = R.id_role WHERE Not R.role = 'Администратор' and E.login like '%" + StrokaSearch.Text + "%'";
  66. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  67. dataBase.openConnection();
  68. SqlDataReader read = com.ExecuteReader();
  69. while (read.Read())
  70. {
  71. ReadSingleRow(dgw, read);
  72. }
  73. read.Close();
  74. }
  75. private void deleteRow()
  76. {
  77. int index = dataGridView1.CurrentCell.RowIndex;
  78. dataGridView1.Rows[index].Visible = false;
  79. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  80. {
  81. dataGridView1.Rows[index].Cells[7].Value = RowState.Deleted;
  82. return;
  83. }
  84. dataGridView1.Rows[index].Cells[7].Value = RowState.Deleted;
  85. id_employee = null;
  86. }
  87. new private void Update()
  88. {
  89. dataBase.openConnection();
  90. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  91. {
  92. var rowState = (RowState)dataGridView1.Rows[index].Cells[7].Value;
  93. if (rowState == RowState.Existed)
  94. continue;
  95. if (rowState == RowState.Deleted)
  96. {
  97. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_employee From Employee WHERE surname = '{dataGridView1.Rows[index].Cells[0].Value}'", dataBase.GetConnection());
  98. var id = sqlCommand_product.ExecuteScalar().ToString();
  99. var deleteQuery = $"delete from Employee where id_employee = {id}";
  100. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  101. command.ExecuteNonQuery();
  102. }
  103. }
  104. dataBase.closeConnection();
  105. }
  106. private void Employee_Load(object sender, EventArgs e)
  107. {
  108. CreateColumns();
  109. RefresDataGird(dataGridView1);
  110. }
  111. private void button_back_Click(object sender, EventArgs e)
  112. {
  113. this.Hide();
  114. MainMenu mainMenu_Admin = new MainMenu();
  115. mainMenu_Admin.ShowDialog();
  116. }
  117. private void StrokaSearch_TextChanged(object sender, EventArgs e)
  118. {
  119. Search(dataGridView1);
  120. }
  121. private void button_delete_Click(object sender, EventArgs e)
  122. {
  123. dataBase.openConnection();
  124. string admin = $"SELECT S.id_sale From Employee E inner join Sales S ON E.id_employee = S.id_employee WHERE E.surname = '{surname}'";
  125. SqlDataAdapter sda = new SqlDataAdapter(admin, dataBase.GetConnection());
  126. DataTable dtbl = new DataTable();
  127. sda.Fill(dtbl);
  128. if (dtbl.Rows.Count < 1)
  129. {
  130. if (id_employee == null)
  131. {
  132. MessageBox.Show("Не выделена запись для удаления!!!");
  133. }
  134. else
  135. {
  136. deleteRow();
  137. save = true;
  138. }
  139. }
  140. else
  141. {
  142. MessageBox.Show("Нельзя удалить запись, на которую есть ссылка!!!");
  143. }
  144. dataBase.closeConnection();
  145. }
  146. private void buttonSave_Click(object sender, EventArgs e)
  147. {
  148. if (save == true)
  149. {
  150. Update();
  151. save = false;
  152. }
  153. }
  154. private void button_insert_Click(object sender, EventArgs e)
  155. {
  156. if (id_employee == null)
  157. {
  158. MessageBox.Show("Не выделена строчка для изменения");
  159. }
  160. else
  161. {
  162. this.Hide();
  163. EmployeeUpdate employeeUpdate = new EmployeeUpdate(id_employee, surname, name, patronymic, pol, date_of_birth, phone, login);
  164. employeeUpdate.ShowDialog();
  165. }
  166. }
  167. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  168. {
  169. var selectedRow = e.RowIndex;
  170. if (e.RowIndex >= 0)
  171. {
  172. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  173. dataBase.openConnection();
  174. surname = row.Cells[0].Value.ToString();
  175. name = row.Cells[1].Value.ToString();
  176. patronymic = row.Cells[2].Value.ToString();
  177. pol = row.Cells[3].Value.ToString();
  178. date_of_birth = row.Cells[4].Value.ToString();
  179. phone = row.Cells[5].Value.ToString();
  180. login = row.Cells[6].Value.ToString();
  181. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_employee From Employee WHERE surname = '{surname}'", dataBase.GetConnection());
  182. id_employee = sqlCommand_product.ExecuteScalar().ToString();
  183. dataBase.closeConnection();
  184. }
  185. }
  186. private void pictureBox2_Click(object sender, EventArgs e)
  187. {
  188. RefresDataGird(dataGridView1);
  189. save = false;
  190. }
  191. private void button_add_Click(object sender, EventArgs e)
  192. {
  193. this.Hide();
  194. EmployeeAdd employeeAdd = new EmployeeAdd();
  195. employeeAdd.ShowDialog();
  196. }
  197. }
  198. }