Esempio n. 1
0
        // Попытка войти в систему
        private void enterButton_Click(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection())
            {
                string userName = userNameTextBox.Text;
                string password = passwordTextBox.Text;

                if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(password))
                {
                    SystemSounds.Exclamation.Play();
                    MessageBox.Show("Введите логин и пароль!");
                    return;
                }
                try
                {
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
                    SqlConnectionStringBuilder sConnB = new SqlConnectionStringBuilder()
                    {
                        DataSource     = Properties.Settings.Default.userServerName,
                        InitialCatalog = Properties.Settings.Default.userServerDatabase,
                        UserID         = userName,
                        Password       = password,
                    };
                    sqlConnection.ConnectionString = sConnB.ConnectionString;
                    sqlConnection.Open();
                    HistoryRecordsController.ChangeUser(userName);
                    HistoryRecordsController.WriteAboutSystemEnter(true);
                    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
                    MainForm mainForm = new MainForm(this, sConnB);
                    mainForm.Show();
                    this.Hide();
                    LoginForm.UserName = userName;
                    LoginForm.Password = password;
                    userNameTextBox.Clear();
                    passwordTextBox.Clear();
                }
                catch (SqlException sqlEx)
                {
                    HistoryRecordsController.ChangeUser(userName);
                    HistoryRecordsController.WriteAboutSystemEnter(false);
                    HistoryRecordsController.WriteExceptionToLogFile(sqlEx, "Не удалось выполнить вход. Введены неверный логин и(или) пароль.");
                    SystemSounds.Exclamation.Play();
                    MessageBox.Show("Не удалось выполнить вход.\nУбедитесь, что введены правильные логин и(или) пароль.", "Ошибка");
                }
                catch (Exception ex)
                {
                    SystemSounds.Exclamation.Play();
                    HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка при входе.");
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }