Account.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. string connectionString;
  25. SqlDataAdapter adapter = new SqlDataAdapter();
  26. DataTable usersTable = new DataTable();
  27. public Account(int IdUser)
  28. {
  29. Menu.UserId = IdUser;
  30. InitializeComponent();
  31. //получаем строку подключения из app.config
  32. connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  33. //вывод в профиль фио, логина и аватарки
  34. SqlConnection connection = new SqlConnection(connectionString);
  35. connection.Open();
  36. SqlCommand command = new SqlCommand();
  37. command.CommandText = "SELECT CONCAT(LastName, ' ', FirstName, ' ', MiddleName), Login, Image FROM Users WHERE IdUser = " + IdUser.ToString();
  38. command.Connection = connection;
  39. adapter.SelectCommand = command;
  40. adapter.Fill(usersTable);
  41. LFM.Text = usersTable.Rows[0][0].ToString();
  42. Login.Text = usersTable.Rows[0][1].ToString();
  43. ImageAva.Source = new BitmapImage(new Uri(usersTable.Rows[0][2].ToString()));
  44. connection.Close();
  45. }
  46. string filename;
  47. private void AlterImage(object sender, RoutedEventArgs e)
  48. {
  49. //изменение аватарки
  50. Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  51. dlg.DefaultExt = ".png";
  52. dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
  53. Nullable<bool> result = dlg.ShowDialog();
  54. if (result == true)
  55. {
  56. filename = dlg.FileName;
  57. ImageAva.Source = new BitmapImage(new Uri(filename));
  58. }
  59. }
  60. private void Save(object sender, RoutedEventArgs e)
  61. {
  62. //сохранение аватарки
  63. SqlConnection connection = new SqlConnection(connectionString);
  64. connection.Open();
  65. SqlCommand command = new SqlCommand();
  66. command.CommandText = "UPDATE Users SET Image = '" + filename + "' WHERE IdUser = " + Menu.UserId.ToString();
  67. command.Connection = connection;
  68. adapter.SelectCommand = command;
  69. adapter.Fill(usersTable);
  70. connection.Close();
  71. MessageBox.Show("Сохранения изменены");
  72. }
  73. private void Home(object sender, RoutedEventArgs e)
  74. {
  75. //возврат на главную
  76. MainWindow main = new MainWindow();
  77. main.Show();
  78. Close();
  79. }
  80. private void Like(object sender, MouseButtonEventArgs e)
  81. {
  82. //переход в избранное
  83. }
  84. private void Exit(object sender, MouseButtonEventArgs e)
  85. {
  86. //выход из аккаунта, возврат на главную
  87. Menu.UserId = 0;
  88. MessageBox.Show("Вы вышли из аккаунта");
  89. MainWindow main = new MainWindow();
  90. main.Show();
  91. Close();
  92. }
  93. }
  94. }