AddEditUslugi.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 AddEditUslugi : Form
  14. {
  15. SqlConnection connection;
  16. SqlCommand command;
  17. SqlDataAdapter adapter;
  18. public AddEditUslugi()
  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. AdminForm adminForm = new AdminForm();
  27. }
  28. private void saveBtn_MouseMove(object sender, MouseEventArgs e)
  29. {
  30. saveBtn.ForeColor = Color.FromArgb(24, 186, 96);
  31. saveBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96);
  32. }
  33. private void backBtn_MouseMove(object sender, MouseEventArgs e)
  34. {
  35. backBtn.ForeColor = Color.FromArgb(24, 186, 96);
  36. backBtn.FlatAppearance.BorderColor = Color.FromArgb(24, 186, 96);
  37. }
  38. private void backBtn_MouseLeave(object sender, EventArgs e)
  39. {
  40. backBtn.ForeColor = Color.Black;
  41. backBtn.FlatAppearance.BorderColor = Color.Black;
  42. }
  43. private void saveBtn_MouseLeave(object sender, EventArgs e)
  44. {
  45. saveBtn.ForeColor = Color.Black;
  46. saveBtn.FlatAppearance.BorderColor = Color.Black;
  47. }
  48. private void saveBtn_Click(object sender, EventArgs e)
  49. {
  50. if (UslugiClass.add == true)
  51. {
  52. if (string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(priceText.Text))
  53. {
  54. MessageBox.Show("Не все поля заполнены!");
  55. return;
  56. }
  57. else
  58. {
  59. if (MessageBox.Show("Вы действительно хотите добавить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  60. {
  61. try
  62. {
  63. connection.Open();
  64. command.CommandText = "INSERT INTO Услуги(Наименование_услуги, Стоимость) VALUES(@name, @pr)";
  65. command.Parameters.AddWithValue("@name", nameText.Text);
  66. command.Parameters.AddWithValue("@pr", Math.Round(Decimal.Parse(priceText.Text), 2));
  67. command.ExecuteReader();
  68. command.Parameters.Clear();
  69. connection.Close();
  70. this.Close();
  71. MessageBox.Show("Запись успешно добавлена!");
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBox.Show(ex.Message);
  76. command.Parameters.Clear();
  77. connection.Close();
  78. }
  79. }
  80. else
  81. {
  82. }
  83. }
  84. }
  85. if (UslugiClass.change == true)
  86. {
  87. if (string.IsNullOrEmpty(nameText.Text) || string.IsNullOrEmpty(priceText.Text))
  88. {
  89. MessageBox.Show("Не все поля заполнены!");
  90. return;
  91. }
  92. else
  93. {
  94. if (MessageBox.Show("Вы действительно хотите изменить запись?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  95. {
  96. try
  97. {
  98. connection.Open();
  99. command.CommandText = "UPDATE Услуги SET Наименование_услуги=@name, Стоимость=@pr WHERE ID=@id";
  100. command.Parameters.AddWithValue("@name", nameText.Text);
  101. command.Parameters.AddWithValue("@pr", Math.Round(Decimal.Parse(priceText.Text), 2));
  102. command.Parameters.AddWithValue("@id", UslugiClass.id);
  103. command.ExecuteReader();
  104. command.Parameters.Clear();
  105. connection.Close();
  106. this.Close();
  107. MessageBox.Show("Запись успешно изменена!");
  108. }
  109. catch (Exception ex)
  110. {
  111. MessageBox.Show(ex.Message);
  112. command.Parameters.Clear();
  113. connection.Close();
  114. }
  115. }
  116. else
  117. {
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }