WindowAdmin.xaml.cs 13 KB

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