Staff.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using System.Windows.Threading;
  15. using System.Data.SqlClient;
  16. using System.Data;
  17. namespace HotelCalifornia
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для Staff.xaml
  21. /// </summary>
  22. public partial class Staff : Window
  23. {
  24. public Staff()
  25. {
  26. InitializeComponent();
  27. //Таймер на обновление времени
  28. DispatcherTimer timer = new DispatcherTimer();
  29. timer.Tick += new EventHandler(Update_Timer_Tick);
  30. timer.Interval = new TimeSpan(0, 0, 1);
  31. timer.Start();
  32. }
  33. //Строка подключения
  34. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True");
  35. //Вывод даты и время в textblock
  36. private void Update_Timer_Tick(object sender, EventArgs e)
  37. {
  38. timetxt.Text = DateTime.Now.ToString();
  39. }
  40. //Выход из приложения
  41. private void Close(object sender, RoutedEventArgs e)
  42. {
  43. Application.Current.Shutdown();
  44. }
  45. //Свернуть окно
  46. private void WindMin_Click(object sender, RoutedEventArgs e)
  47. {
  48. this.WindowState = WindowState.Minimized;
  49. }
  50. //Возврат к окну выбора функции
  51. private void Back(object sender, RoutedEventArgs e)
  52. {
  53. Variant variant = new Variant();
  54. variant.idadmintxt.Text = idadmintxt.Text;
  55. this.Close();
  56. variant.Show();
  57. }
  58. //Выбор строки из БД
  59. private void datastaff_SelectionChanged(object sender, SelectionChangedEventArgs e)
  60. {
  61. try
  62. {
  63. DataGrid gd = (DataGrid)sender;
  64. DataRowView rowView = gd.SelectedItem as DataRowView;
  65. if (rowView != null)
  66. {
  67. idtxt.Text = rowView["ID_Administrator"].ToString();
  68. nametxt.Text = rowView["FirstName"].ToString();
  69. familiyatxt.Text = rowView["LastName"].ToString();
  70. otchestvotxt.Text = rowView["MiddleName"].ToString();
  71. logintxt.Text = rowView["Login"].ToString();
  72. passwordtxt.Password = rowView["Password"].ToString();
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  78. }
  79. }
  80. //Добавление администратора
  81. private void Add_Click(object sender, RoutedEventArgs e)
  82. {
  83. if (familiyatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || logintxt.Text == "" || passwordtxt.Password == "")
  84. {
  85. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  86. }
  87. else
  88. {
  89. try
  90. {
  91. con.Open();
  92. SqlCommand cmd = new SqlCommand("Select * from Administrator where Login ='" + logintxt.Text + "'", con);
  93. cmd.CommandType = CommandType.Text;
  94. SqlDataAdapter adapter = new SqlDataAdapter();
  95. adapter.SelectCommand = cmd;
  96. DataSet dataSet = new DataSet();
  97. adapter.Fill(dataSet);
  98. if (dataSet.Tables[0].Rows.Count > 0)
  99. {
  100. MessageBox.Show("Такой Администратор уже создан!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  101. con.Close();
  102. }
  103. else
  104. {
  105. string sql = "INSERT INTO Administrator (LastName,FirstName,MiddleName,Login,Password) VALUES('" + familiyatxt.Text + "','" + nametxt.Text + "','" + otchestvotxt.Text + "','" + logintxt.Text + "','" + passwordtxt.Password + "')";
  106. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  107. dataAdapter.SelectCommand.ExecuteNonQuery();
  108. con.Close();
  109. showgrid();
  110. idtxt.Text = "";
  111. nametxt.Text = "";
  112. familiyatxt.Text = "";
  113. otchestvotxt.Text = "";
  114. logintxt.Text = "";
  115. passwordtxt.Password = "";
  116. MessageBox.Show("Администратор был добавлен!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. con.Close();
  122. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  123. }
  124. }
  125. }
  126. //Обновление администратора
  127. private void Update_Click(object sender, RoutedEventArgs e)
  128. {
  129. if (idtxt.Text == "")
  130. {
  131. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  132. }
  133. else if (familiyatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || logintxt.Text == "" || passwordtxt.Password == "")
  134. {
  135. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  136. }
  137. else
  138. {
  139. try
  140. {
  141. con.Open();
  142. string sql = "Update Administrator set FirstName ='" + nametxt.Text + "', LastName = '" + familiyatxt.Text + "', MiddleName = '" + otchestvotxt.Text + "', Login = '" + logintxt.Text + "', Password = '" + passwordtxt.Password + "' where ID_Administrator = '" + idtxt.Text + "'";
  143. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  144. dataAdapter.SelectCommand.ExecuteNonQuery();
  145. con.Close();
  146. idtxt.Text = "";
  147. nametxt.Text = "";
  148. familiyatxt.Text = "";
  149. otchestvotxt.Text = "";
  150. logintxt.Text = "";
  151. passwordtxt.Password = "";
  152. showgrid();
  153. MessageBox.Show("Администратор был изменен!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  154. }
  155. catch (Exception ex)
  156. {
  157. con.Close();
  158. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  159. }
  160. }
  161. }
  162. //Удаление администратора
  163. private void Delete_Click(object sender, RoutedEventArgs e)
  164. {
  165. if (idtxt.Text == "")
  166. {
  167. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  168. }
  169. else
  170. {
  171. try
  172. {
  173. con.Open();
  174. string sql = "DELETE FROM Administrator WHERE ID_Administrator = '" + idtxt.Text + "'";
  175. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  176. dataAdapter.SelectCommand.ExecuteNonQuery();
  177. con.Close();
  178. idtxt.Text = "";
  179. nametxt.Text = "";
  180. familiyatxt.Text = "";
  181. otchestvotxt.Text = "";
  182. logintxt.Text = "";
  183. passwordtxt.Password = "";
  184. showgrid();
  185. MessageBox.Show("Администратор удален!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  186. }
  187. catch (Exception ex)
  188. {
  189. con.Close();
  190. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  191. }
  192. }
  193. }
  194. //Ограничение для ввода текста
  195. #region Ограничение
  196. private void nametxt_TextChanged(object sender, TextChangedEventArgs e)
  197. {
  198. if (sender is TextBox textBox)
  199. {
  200. textBox.Text = new string
  201. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  202. }
  203. }
  204. private void familiyatxt_TextChanged(object sender, TextChangedEventArgs e)
  205. {
  206. if (sender is TextBox textBox)
  207. {
  208. textBox.Text = new string
  209. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  210. }
  211. }
  212. private void otchestvotxt_TextChanged(object sender, TextChangedEventArgs e)
  213. {
  214. if (sender is TextBox textBox)
  215. {
  216. textBox.Text = new string
  217. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  218. }
  219. }
  220. private void logintxt_TextChanged(object sender, TextChangedEventArgs e)
  221. {
  222. if (sender is TextBox textBox)
  223. {
  224. textBox.Text = new string
  225. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  226. }
  227. }
  228. private void passwordtxt_PasswordChanged(object sender, RoutedEventArgs e)
  229. {
  230. if (sender is TextBox textBox)
  231. {
  232. textBox.Text = new string
  233. (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray());
  234. }
  235. }
  236. #endregion
  237. //Фомировка данных из БД
  238. void showgrid()
  239. {
  240. try
  241. {
  242. con.Open();
  243. string sql = "SELECT * From Administrator";
  244. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  245. DataTable data = new DataTable("Administrator");
  246. dataAdapter.Fill(data);
  247. datastaff.ItemsSource = data.DefaultView;
  248. dataAdapter.Update(data);
  249. con.Close();
  250. datastaff.Columns[0].Header = "ID";
  251. datastaff.Columns[1].Header = "Фамилия";
  252. datastaff.Columns[2].Header = "Имя";
  253. datastaff.Columns[3].Header = "Отчество";
  254. datastaff.Columns[4].Header = "Логин";
  255. datastaff.Columns[5].Visibility = Visibility.Collapsed;
  256. datastaff.Columns[0].Visibility = Visibility.Collapsed;
  257. }
  258. catch (Exception ex)
  259. {
  260. con.Close();
  261. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  262. }
  263. }
  264. //Обновление
  265. private void Refresh_Click(object sender, RoutedEventArgs e)
  266. {
  267. showgrid();
  268. idtxt.Text = "";
  269. nametxt.Text = "";
  270. familiyatxt.Text = "";
  271. otchestvotxt.Text = "";
  272. logintxt.Text = "";
  273. passwordtxt.Password = "";
  274. }
  275. //Перетаскивание окна
  276. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  277. {
  278. DragMove();
  279. }
  280. //Запуск в самом начале
  281. private void Window_Loaded(object sender, RoutedEventArgs e)
  282. {
  283. showgrid();
  284. }
  285. }
  286. }