Account.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /// Логика взаимодействия для Account.xaml
  21. /// </summary>
  22. public partial class Account : Window
  23. {
  24. public static int UserId = 0;
  25. string connectionString;
  26. SqlDataAdapter adapter = new SqlDataAdapter();
  27. DataTable usersTable = new DataTable();
  28. public Account(int IdUser)
  29. {
  30. Menu.UserId = IdUser;
  31. InitializeComponent();
  32. //получаем строку подключения из app.config
  33. connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  34. //вывод в профиль фио, логина и аватарки
  35. SqlConnection connection = new SqlConnection(connectionString);
  36. connection.Open();
  37. SqlCommand command = new SqlCommand();
  38. command.CommandText = "SELECT CONCAT(LastName, ' ', FirstName, ' ', MiddleName), Login, Image FROM Users WHERE IdUser = " + IdUser.ToString();
  39. command.Connection = connection;
  40. adapter.SelectCommand = command;
  41. adapter.Fill(usersTable);
  42. LFM.Text = usersTable.Rows[0][0].ToString();
  43. Login.Text = usersTable.Rows[0][1].ToString();
  44. ImageAva.Source = new BitmapImage(new Uri(usersTable.Rows[0][2].ToString()));
  45. connection.Close();
  46. }
  47. //для теста
  48. public string testaccLFM()
  49. {
  50. usersTable.Clear();
  51. SqlConnection connection = new SqlConnection(connectionString);
  52. connection.Open();
  53. SqlCommand command = new SqlCommand();
  54. command.CommandText = "SELECT CONCAT(LastName, ' ', FirstName, ' ', MiddleName), Login, Image FROM Users WHERE IdUser = 1";
  55. command.Connection = connection;
  56. adapter.SelectCommand = command;
  57. adapter.Fill(usersTable);
  58. string LFM;
  59. LFM = usersTable.Rows[0][0].ToString();
  60. connection.Close();
  61. return LFM;
  62. }
  63. public string testaccLogin()
  64. {
  65. usersTable.Clear();
  66. SqlConnection connection = new SqlConnection(connectionString);
  67. connection.Open();
  68. SqlCommand command = new SqlCommand();
  69. command.CommandText = "SELECT CONCAT(LastName, ' ', FirstName, ' ', MiddleName), Login, Image FROM Users WHERE IdUser = 1";
  70. command.Connection = connection;
  71. adapter.SelectCommand = command;
  72. adapter.Fill(usersTable);
  73. string log;
  74. log = usersTable.Rows[0][1].ToString();
  75. connection.Close();
  76. return log;
  77. }
  78. //
  79. string filename;
  80. private void AlterImage(object sender, RoutedEventArgs e)
  81. {
  82. //изменение аватарки
  83. Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  84. dlg.DefaultExt = ".png";
  85. dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg";
  86. Nullable<bool> result = dlg.ShowDialog();
  87. if (result == true)
  88. {
  89. filename = dlg.FileName;
  90. ImageAva.Source = new BitmapImage(new Uri(filename));
  91. }
  92. }
  93. private void Save(object sender, RoutedEventArgs e)
  94. {
  95. //сохранение аватарки
  96. SqlConnection connection = new SqlConnection(connectionString);
  97. connection.Open();
  98. SqlCommand command = new SqlCommand();
  99. command.CommandText = "UPDATE Users SET Image = '" + filename + "' WHERE IdUser = " + Menu.UserId.ToString();
  100. command.Connection = connection;
  101. adapter.SelectCommand = command;
  102. adapter.Fill(usersTable);
  103. connection.Close();
  104. MessageBox.Show("Сохранения изменены");
  105. }
  106. private void Home(object sender, RoutedEventArgs e)
  107. {
  108. //возврат на главную
  109. MainWindow main = new MainWindow();
  110. main.Show();
  111. Close();
  112. }
  113. private void Like(object sender, MouseButtonEventArgs e)
  114. {
  115. //переход в избранное
  116. Windows.Fav fav = new Windows.Fav(UserId);
  117. fav.Show();
  118. Close();
  119. }
  120. private void Exit(object sender, MouseButtonEventArgs e)
  121. {
  122. //выход из аккаунта, возврат на главную
  123. Menu.UserId = 0;
  124. MessageBox.Show("Вы вышли из аккаунта");
  125. MainWindow main = new MainWindow();
  126. main.Show();
  127. Close();
  128. }
  129. }
  130. }