Form2.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 KokursTest2
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form2_Load(object sender, EventArgs e)
  20. {
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. String connstring = "Data Source=DESKTOP-Q8BTJMH;Initial Catalog=konkurs;Integrated Security=True";
  25. SqlConnection conn = new SqlConnection(connstring);
  26. conn.Open();
  27. SqlCommand command = new SqlCommand("Insert INTO users (login,password,admin,manager)VALUES(@login,@password,@admin,@manager)", conn);
  28. command.Parameters.Add("@login", textBox1.Text);
  29. command.Parameters.Add("@password", textBox2.Text);
  30. if (radioButton1.Checked)
  31. {
  32. command.Parameters.Add("@admin", "1");
  33. }
  34. else
  35. {
  36. command.Parameters.Add("@admin", "0");
  37. }
  38. if (radioButton2.Checked)
  39. {
  40. command.Parameters.Add("@manager", "1");
  41. }
  42. else
  43. {
  44. command.Parameters.Add("@manager", "0");
  45. }
  46. try
  47. {
  48. command.ExecuteNonQuery();
  49. MessageBox.Show("Пользователь успешно добавлен","Успешно",MessageBoxButtons.OK,MessageBoxIcon.Information);
  50. }
  51. catch(Exception ex)
  52. {
  53. MessageBox.Show(ex.Message.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  54. }
  55. finally
  56. {
  57. }
  58. }
  59. private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  60. {
  61. Application.Exit();
  62. }
  63. private void panel1_Paint(object sender, PaintEventArgs e)
  64. {
  65. }
  66. private void textBox2_TextChanged(object sender, EventArgs e)
  67. {
  68. }
  69. private void textBox1_TextChanged(object sender, EventArgs e)
  70. {
  71. }
  72. private void textBox1_Enter(object sender, EventArgs e)
  73. {
  74. if (textBox1.Text == "Введите логин")
  75. {
  76. textBox1.Text = "";
  77. }
  78. }
  79. private void textBox1_Leave(object sender, EventArgs e)
  80. {
  81. if (textBox1.Text == "")
  82. {
  83. textBox1.Text = "Введите логин";
  84. }
  85. }
  86. private void textBox2_Enter(object sender, EventArgs e)
  87. {
  88. if (textBox2.Text == "Введите пароль")
  89. {
  90. textBox2.Text = "";
  91. }
  92. }
  93. private void textBox2_Leave(object sender, EventArgs e)
  94. {
  95. if (textBox2.Text == "")
  96. {
  97. textBox2.Text = "Введите пароль";
  98. }
  99. }
  100. private void textBox3_TextChanged(object sender, EventArgs e)
  101. {
  102. }
  103. private void button2_Click(object sender, EventArgs e)
  104. {
  105. String connstring = "Data Source=DESKTOP-Q8BTJMH;Initial Catalog=konkurs;Integrated Security=True";
  106. SqlConnection conn = new SqlConnection(connstring);
  107. conn.Open();
  108. SqlCommand command = new SqlCommand("DELETE FROM users WHERE login = @login", conn);
  109. command.Parameters.Add("@login", textBox3.Text);
  110. try
  111. {
  112. command.ExecuteNonQuery();
  113. MessageBox.Show("Пользователь успешно удален", "Успешно", MessageBoxButtons.OK, MessageBoxIcon.Information);
  114. }
  115. catch (Exception ex)
  116. {
  117. MessageBox.Show(ex.Message.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  118. }
  119. finally
  120. {
  121. }
  122. }
  123. }
  124. }