Registration.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace distant
  12. {
  13. public partial class Registration : Form
  14. {
  15. public Registration()
  16. {
  17. InitializeComponent();
  18. pictureBox2.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
  19. pictureBox8.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
  20. pictureBox4.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
  21. pictureBox10.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
  22. FormBorderStyle = FormBorderStyle.FixedSingle;
  23. LoginBox.Text = "Введите имя";
  24. LoginBox1.Text = "Введите фамилию";
  25. PasswordBox.Text = "Введите логин";
  26. PasswordBox1.Text = "Введите пароль";
  27. }
  28. private void Registration_Load(object sender, EventArgs e)
  29. {
  30. }
  31. private void LoginBox_Enter(object sender, EventArgs e)
  32. {
  33. if (LoginBox.Text == "Введите имя")
  34. LoginBox.Text = "";
  35. }
  36. private void LoginBox_Leave(object sender, EventArgs e)
  37. {
  38. if (LoginBox.Text == "")
  39. LoginBox.Text = "Введите имя";
  40. }
  41. private void LoginBox1_Enter_1(object sender, EventArgs e)
  42. {
  43. if (LoginBox1.Text == "Введите фамилию")
  44. LoginBox1.Text = "";
  45. }
  46. private void LoginBox1_Leave_1(object sender, EventArgs e)
  47. {
  48. if (LoginBox1.Text == "")
  49. LoginBox1.Text = "Введите фамилию";
  50. }
  51. private void PasswordBox_Enter_1(object sender, EventArgs e)
  52. {
  53. if (PasswordBox.Text == "Введите логин")
  54. PasswordBox.Text = "";
  55. }
  56. private void PasswordBox_Leave_1(object sender, EventArgs e)
  57. {
  58. if (PasswordBox.Text == "")
  59. PasswordBox.Text = "Введите логин";
  60. }
  61. private void PasswordBox1_Enter_1(object sender, EventArgs e)
  62. {
  63. if (PasswordBox1.Text == "Введите пароль")
  64. PasswordBox1.Text = "";
  65. }
  66. private void PasswordBox1_Leave(object sender, EventArgs e)
  67. {
  68. if (PasswordBox1.Text == "Введите пароль")
  69. PasswordBox1.Text = "";
  70. }
  71. private void button1_Click(object sender, EventArgs e)
  72. {
  73. if (LoginBox.Text == "Введите имя")
  74. {
  75. MessageBox.Show("Введите имя");
  76. return;
  77. }
  78. if (PasswordBox1.Text == "Введите пароль")
  79. {
  80. MessageBox.Show("Введите пароль"); return;
  81. }
  82. if (PasswordBox.Text == "Введите логин")
  83. {
  84. MessageBox.Show("Введите пароль"); return;
  85. }
  86. if (LoginBox1.Text == "Введите фамилию")
  87. {
  88. MessageBox.Show("Введите фамилию"); return;
  89. }
  90. if (checkUser())
  91. return;
  92. Db db = new Db();
  93. MySqlCommand mySqlCommand = new MySqlCommand("INSERT INTO Users (login, password, name, surname) VALUES (@login, @password,@name,@surname)", db.GetConnection());
  94. mySqlCommand.Parameters.Add("@login", MySqlDbType.VarChar).Value = PasswordBox.Text;
  95. mySqlCommand.Parameters.Add("password", MySqlDbType.VarChar).Value = PasswordBox1.Text;
  96. mySqlCommand.Parameters.Add("@name", MySqlDbType.VarChar).Value = LoginBox.Text;
  97. mySqlCommand.Parameters.Add("@surname", MySqlDbType.VarChar).Value = LoginBox1.Text;
  98. db.openConnection();
  99. if (mySqlCommand.ExecuteNonQuery() == 1)
  100. MessageBox.Show("Аккаунт был создан");
  101. else
  102. MessageBox.Show("Аккаунт не был создан");
  103. db.closeConnection();
  104. }
  105. public Boolean checkUser()
  106. {
  107. Db db = new Db();
  108. DataTable dataTable = new DataTable();
  109. MySqlDataAdapter adapter = new MySqlDataAdapter();
  110. MySqlCommand mySqlCommand = new MySqlCommand("SELECT * FROM Users WHERE login = @login", db.GetConnection());
  111. mySqlCommand.Parameters.Add("@login", MySqlDbType.VarChar).Value = PasswordBox.Text;
  112. adapter.SelectCommand = mySqlCommand;
  113. adapter.Fill(dataTable);
  114. if (dataTable.Rows.Count > 0)
  115. {
  116. MessageBox.Show("Такой пользователь уже найден, введите другой логин");
  117. return true;
  118. }
  119. else
  120. return false;
  121. }
  122. }
  123. }