AddEditLichnoe.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace ARM_spec_otdelenia
  12. {
  13. public partial class AddEditLichnoe : Form
  14. {
  15. SqlConnection connection;
  16. SqlCommand command;
  17. SqlDataAdapter adapter;
  18. public AddEditLichnoe()
  19. {
  20. InitializeComponent();
  21. connection = new SqlConnection("Server=DESKTOP-2UUHBK8\\SQLEXPRESS; Database=Спец_отделение; Trusted_connection=true;");
  22. command = new SqlCommand();
  23. command.Connection = connection;
  24. command.CommandType = CommandType.Text;
  25. adapter = new SqlDataAdapter(command);
  26. }
  27. private void saveBtn_MouseMove(object sender, MouseEventArgs e)
  28. {
  29. saveBtn.ForeColor = Color.FromArgb(24, 186, 96);
  30. saveBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96);
  31. }
  32. private void backBtn_MouseMove(object sender, MouseEventArgs e)
  33. {
  34. backBtn.ForeColor = Color.FromArgb(24, 186, 96);
  35. backBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96);
  36. }
  37. private void backBtn_MouseLeave(object sender, EventArgs e)
  38. {
  39. backBtn.ForeColor = Color.Black;
  40. backBtn.FlatAppearance.BorderColor = Color.Black;
  41. }
  42. private void saveBtn_MouseLeave(object sender, EventArgs e)
  43. {
  44. saveBtn.ForeColor = Color.Black;
  45. saveBtn.FlatAppearance.BorderColor = Color.Black;
  46. }
  47. private void saveBtn_Click(object sender, EventArgs e)
  48. {
  49. if (LichnoeClass.add == true)
  50. {
  51. if (string.IsNullOrEmpty(surnameText.Text) || string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(otchText.Text) || string.IsNullOrEmpty(ageText.Text) || string.IsNullOrEmpty(polText.Text))
  52. {
  53. MessageBox.Show("Не все поля заполнены!");
  54. return;
  55. }
  56. else
  57. {
  58. if (MessageBox.Show("Вы действительно хотите добавить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  59. {
  60. try
  61. {
  62. connection.Open();
  63. command.CommandText = "INSERT INTO Личные_данные(Фамилия, Имя, Отчество, Возраст, Пол) VALUES(@fam, @name, @otch, @age, @pol)";
  64. command.Parameters.AddWithValue("@fam", surnameText.Text);
  65. command.Parameters.AddWithValue("@name", nameText.Text);
  66. command.Parameters.AddWithValue("@otch", otchText.Text);
  67. command.Parameters.AddWithValue("@age", ageText.Text);
  68. command.Parameters.AddWithValue("@pol", polText.Text);
  69. command.ExecuteReader();
  70. command.Parameters.Clear();
  71. connection.Close();
  72. this.Close();
  73. MessageBox.Show("Запись успешно добавлена!");
  74. }
  75. catch (Exception ex)
  76. {
  77. MessageBox.Show(ex.Message);
  78. command.Parameters.Clear();
  79. connection.Close();
  80. }
  81. }
  82. else
  83. {
  84. }
  85. }
  86. }
  87. if (LichnoeClass.change == true)
  88. {
  89. if (string.IsNullOrEmpty(surnameText.Text) || string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(otchText.Text) || string.IsNullOrEmpty(ageText.Text) || string.IsNullOrEmpty(polText.Text))
  90. {
  91. MessageBox.Show("Не все поля заполнены!");
  92. return;
  93. }
  94. else
  95. {
  96. if (MessageBox.Show("Вы действительно хотите изменить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  97. {
  98. try
  99. {
  100. connection.Open();
  101. command.CommandText = "UPDATE Личные_данные SET Фамилия=@fam, Имя=@name, Отчество=@otch, Возраст=@age, Пол=@pol WHERE ID=@id";
  102. command.Parameters.AddWithValue("@fam", surnameText.Text);
  103. command.Parameters.AddWithValue("@name", nameText.Text);
  104. command.Parameters.AddWithValue("@otch", otchText.Text);
  105. command.Parameters.AddWithValue("@age", ageText.Text);
  106. command.Parameters.AddWithValue("@pol", polText.Text);
  107. command.Parameters.AddWithValue("@id", LichnoeClass.id);
  108. command.ExecuteReader();
  109. command.Parameters.Clear();
  110. connection.Close();
  111. this.Close();
  112. MessageBox.Show("Запись успешно изменена!");
  113. }
  114. catch (Exception ex)
  115. {
  116. MessageBox.Show(ex.Message);
  117. command.Parameters.Clear();
  118. connection.Close();
  119. }
  120. }
  121. else
  122. {
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }