admin.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 hospital
  12. {
  13. public partial class admin : Form
  14. {
  15. SqlConnection sqlConnection = null;
  16. public admin()
  17. {
  18. InitializeComponent();
  19. }
  20. private void admin_Load(object sender, EventArgs e)
  21. {
  22. FormCreating.openConnection(ref sqlConnection);
  23. SqlDataAdapter dataAdapter = new SqlDataAdapter($"select * from employees", sqlConnection);
  24. DataSet dataSet = new DataSet();
  25. dataAdapter.Fill(dataSet);
  26. dataGridView1.DataSource = dataSet.Tables[0];
  27. SqlCommand a = new SqlCommand($"select name_post from posts", sqlConnection);
  28. SqlDataReader reader = a.ExecuteReader();
  29. while (reader.Read())
  30. {
  31. comboBox1.Items.Add(reader.GetString(0));
  32. }
  33. reader.Close();
  34. }
  35. private void button1_Click(object sender, EventArgs e)
  36. {
  37. if (!comboBox1.Text.Equals(""))
  38. {
  39. string row = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
  40. SqlCommand b = new SqlCommand($"select id_post from posts where name_post like N'{comboBox1.SelectedItem}'", sqlConnection);
  41. SqlCommand a = new SqlCommand($"update employees set id_post = @id_post where id_employee like {row}", sqlConnection);
  42. a.Parameters.AddWithValue("id_post", b.ExecuteScalar().ToString());
  43. a.ExecuteNonQuery();
  44. SqlDataAdapter dataAdapter = new SqlDataAdapter($"select * from employees", sqlConnection);
  45. DataSet dataSet = new DataSet();
  46. dataAdapter.Fill(dataSet);
  47. dataGridView1.DataSource = dataSet.Tables[0];
  48. }
  49. else
  50. {
  51. MessageBox.Show("Ошибка", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  52. }
  53. }
  54. private void admin_FormClosing(object sender, FormClosingEventArgs e)
  55. {
  56. Application.Exit();
  57. }
  58. }
  59. }