ADD_customersADD.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 NN_TelekomPP.Forms
  12. {
  13. public partial class ADD_customersADD : Form
  14. {
  15. DataBase dataBase = new DataBase();
  16. public ADD_customersADD()
  17. {
  18. InitializeComponent();
  19. // comboBox1 - организации
  20. string searchString = $"Select Name From Organizations";
  21. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  22. dataBase.openConnection();
  23. SqlDataReader read = com.ExecuteReader();
  24. while (read.Read())
  25. {
  26. comboBox1.Items.Add(read.GetString(0));
  27. }
  28. read.Close();
  29. dataBase.closeConnection();
  30. // comboBox4 - дополнительные услуги
  31. string searchString4 = $"Select Name From ServicesADD";
  32. SqlCommand com4 = new SqlCommand(searchString4, dataBase.GetConnection());
  33. dataBase.openConnection();
  34. SqlDataReader read4 = com4.ExecuteReader();
  35. while (read4.Read())
  36. {
  37. comboBox2.Items.Add(read4.GetString(0));
  38. }
  39. read4.Close();
  40. dataBase.closeConnection();
  41. }
  42. private void button_back_Click(object sender, EventArgs e)
  43. {
  44. this.Hide();
  45. ADD_customers M = new ADD_customers();
  46. M.ShowDialog();
  47. }
  48. private void Save_button_Click(object sender, EventArgs e)
  49. {
  50. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  51. var num2 = $"select ServicesADD_code from ServicesADD where Name = '{comboBox2.Text}'";
  52. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  53. SqlDataAdapter sda2 = new SqlDataAdapter(num2, dataBase.GetConnection());
  54. DataTable dtbl = new DataTable();
  55. sda.Fill(dtbl);
  56. if (comboBox1.Text.Replace(" ", "") == "") MessageBox.Show("Поле название организации не может быть пустым");
  57. else if(comboBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле дополнительная услуга не может быть пустым");
  58. else
  59. {
  60. dataBase.openConnection();
  61. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  62. SqlCommand sqlCommand_org2 = new SqlCommand(num2, dataBase.GetConnection());
  63. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  64. var id_org2 = sqlCommand_org2.ExecuteScalar().ToString();
  65. // Проверка есть ли у данной организации уже данная дополнительная услуга
  66. string b = $"select Organizations.Name from Services_customers join Organizations on Services_customers.Organizations_code = Organizations.Organizations_code join Internet on Internet.Internet_code = Services_customers.Internet_code join ADD_customers on ADD_customers.Organizations_code=Services_customers.Organizations_code join ServicesADD on ADD_customers.ServicesADD_code=ServicesADD .ServicesADD_code join SubscriptionFee on SubscriptionFee.AP_code=Services_customers.AP_code where ServicesADD.Name='{comboBox2.Text}' GROUP BY Organizations.Name,SubscriptionFee.Name,Internet.Name,Services_customers.Services_customers_code ";
  67. SqlCommand com1 = new SqlCommand(b, dataBase.GetConnection());
  68. dataBase.openConnection();
  69. SqlDataReader read = com1.ExecuteReader();
  70. while (read.Read())
  71. {
  72. if (comboBox1.Text == read.GetString(0))
  73. {
  74. MessageBox.Show("Данная организация уже есть в таблице");
  75. return;
  76. }
  77. }
  78. read.Close();
  79. var addQuery = $"insert into ADD_customers (Organizations_code, ServicesADD_code) values ('{id_org}','{id_org2}')";
  80. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  81. command.ExecuteNonQuery();
  82. MessageBox.Show("Запись успешно добавлена в таблицу");
  83. dataBase.closeConnection();
  84. }
  85. }
  86. private void global_FormClosed(object sender, FormClosedEventArgs e)
  87. {
  88. Application.Exit();
  89. }
  90. }
  91. }