SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing web service which will be part of my sharepoint solution. This service will save and read some data from custom db which will reside on the same mssql instance as sharepoint. I'd like my service to be reusable, so I'm looking for a way to get mssql instance name from sharepoint and use this to connect to DB. Have You ever done something like this before? Or maybe there is a better approach?

share|improve this question
up vote 2 down vote accepted

In your web service, you can start beginning with "using" the SharePoint object model,

using Microsoft.SharePoint;

And then, get the Database instance properties using the SPDatabaseServiceInstance object.

Sample code might look like this

SPSite siteCollection = SPContext.Current.Site;
SPContentDatabase contentDB = siteCollection.ContentDatabase;
SPDatabaseServiceInstance contentDBInstance = contentDB.ServiceInstance;
share|improve this answer

There is a chance that service will not have access to SPContext and using SPFarm.Local will make more sense. You could use solution proposed in how to find the sqlserver name(data source) used by sharepoint?.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.