Cute
Cute

Reputation: 14021

how to find the sqlserver name(data source) used by sharepoint?

Can u people Help me How to 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...

Upvotes: 2

Views: 3042

Answers (2)

Doomdrake
Doomdrake

Reputation: 11

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.

Upvotes: 0

Kusek
Kusek

Reputation: 5384

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;
    }

Upvotes: 5

Related Questions