Form2.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 testkonkurs1
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19. private void AddUser_Click(object sender, EventArgs e)
  20. {
  21. string connString = "Data Source=DESKTOP-Q8BTJMH;Initial Catalog=Testkonkurs;Integrated Security=True";
  22. SqlConnection conn = new SqlConnection(connString);
  23. SqlCommand command = new SqlCommand("INSERT INTO users(login,password,admin,manager)VALUES(@login,@password,@admin,@manager)", conn);
  24. command.Parameters.AddWithValue("@login", login.Text);
  25. command.Parameters.AddWithValue("@password", password.Text);
  26. if (admin.Checked)
  27. {
  28. command.Parameters.AddWithValue("@admin", "1");
  29. }
  30. else
  31. {
  32. command.Parameters.AddWithValue("@admin", "0");
  33. }
  34. if (manager.Checked)
  35. {
  36. command.Parameters.AddWithValue("@manager", "1");
  37. }
  38. else
  39. {
  40. command.Parameters.AddWithValue("@manager", "0");
  41. }
  42. conn.Open();
  43. try
  44. {
  45. command.ExecuteNonQuery();
  46. MessageBox.Show("пользователь успешно добавлен", "Успешно", MessageBoxButtons.OK, MessageBoxIcon.Information);
  47. }
  48. catch(Exception ex)
  49. {
  50. MessageBox.Show(ex.Message.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  51. }
  52. }
  53. private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  54. {
  55. Application.Exit();
  56. }
  57. private void Delete_Click(object sender, EventArgs e)
  58. {
  59. string connString = "Data Source=DESKTOP-Q8BTJMH;Initial Catalog=Testkonkurs;Integrated Security=True";
  60. SqlConnection conn = new SqlConnection(connString);
  61. SqlCommand command = new SqlCommand("DELETE FROM users WHERE login = @login", conn);
  62. command.Parameters.AddWithValue("@login", LoginDel.Text);
  63. conn.Open();
  64. try
  65. {
  66. command.ExecuteNonQuery();
  67. MessageBox.Show("Пользователь успешно удален", "Успешно", MessageBoxButtons.OK, MessageBoxIcon.Information);
  68. }
  69. catch (Exception ex)
  70. {
  71. MessageBox.Show(ex.Message.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  72. }
  73. }
  74. }
  75. }