Hi Sergiu,
The first thing is that you are trying to do it without Kentico API, you should always use Kentico API when you are working with application's objects. Second thing that I want to note is that I would recommend you to try to inspect or even debug what is happening when you do that via UI, so you can see what code is handling this functionality.
So take a look at the following file:
\CMSModules\AdminControls\Controls\Class\ClassSites.ascx.cs
and around line 150 you can find the code responsible for adding sites to page type (classes are used).
Best regards, Martin