2

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

2 Answers 2

5

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;
    }
Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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??
HI it is not getting the instance name in server farm??? is thee is other way to do this ??/
@Cute: I have updated the Answer already to get the Instance of the DB Server.
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
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.