some people create database names that REQUIRE brackets (like 12345), so I would try something like this:
from [' + name + '].dbo.mytable ...
same goes for table names too, btw
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service