MainWindow.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Data.SqlClient;
  16. using System.Data;
  17. namespace BorisProject
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=boris;Integrated Security=True");
  29. TrenerWindow trenerwindow = new TrenerWindow();
  30. //Авторизация
  31. private void Vhod_Click(object sender, RoutedEventArgs e)
  32. {
  33. if (logintxt.Text == "" || passwordpsw.Password == "")
  34. {
  35. MessageBox.Show("Ошибка! Пустые поля!");
  36. }
  37. else if (logintxt.Text =="1")
  38. {
  39. TrenerWindow trenerWindow = new TrenerWindow();
  40. this.Close();
  41. trenerWindow.Show();
  42. }
  43. else if (logintxt.Text == "admin" && passwordpsw.Password=="admin")
  44. {
  45. WindowAdmin admin = new WindowAdmin();
  46. this.Close();
  47. admin.Show();
  48. }
  49. else
  50. {
  51. try
  52. {
  53. con.Open();
  54. SqlCommand cmd = new SqlCommand("Select * from Trener where Login ='" + logintxt.Text + "' and Password ='" + passwordpsw.Password + "'", con);
  55. cmd.CommandType = CommandType.Text;
  56. SqlDataAdapter adapter = new SqlDataAdapter();
  57. adapter.SelectCommand = cmd;
  58. DataSet dataSet = new DataSet();
  59. adapter.Fill(dataSet);
  60. if (dataSet.Tables[0].Rows.Count > 0)
  61. {
  62. MessageBox.Show("Добро пожаловать!");
  63. string username = dataSet.Tables[0].Rows[0]["ID_trener"].ToString();
  64. trenerwindow.txtidtrener.Text = username;
  65. trenerwindow.Show();
  66. this.Close();
  67. }
  68. else
  69. {
  70. con.Close();
  71. MessageBox.Show("Такого пользователь нет!");
  72. }
  73. }
  74. catch
  75. {
  76. con.Close();
  77. MessageBox.Show("Ошибка");
  78. }
  79. }
  80. }
  81. private void Exit_Click(object sender, RoutedEventArgs e)
  82. {
  83. Application.Current.Shutdown();
  84. }
  85. //Тест
  86. public bool Auth(string login, string password)
  87. {
  88. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=boris;Integrated Security=True");
  89. con.Open();
  90. SqlCommand cmd = new SqlCommand("Select * from Trener where Login ='" + login.ToString() + "' and Password ='" + password.ToString() + "'", con);
  91. cmd.CommandType = CommandType.Text;
  92. SqlDataAdapter adapter = new SqlDataAdapter();
  93. adapter.SelectCommand = cmd;
  94. DataSet dataSet = new DataSet();
  95. adapter.Fill(dataSet);
  96. if (dataSet.Tables[0].Rows.Count > 0)
  97. {
  98. con.Close();
  99. MessageBox.Show("Вы авторизованы");
  100. return true;
  101. }
  102. else
  103. {
  104. con.Close();
  105. MessageBox.Show("Ошибка логина/пароля");
  106. return false;
  107. }
  108. }
  109. }
  110. }