Fixed #16969 -- Don't connect to named database when possible

Thanks Andreas Pelme for the report and initial patch.