using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace ARM_spec_otdelenia { public partial class AddEditSocPol : Form { SqlConnection connection; SqlCommand command; SqlDataAdapter adapter; public AddEditSocPol() { InitializeComponent(); connection = new SqlConnection("Server=DESKTOP-2UUHBK8\\SQLEXPRESS; Database=Спец_отделение; Trusted_connection=true;"); command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; adapter = new SqlDataAdapter(command); } private void saveBtn_MouseMove(object sender, MouseEventArgs e) { saveBtn.ForeColor = Color.FromArgb(24, 186, 96); saveBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96); } private void backBtn_MouseMove(object sender, MouseEventArgs e) { backBtn.ForeColor = Color.FromArgb(24, 186, 96); backBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96); } private void backBtn_MouseLeave(object sender, EventArgs e) { backBtn.ForeColor = Color.Black; backBtn.FlatAppearance.BorderColor = Color.Black; } private void saveBtn_MouseLeave(object sender, EventArgs e) { saveBtn.ForeColor = Color.Black; saveBtn.FlatAppearance.BorderColor = Color.Black; } private void saveBtn_Click(object sender, EventArgs e) { if (SocpolClass.add == true) { if (string.IsNullOrEmpty(workText.Text) || string.IsNullOrEmpty(cashText.Text) || string.IsNullOrEmpty(babyText.Text) || string.IsNullOrEmpty(familyText.Text) || string.IsNullOrEmpty(chsText.Text)) { MessageBox.Show("Не все поля заполнены!"); return; } else { if (MessageBox.Show("Вы действительно хотите добавить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { connection.Open(); command.CommandText = "INSERT INTO Социальное_положение(Место_работы, Доход, Количество_детей, Состав_семьи, Наличие_ЧС) VALUES(@work, @cash, @baby, @fam, @chs)"; command.Parameters.AddWithValue("@work", workText.Text); command.Parameters.AddWithValue("@cash", Decimal.Parse(cashText.Text)); command.Parameters.AddWithValue("@baby", babyText.Text); command.Parameters.AddWithValue("@fam", familyText.Text); command.Parameters.AddWithValue("@chs", chsText.Text); command.ExecuteReader(); command.Parameters.Clear(); connection.Close(); this.Close(); MessageBox.Show("Запись успешно добавлена!"); } catch (Exception ex) { MessageBox.Show(ex.Message); command.Parameters.Clear(); connection.Close(); } } else { } } } if (SocpolClass.change == true) { if (string.IsNullOrEmpty(workText.Text) || string.IsNullOrEmpty(cashText.Text) || string.IsNullOrEmpty(babyText.Text) || string.IsNullOrEmpty(familyText.Text) || string.IsNullOrEmpty(chsText.Text)) { MessageBox.Show("Не все поля заполнены!"); return; } else { if (MessageBox.Show("Вы действительно хотите изменить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { connection.Open(); command.CommandText = "UPDATE Социальное_положение SET Место_работы=@work, Доход=@cash, Количество_детей=@baby, Состав_семьи=@fam, Наличие_ЧС=@chs WHERE ID=@id"; command.Parameters.AddWithValue("@work", workText.Text); command.Parameters.AddWithValue("@cash", Decimal.Parse(cashText.Text)); command.Parameters.AddWithValue("@baby", babyText.Text); command.Parameters.AddWithValue("@fam", familyText.Text); command.Parameters.AddWithValue("@chs", chsText.Text); command.Parameters.AddWithValue("@id", SocpolClass.id); command.ExecuteReader(); command.Parameters.Clear(); connection.Close(); this.Close(); MessageBox.Show("Запись успешно изменена!"); } catch (Exception ex) { MessageBox.Show(ex.Message); command.Parameters.Clear(); connection.Close(); } } else { } } } } } }