ДобавитьПол.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ЭлектронныйЖурнал
  12. {
  13. public partial class ДобавитьПол : Form
  14. {
  15. SqlConnection connection;
  16. SqlCommand command;
  17. SqlDataAdapter adapter;
  18. DataTable table;
  19. string conn = "Server=DESKTOP-JES5T51\\SQLEXPRESS;DataBase=EJBD;Trusted_connection=true";
  20. public ДобавитьПол()
  21. {
  22. InitializeComponent();
  23. connection = new SqlConnection("Server=DESKTOP-JES5T51\\SQLEXPRESS;DataBase=EJBD;Trusted_connection=true");
  24. command = new SqlCommand();
  25. command.Connection = connection;
  26. command.CommandText = Text;
  27. adapter = new SqlDataAdapter(command);
  28. table = new DataTable();
  29. }
  30. private void ShowTable(string text)
  31. {
  32. dataGridView1.Columns.Clear();
  33. dataGridView1.DataSource = null;
  34. command.CommandText = text;
  35. table.Clear();
  36. adapter.Fill(table);
  37. dataGridView1.DataSource = table;
  38. }
  39. private void Пользователи_Load(object sender, EventArgs e)
  40. {
  41. ShowTable("Select Login as Логин, Password as Пароль from Пользователи");
  42. }
  43. private void AddButton_Click(object sender, EventArgs e)
  44. {
  45. {
  46. if (string.IsNullOrEmpty(LoginB.Text) || string.IsNullOrEmpty(PassB.Text))
  47. {
  48. MessageBox.Show("Не все поля заполнен!");
  49. return;
  50. }
  51. else
  52. {
  53. if (MessageBox.Show("Добавить нового пользователя?", "Внимание", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  54. {
  55. try
  56. {
  57. connection.Open();
  58. command.CommandText = "INSERT INTO Пользователи (Login, Password) VALUES(@l,@p)";
  59. command.Parameters.AddWithValue("@l", LoginB.Text);
  60. command.Parameters.AddWithValue("@p", PassB.Text);
  61. command.ExecuteReader();
  62. command.Parameters.Clear();
  63. connection.Close();
  64. MessageBox.Show("Новый пользователь был добавлен!");
  65. ShowTable("Select Login as Логин, Password as Пароль from Пользователи");
  66. }
  67. catch (Exception ex)
  68. {
  69. MessageBox.Show(ex.Message);
  70. command.Parameters.Clear();
  71. connection.Close();
  72. }
  73. }
  74. }
  75. }
  76. }
  77. private void DeleteButton_Click(object sender, EventArgs e)
  78. {
  79. if (MessageBox.Show("вы хотите удалить?", "Ошибка", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  80. {
  81. connection.Open();
  82. command.CommandText = ("Delete From Пользователи Where Login=@l");
  83. command.Parameters.AddWithValue("@l", dataGridView1.CurrentRow.Cells[0].Value);
  84. command.ExecuteReader();
  85. command.Parameters.Clear();
  86. connection.Close();
  87. MessageBox.Show("Запись была удалена");
  88. ShowTable("Select Login as Логин, Password as Пароль from Пользователи");
  89. }
  90. }
  91. private void Back_Click(object sender, EventArgs e)
  92. {
  93. Menu i = new Menu();
  94. i.Show();
  95. this.Hide();
  96. }
  97. }
  98. }