예제 #1
0
        //ВХОД
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            bool isLoginCorrect    = CheckLogin(Login, Alert_Login);
            bool isPasswordCorrect = CheckPassword(Password, Alert_Password);

            if (isLoginCorrect && isPasswordCorrect)
            {
                try
                {
                    // Создание подключения
                    string sqlExpression = $"SELECT * FROM USERS WHERE ([LOGIN] = '{Login.Text}')";

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        SqlCommand    command = new SqlCommand(sqlExpression, connection);
                        SqlDataReader reader  = command.ExecuteReader();

                        if (reader.HasRows) // если есть данные
                        {
                            if (reader.Read())
                            {
                                if (CheckPassword(Password.Password, reader.GetValue(2).ToString()))
                                {
                                    if (reader.GetValue(3).ToString() == "User")
                                    {
                                        UserMenu userMenu = new UserMenu();
                                        userMenu.Show();
                                        this.Close();
                                    }
                                    else
                                    {
                                        CreaterMenu createrMenu = new CreaterMenu();
                                        createrMenu.Show();
                                        this.Close();
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(Password.Password + " vs " + reader.GetValue(2).ToString());
                                }
                            }
                        }
                        else
                        {
                            MessageBox.Show("пользователь не существует");
                        }
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
예제 #2
0
        //РЕГИСТРАЦИЯ
        private void OnRegisterButton_Click(object sender, RoutedEventArgs e)
        {
            bool isLoginCorrect    = CheckLogin(NewLogin, Alert_NewLogin);
            bool isPasswordCorrect = CheckPassword(NewPassword, Alert_NewPassword);

            if (isLoginCorrect && isPasswordCorrect)
            {
                try
                {
                    // Создание подключения
                    string sqlExpression = $"INSERT USERS VALUES('{NewLogin.Text}', '{ NewPassword.Password}','{UserType.Text}')";

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        connection.Open();
                        SqlCommand command = new SqlCommand(sqlExpression, connection);
                        command.ExecuteNonQuery();
                    }

                    switch (UserType.SelectedIndex)
                    {
                    case 0:
                        UserMenu userMenu = new UserMenu();
                        userMenu.Show();
                        this.Close();
                        break;

                    case 1:
                        CreaterMenu createrMenu = new CreaterMenu();
                        createrMenu.Show();
                        this.Close();
                        break;
                    }
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 2627)
                    {
                        NewLogin.BorderBrush      = new SolidColorBrush(Colors.Orange);
                        Alert_NewLogin.Visibility = Visibility.Visible;
                        Alert_NewLogin.ToolTip    = "Пользователь с таким именем уже существует";
                    }
                    else
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }