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?
In your web service, you can start beginning with "using" the SharePoint object model,
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;
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?.