How to: Find SharePoint’s Database server and instance

Here’s an easy way to get the server and instance name of the database server your SharePoint server is using. It’s very useful for constructing connection strings on the fly (hint, hint).

public string FindSPSQLServerName()
     String sServerName = "";
     SPWebService service = SPFarm.Local.Services.GetValue();

     SPDatabaseServiceInstance s = service.DefaultDatabaseInstance;
     sServerName = s.Server.DisplayName + "\" + s.Instance;
     return sServerName;

Comments are closed.

Post Navigation

Switch to our mobile site