how to find the sqlserver name(data source) used by sharepoint?
Can u people Help me How t开发者_JAVA技巧o get the sqlserver name used by sharepoint programatically?? is there any such api provided like i have installed sharepoint on the sqlnamed instance. How to get the sqlservername.
Thanks in Advance...
This code should help you and you need to run this code from the SharePoint Server or WFE server.
public String GetSharePointSQLServerName()
{
String sServerName = "notFound";
foreach (var item in SPFarm.Local.Servers)
{
foreach (var svc in item.ServiceInstances)
{
if (svc is SPDatabaseServiceInstance)
{
SPDatabaseServiceInstance s = svc as SPDatabaseServiceInstance;
sServerName = item.DisplayName+"\\"+s.Instance;
}
}
}
return sServerName;
}
So what is if you do not have a named database instance?
better you use this:
var serverNames = from server in SPFarm.Local.Servers
from service in server.ServiceInstances
where service is SPDatabaseServiceInstance
let dbInstance = (SPDatabaseServiceInstance) service
select dbInstance.NormalizedDataSource;
If you have multiple names for your sql server (like aliases) the returned collection will have multiple entries. Just take the first of the collection.
精彩评论