Ejemplo n.º 1
0
        private static ObservableCollection<Session> GetUserSessions(User user)
        {
            ObservableCollection<Session> sessions = new ObservableCollection<Session>();

            string query = "SELECT * FROM Session WHERE UserID = " + user.UserID;
            try
            {
                SqlConnection connection = new SqlConnection(ConnectionString);
                SqlCommand cmd = new SqlCommand(query, connection);
                connection.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {

                    Session s = new Session(
                        Convert.ToDateTime(reader["LastReply"]),
                        Convert.ToDateTime(reader["LoginTime"]),
                        reader["LogoutTime"] != DBNull.Value ? Convert.ToDateTime(reader["LogoutTime"]) : Convert.ToDateTime(reader["LastReply"]),
                        reader["Site"].ToString()
                    );
                    sessions.Add(s);
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                string errmsg = "Fehler beim Abrufen der Sitzungen des Benutzers '" + user.Username + "'.\n\n";
                errmsg += "DatabaseHandler.GetUserSessions(user): " + ex.ToString();
                throw new Exception(errmsg);
            }

            return sessions;
        }
Ejemplo n.º 2
0
        public static void SaveUserSession(Session session)
        {
            if (session.LoginTime == null) throw new Exception("Die Session kann nicht eindeutig zugeordnet werden, da das Attribut 'LoginTime' fehlt.");
            if (session.Site == null) throw new Exception("Die Session kann nicht eindeutig zugeordnet werden, da das Attribut 'Site' fehlt.");

            try
            {
                string query = "UPDATE [SESSION] SET LastReply = '" + session.LastReply + "'" +
                    ", LogoutTime = " + (session.LogoutTime == null ? "null" : "'" + session.LogoutTime + "'") +
                    " WHERE LoginTime = '" + session.LoginTime + "' AND [Site] = '" + session.Site + "'";
                SqlConnection connection = new SqlConnection(ConnectionString);
                SqlCommand cmd = new SqlCommand(query, connection);
                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                string errmsg = "Fehler beim Speichern der Sitzungsdaten der Session.\n\n";
                errmsg += "DatabaseHandler.LogUserIn(logintime, site, session): " + ex.ToString();
                throw new Exception(errmsg);
            }
        }