Form2.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 konkursTest14
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  20. {
  21. Application.Exit();
  22. }
  23. private void addUser_Click(object sender, EventArgs e)
  24. {
  25. String connString = "Data Source=CLASS3108;Initial Catalog=konkurstest;Integrated Security=True";
  26. SqlConnection conn = new SqlConnection(connString);
  27. SqlCommand command = new SqlCommand("INSERT INTO users(login,password,admin,manager)VALUES(@login,@password,@admin,@manager)", conn);
  28. command.Parameters.Add("@login", Login.Text);
  29. command.Parameters.Add("@password", password.Text);
  30. conn.Open();
  31. try
  32. {
  33. if (AdminButton.Checked)
  34. {
  35. command.Parameters.Add("@admin", "1");
  36. }else
  37. {
  38. command.Parameters.Add("@admin", "0");
  39. }
  40. if (ManagerButton.Checked)
  41. {
  42. command.Parameters.Add("@manager", "1");
  43. }
  44. else
  45. {
  46. command.Parameters.Add("@manager", "0");
  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 panel1_Paint(object sender, PaintEventArgs e)
  60. {
  61. }
  62. private void Login_Enter(object sender, EventArgs e)
  63. {
  64. if (Login.Text == "Введите логин")
  65. {
  66. Login.Text = "";
  67. }
  68. }
  69. private void Login_Leave(object sender, EventArgs e)
  70. {
  71. if (Login.Text == "")
  72. {
  73. Login.Text = "Введите логин";
  74. }
  75. }
  76. private void password_Enter(object sender, EventArgs e)
  77. {
  78. if (password.Text == "Введите пароль")
  79. {
  80. password.Text = "";
  81. }
  82. }
  83. private void password_Leave(object sender, EventArgs e)
  84. {
  85. if (password.Text == "")
  86. {
  87. password.Text = "Введите пароль";
  88. }
  89. }
  90. private void Delete_Click(object sender, EventArgs e)
  91. {
  92. String connString = "Data Source=CLASS3108;Initial Catalog=konkurstest;Integrated Security=True";
  93. SqlConnection conn = new SqlConnection(connString);
  94. SqlCommand command = new SqlCommand("DELETE FROM users where login = @login", conn);
  95. command.Parameters.Add("@login", LoginDel.Text);
  96. conn.Open();
  97. try
  98. {
  99. command.ExecuteNonQuery();
  100. MessageBox.Show("Пользователь успешно удален", "Успешно", MessageBoxButtons.OK,MessageBoxIcon.Information);
  101. }
  102. catch(Exception ex)
  103. {
  104. MessageBox.Show(ex.Message.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  105. }
  106. finally
  107. {
  108. }
  109. }
  110. private void LoginDel_Enter(object sender, EventArgs e)
  111. {
  112. if(LoginDel.Text == "Введите логин")
  113. {
  114. LoginDel.Text = "";
  115. }
  116. }
  117. private void LoginDel_Leave(object sender, EventArgs e)
  118. {
  119. if (LoginDel.Text == "")
  120. {
  121. LoginDel.Text = "Введите логин";
  122. }
  123. }
  124. }
  125. }