private void RegistrationButton_Click(object sender, EventArgs e) { int fake; Int32.TryParse(this.AgeTextbox.Text, out fake); if (this.LoginTextobx.Text != "" && this.PasswordTextbox.Text != "" && (this.PasswordTextbox.Text == this.PasswordRepeatTextbox.Text) && fake > 0) { try { MySqlConnection connection = new MySqlConnection(); connection = GlobalThingsClass.Con(GlobalThingsClass.Server, GlobalThingsClass.Databse, GlobalThingsClass.UID, GlobalThingsClass.Password); //MessageBox.Show(GlobalThingsClass.Server + " " + GlobalThingsClass.Databse + " " + GlobalThingsClass.UID + " " + GlobalThingsClass.Password); connection.Open(); MySqlCommand command = new MySqlCommand(); command.Connection = connection; command.CommandText = "INSERT INTO users(login, password, sex, age) values (@login, @password, @sex, @age)"; MySqlParameter loginParameter = new MySqlParameter("@login", this.LoginTextobx.Text); command.Parameters.Add(loginParameter); MySqlParameter passwordParameter = new MySqlParameter("@password", this.PasswordTextbox.Text); command.Parameters.Add(passwordParameter); MySqlParameter sexParameter = new MySqlParameter("@sex", this.SexSelectComboBox.Text); command.Parameters.Add(sexParameter); MySqlParameter ageParameter = new MySqlParameter("@age", fake); command.Parameters.Add(ageParameter); command.ExecuteNonQuery(); MessageBox.Show("Новый пользователь добавлен. Логин: " + this.LoginTextobx.Text + ", пол: " + this.SexSelectComboBox.Text + ", возраст: " + fake, "Успешно зарегистрирован", MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); this.Owner.Show(); this.Hide(); } catch (MySqlException exception) { MessageBox.Show("Возможно, такой пользователь уже существует, нарушен синтаксис или сервер перестал отвечать" + Environment.NewLine + "Отладочная информация:" + Environment.NewLine + exception.StackTrace, "Что-то пошло не так", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception exception) { MessageBox.Show(exception.StackTrace); } } else { MessageBox.Show("Проверьте введённые данные", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void LoginButton_Click(object sender, EventArgs e) { if (!SettingsCheckBox.Checked) { GlobalThingsClass.Server = settings.Fields.Server; GlobalThingsClass.Databse = settings.Fields.Database; GlobalThingsClass.UID = settings.Fields.UID; GlobalThingsClass.Password = settings.Fields.Password; try { MySqlConnection connection = new MySqlConnection(); connection = GlobalThingsClass.Con(GlobalThingsClass.Server, GlobalThingsClass.Databse, GlobalThingsClass.UID, GlobalThingsClass.Password); connection.Open(); MySqlCommand command = new MySqlCommand(); command.Connection = connection; command.CommandText = "SELECT COUNT(*) FROM USERS WHERE Login='******' AND Password='******'"; int rows = int.Parse(command.ExecuteScalar().ToString()); if (rows > 0) { MessageBox.Show("Вы зашли как " + this.LoginTextbox.Text + " на сервер " + GlobalThingsClass.Server, "Авторизация", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hide(); this.Owner.Enabled = true; connection.Close(); } else { MessageBox.Show("Пользователь не найден или пароль неверен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); connection.Close(); } //MessageBox.Show("Connected to " + Server + " successfully"); } catch (MySqlException exception) { MessageBox.Show(exception.StackTrace); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } } else { GlobalThingsClass.Server = this.ServerSettingTextbox.Text; GlobalThingsClass.Databse = this.DatabaseSettingsTextbox.Text; GlobalThingsClass.UID = this.UIDSettingsTextbox.Text; GlobalThingsClass.Password = this.PasswordSettingsTextbox.Text; try { MySqlConnection connection = new MySqlConnection(); connection = GlobalThingsClass.Con(GlobalThingsClass.Server, GlobalThingsClass.Databse, GlobalThingsClass.UID, GlobalThingsClass.Password); connection.Open(); MySqlCommand command = new MySqlCommand(); command.Connection = connection; command.CommandText = "SELECT COUNT(*) FROM USERS WHERE Login=@login AND password=@password"; MySqlParameter loginParameter = new MySqlParameter("@login", this.LoginTextbox.Text); command.Parameters.Add(loginParameter); MySqlParameter passwordParameter = new MySqlParameter("@password", this.PasswordTextbox.Text); command.Parameters.Add(passwordParameter); int rows = int.Parse(command.ExecuteScalar().ToString()); if (rows > 0) { MessageBox.Show("Вы зашли как " + this.LoginTextbox.Text + " на сервер " + GlobalThingsClass.Server, "Авторизация", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hide(); this.Owner.Enabled = true; connection.Close(); } else { MessageBox.Show("Пользователь не найден или пароль неверен", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); connection.Close(); } //MessageBox.Show("Connected to " + Server + " successfully"); } catch (MySqlException exception) { MessageBox.Show(exception.StackTrace); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } } if (SaveSettingsCheckBox.Checked) { settings.Fields.Server = this.ServerSettingTextbox.Text; settings.Fields.Database = this.DatabaseSettingsTextbox.Text; settings.Fields.UID = this.UIDSettingsTextbox.Text; settings.Fields.Password = this.PasswordSettingsTextbox.Text; settings.WriteSettingsToXML(); } }