Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.