To find the content databases for the web application selected by WebApplicationSelector control, the code below works:

SPWebApplication webApplication = Selector.CurrentItem;
SPContentDatabaseCollection contentDBs = webApplication.ContentDatabases;
foreach (SPContentDatabase contentDB in contentDBs)
{
Console.WriteLine(contentDB.Name);
}

To find all content databases in the Farm, the code is given below:

SPServiceCollection services = SPFarm.Local.Services;
//foreach (SPService service in services)
{
if (service is SPWebService)
{
SPWebService webService = (SPWebService)service;
SPWebApplicationCollection webApplications = webService.WebApplications;
foreach (SPWebApplication webApplication in webApplications)
{
SPContentDatabaseCollection contentDBs = webApplication.ContentDatabases;
foreach (SPContentDatabase contentDB in contentDBs)
{
Console.WriteLine(contentDB.Name);
}
}
}
}

Advertisements