The struggling Sandoval came through after reliever J.P. Howell (1-1) intentionally walked Hunter Pence with two outs. Sandoval, who entered the game batting just .164, grounded into center field to score Joaquin Arias from second.
Jean Machi (3-0) pitched out of a bases-loaded jam earlier in the seventh for the win.
Buster Posey broke an 0-for-13 skid with an RBI single in the third to give the Giants a 1-0 lead. Dee Gordon's tying triple in the sixth accounted for the Dodgers' lone run.