You can find the server instances with this function:
DataTable _dtServers = SqlDataSourceEnumerator.Instance.GetDataSources();
And you can attach a DataBase with this code:
CREATE DATABASE database_name
ON <filespec> [ ,...n ]
FOR { ATTACH [ WITH <service_broker_option> ]
| ATTACH_REBUILD_LOG }
[;]
You can find more information about it here:
CLICK