With the help of supercell method, the first-principle calculations were performed for the study of doping crystal Mg1-xAlxB2 and Mg(B1-yCy)2. Analyzing the variations of the charge distribution and the partial densities of states, we found that the compounds with doping A1 to MgB2 compound and/or replacing boron by carbon exhibit new covalent bond effects and unexpected electronic properties, related to superconductivity. The study of the density of states indicates that superconductivity decreases with the increase of AI fraction and carbon concentration. There exists a transition of superconductor to non-superconductor with the change of AI doping fraction. The substitution of boron by carbon results in the decrease of the transition temperature since the decrease of the electron concentration and the lattice constant. The theoretical predictions agree with experimental observations.