Here is a function that will set a site (including sub sites) to use a custom masterpage.
You pass in the masterpage, web, and if you want to include sub sites (it will call itself with the sub site name).
public static void SetCustomMasterPage(string masterPage, SPWeb web, bool includeSubSites)
web.CustomMasterUrl = GetMasterPageUrl(masterPage);
foreach (SPWeb child in web.Webs)
SetCustomMasterPage(masterPage, child, true);