Staff.xaml.cs 12 KB

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