When did he say so?
Radak: When he was cured and saved from his afflictions. He said in his heart, 'You do not desire Korbanos, rather, that I do what You commanded me.'
Malbim: "Az" means, had You not commanded about Korbanos in the Torah, I would have found it written Alai - in my heart. My intellect and emotions of my Nefesh recognize the obligation to return thanks to one who benefited us.
What do we learn from "bi'Mgilas Sefer Kasuv Alai"?
Yevamos 77a: David said, when I was anointed, I thought that my grandeur was just starting; I did not realize, it was already written in the Torah about me. It says "ha'Nimtza'os" (Bereishis 19:15) about Lot's daughters (David descends from Rus, from Mo'av), like it says "Matzasi David Avdi" (Tehilim 89:21).
Gitin 60a: The Torah was given one Parshah at a time - it is called "Megilas Sefer."
Rashi: At the time of Matan Torah, I said in front of You 'I came to You in Your Bris - "Na'aseh v'Nishma" (Shemos 24:7). This testimony is written about me in Megilas Sefer - in the Torah of Moshe.
Radak: [I came] to fulfill everything written about me [and all of Yisrael] in the Sefer Torah.
Malbim: Refer to 40:8:1:2.