More precisely, my question is as follows: Let $X$ be a qcqs scheme, $Z \subset X$ a closed subscheme and assume that there exists an open affine subscheme $U \subset X$ containing $Z$ such that $Z$ is a connected component of $U$. Does this imply that $Z$ is a connected component of $X$ ?
I do not know whether we need the algebro-geometric setup since this question only regards the underlying topological space of the scheme. Note that since $X$ is qcqs, every connected component is the intersection of its clopen supersets in $X$.