using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Data.SqlClient; using System.Data; namespace SkladProject { /// /// Логика взаимодействия для WindowAdmin.xaml /// public partial class WindowAdmin : Window { public WindowAdmin() { InitializeComponent(); } SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=praktika;Integrated Security=True"); private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { DragMove(); } private void datastaff_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { DataGrid gd = (DataGrid)sender; DataRowView rowView = gd.SelectedItem as DataRowView; if (rowView != null) { idusertxt.Text = rowView["ID_User"].ToString(); nametxt.Text = rowView["FirstName"].ToString(); familiyatxt.Text = rowView["LastName"].ToString(); otchestvotxt.Text = rowView["MiddleName"].ToString(); logintxt.Text = rowView["Login"].ToString(); passwordtxt.Password = rowView["Password"].ToString(); rolecombo.Text = rowView["NameRole"].ToString(); } } catch (Exception ex) { MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } private void Back(object sender, RoutedEventArgs e) { MainWindow mainWindow = new MainWindow(); mainWindow.Show(); this.Close(); } private void WinMin_Click(object sender, RoutedEventArgs e) { this.WindowState = WindowState.Minimized; } void fillcombo() { try { rolecombo.Items.Clear(); con.Open(); SqlCommand sql = con.CreateCommand(); sql.CommandType = CommandType.Text; sql.CommandText = "Select NameRole from Roles"; sql.ExecuteNonQuery(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(sql); da.Fill(dt); foreach (DataRow dr in dt.Rows) { rolecombo.Items.Add(dr["NameRole"].ToString()); } con.Close(); } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } void showgrid() { try { con.Open(); string sql = "SELECT ID_User,LastName, FirstName, MiddleName, Login, Password, [NameRole] From Users inner join Roles on Users.ID_RoleUser = Roles.ID_Role"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con); DataTable data = new DataTable("Users"); dataAdapter.Fill(data); datastaff.ItemsSource = data.DefaultView; dataAdapter.Update(data); con.Close(); datastaff.Columns[0].Header = "ID"; datastaff.Columns[1].Header = "Фамилия"; datastaff.Columns[2].Header = "Имя"; datastaff.Columns[3].Header = "Отчество"; datastaff.Columns[4].Header = "Логин"; datastaff.Columns[6].Header = "Роль"; datastaff.Columns[5].Visibility = Visibility.Collapsed; datastaff.Columns[0].Visibility = Visibility.Collapsed; } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } private void Add_Click(object sender, RoutedEventArgs e) { if (familiyatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || logintxt.Text == "" || passwordtxt.Password == "" || rolecombo.Text == "") { MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else { try { con.Open(); SqlCommand cmd = new SqlCommand("Select * from Users where Login ='" + logintxt.Text + "'", con); cmd.CommandType = CommandType.Text; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = cmd; DataSet dataSet = new DataSet(); adapter.Fill(dataSet); if (dataSet.Tables[0].Rows.Count > 0) { MessageBox.Show("Пользователь с таким логиным уже создан!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information); con.Close(); } else { string idrole = ""; if (rolecombo.Text == "Администратор") idrole = "1"; else idrole = "2"; 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() + "')"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con); dataAdapter.SelectCommand.ExecuteNonQuery(); con.Close(); showgrid(); idusertxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Password = ""; rolecombo.Text = ""; } } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void Update_Click(object sender, RoutedEventArgs e) { if(idusertxt.Text == "") { MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else if (familiyatxt.Text == "" || nametxt.Text == "" || otchestvotxt.Text == "" || logintxt.Text == "" || passwordtxt.Password == "" || rolecombo.Text == "") { MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else { try { con.Open(); string idrole = ""; if (rolecombo.Text == "Администратор") idrole = "1"; else idrole = "2"; 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 + "'"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con); dataAdapter.SelectCommand.ExecuteNonQuery(); con.Close(); idusertxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Password = ""; rolecombo.Text = ""; showgrid(); } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void Delete_Click(object sender, RoutedEventArgs e) { if (idusertxt.Text == "") { MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information); } else { try { con.Open(); string sql = "DELETE FROM Users WHERE ID_User = '" + idusertxt.Text + "'"; SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con); dataAdapter.SelectCommand.ExecuteNonQuery(); con.Close(); idusertxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Password = ""; rolecombo.Text = ""; showgrid(); } catch (Exception ex) { con.Close(); MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void Window_Loaded(object sender, RoutedEventArgs e) { fillcombo(); showgrid(); } private void Refresh_Click(object sender, RoutedEventArgs e) { idusertxt.Text = ""; nametxt.Text = ""; familiyatxt.Text = ""; otchestvotxt.Text = ""; logintxt.Text = ""; passwordtxt.Password = ""; rolecombo.Text = ""; showgrid(); } private void Exit_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } private void familiyatxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray()); } } private void nametxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray()); } } private void otchestvotxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray()); } } private void logintxt_TextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray()); } } private void passwordtxt_PasswordChanged(object sender, RoutedEventArgs e) { if (sender is TextBox textBox) { textBox.Text = new string (textBox.Text.Where(ch => (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')).ToArray()); } } } }