tags:

views:

105

answers:

1

Hi,

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...

+3  A: 

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;
    }
Kusek
Here I am getting the server name as "Demo-64-Test".But i have installed sqlserver as Named instance. "Demo-64-Test\SQLNamed".I have to get this one.
Cute
@cute: I have updated the code to get the Instance.
Kusek
@Kusek:Thnks It Helps me.But i have a Doubt.If i Installed The sqlserver as Default one then it gives the Default instance only??is is possible with above code??
Cute
HI it is not getting the instance name in server farm???is thee is other way to do this ??/
Cute
@Cute: I have updated the Answer already to get the Instance of the DB Server.
Kusek
the same i have used but i am not getting it says the role as invalid for the machine whre my named sqlserver insatnce is installed in server farm
Cute