Can hyperglycemia be related to stress or an infection?

Yes. In times of infection or stress, cortisol and catecholamines are released and raise blood sugar levels. Insulin demand is also increased. Therefore, stress or infection can precipitate hyperglycemia, especially in people with Insulin resistance or diabetes.
Either one or both. Both stress and infection raise glucose levels, especially in diabetics.