123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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 AddEditLichnoe : Form
- {
- SqlConnection connection;
- SqlCommand command;
- SqlDataAdapter adapter;
- public AddEditLichnoe()
- {
- 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 (LichnoeClass.add == true)
- {
- if (string.IsNullOrEmpty(surnameText.Text) || string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(otchText.Text) || string.IsNullOrEmpty(ageText.Text) || string.IsNullOrEmpty(polText.Text))
- {
- MessageBox.Show("Не все поля заполнены!");
- return;
- }
- else
- {
- if (MessageBox.Show("Вы действительно хотите добавить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- try
- {
- connection.Open();
- command.CommandText = "INSERT INTO Личные_данные(Фамилия, Имя, Отчество, Возраст, Пол) VALUES(@fam, @name, @otch, @age, @pol)";
- command.Parameters.AddWithValue("@fam", surnameText.Text);
- command.Parameters.AddWithValue("@name", nameText.Text);
- command.Parameters.AddWithValue("@otch", otchText.Text);
- command.Parameters.AddWithValue("@age", ageText.Text);
- command.Parameters.AddWithValue("@pol", polText.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 (LichnoeClass.change == true)
- {
- if (string.IsNullOrEmpty(surnameText.Text) || string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(otchText.Text) || string.IsNullOrEmpty(ageText.Text) || string.IsNullOrEmpty(polText.Text))
- {
- MessageBox.Show("Не все поля заполнены!");
- return;
- }
- else
- {
- if (MessageBox.Show("Вы действительно хотите изменить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- try
- {
- connection.Open();
- command.CommandText = "UPDATE Личные_данные SET Фамилия=@fam, Имя=@name, Отчество=@otch, Возраст=@age, Пол=@pol WHERE ID=@id";
- command.Parameters.AddWithValue("@fam", surnameText.Text);
- command.Parameters.AddWithValue("@name", nameText.Text);
- command.Parameters.AddWithValue("@otch", otchText.Text);
- command.Parameters.AddWithValue("@age", ageText.Text);
- command.Parameters.AddWithValue("@pol", polText.Text);
- command.Parameters.AddWithValue("@id", LichnoeClass.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
- {
- }
- }
- }
- }
- }
- }
|