Reg.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using System.Data.SqlClient;
  15. using System.Data;
  16. using System.Configuration;
  17. namespace kursach.Windows
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для Reg.xaml
  21. /// </summary>
  22. public partial class Reg : Window
  23. {
  24. string connectionString;
  25. SqlDataAdapter adapter = new SqlDataAdapter();
  26. DataTable usersTable = new DataTable();
  27. public Reg()
  28. {
  29. InitializeComponent();
  30. //получаем строку подключения из app.config
  31. connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  32. }
  33. private void back(object sender, RoutedEventArgs e)
  34. {
  35. //вернуться назад
  36. Windows.Auth auth = new Windows.Auth();
  37. auth.Show();
  38. Close();
  39. }
  40. private void home(object sender, RoutedEventArgs e)
  41. {
  42. //вернуться на главную
  43. MainWindow main = new MainWindow();
  44. main.Show();
  45. Close();
  46. }
  47. private void okay(object sender, RoutedEventArgs e)
  48. {
  49. //обработчик ошибок при регистрации
  50. if(lname.Text == "" || fname.Text == "" || mname.Text == "" || login.Text == "" || pass1.Password == "" || pass2.Password == "")
  51. {
  52. MessageBox.Show("Не все обязательные поля заполнены.");
  53. return;
  54. }
  55. if(login.Text.Length < 5 || pass1.Password.Length < 5)
  56. {
  57. MessageBox.Show("Слишком короткий логин и/или пароль.");
  58. return;
  59. }
  60. if(pass1.Password != pass2.Password)
  61. {
  62. MessageBox.Show("Пароли не совпадают.");
  63. return;
  64. }
  65. SqlConnection connection = new SqlConnection(connectionString);
  66. connection.Open();
  67. SqlCommand command = new SqlCommand();
  68. command.CommandText = "SELECT * FROM Users WHERE Login = '" + login.Text + "'";
  69. command.Connection = connection;
  70. adapter.SelectCommand = command;
  71. adapter.Fill(usersTable);
  72. if (usersTable.Rows.Count != 0)
  73. {
  74. MessageBox.Show("Такой логин уже существует, попробуйте другой.");
  75. return;
  76. }
  77. else
  78. {
  79. //успех, переход в профиль
  80. command.CommandText = "INSERT INTO Users (Login, Password, LastName, FirstName, MiddleName) VALUES ('" + login.Text + "', '" + pass1.Password + "', '" + lname.Text + "', '" + fname.Text + "', '" + mname.Text + "')";
  81. command.Connection = connection;
  82. adapter.InsertCommand = command;
  83. adapter.Fill(usersTable);
  84. MessageBox.Show("Регистрация прошла успешно.");
  85. Windows.Auth auth = new Windows.Auth();
  86. auth.Show();
  87. Close();
  88. }
  89. connection.Close();
  90. }
  91. //для теста
  92. public bool testreg(string LName, string FName, string MName, string log, string pass1, string pass2)
  93. {
  94. usersTable.Clear();
  95. //обработчик ошибок при регистрации
  96. if (LName == "" || FName == "" || MName == "" || log == "" || pass1 == "" || pass2 == "")
  97. {
  98. return false;
  99. }
  100. if (log.Length < 5 || pass1.Length < 5)
  101. {
  102. return false;
  103. }
  104. if (pass1 != pass2)
  105. {
  106. return false;
  107. }
  108. SqlConnection connection = new SqlConnection(connectionString);
  109. connection.Open();
  110. SqlCommand command = new SqlCommand();
  111. command.CommandText = "SELECT * FROM Users WHERE Login = '" + log + "'";
  112. command.Connection = connection;
  113. adapter.SelectCommand = command;
  114. adapter.Fill(usersTable);
  115. if (usersTable.Rows.Count != 0)
  116. {
  117. connection.Close();
  118. return false;
  119. }
  120. else
  121. {
  122. //успех, переход в профиль
  123. connection.Close();
  124. return true;
  125. }
  126. }
  127. }
  128. }