We studied liquid and vitreous SiO2 by performing first-principles molecular-dynamics simulations. Diffusion in the liquid is shown to occur through correlated jump events, which disrupt the network only for short time periods. The persistence of the network even at high temperatures is confirmed by the average structural properties of the liquid. By quenching from the melt, we obtained a model for the glass, which forms a perfectly chemically ordered network. Structural and electronic properties of our model glass present a remarkable agreement with vitreous SiO2: the calculated total structure factor closely agrees with data from neutron diffraction experiments and features in the x-ray photoemission spectrum are well reproduced by the electronic density of states. This agreement strongly supports other structural properties which are yet unavailable from experiment such as partial pair correlation functions and bond-angle distributions. A comparative study of the electronic density of states in liquid, vitreous, and crystalline SiO2 shows that enhancement of disorder gives rise to a reduction of the gap.