2

In the code behind of an application page, I need to query a SQL table from a database which is on the same server and instance as the SharePoint content and config databases.

Is there an easy way to build a farm independant connection string by code, so that the application page can be deployed without changes to a different farm with similar coonfiguration and database?

6
  • 3
    Why don't you just add a connection string to the web.config file? Not even SharePoint dependent. Commented Mar 6, 2014 at 15:38
  • You should consider using stored procedure and typed datasets, asp.net/web-forms/tutorials/data-access/…, so when you deploy your application page, just update dataaccess on other farm's wfe server Commented Mar 6, 2014 at 15:43
  • Because I don't know the connection string on the target platform and I want to request it from the system.
    – elsni
    Commented Mar 6, 2014 at 15:49
  • @Muhammad Raja: The SQL table is only for data transfer from/to SAP. Flat, no stored procedures and other fancy stuff. I have no influence on that.
    – elsni
    Commented Mar 6, 2014 at 15:50
  • to a different farm with similar coonfiguration and database? Connection String is combination of username and password, serverName and database, you can't request it from anywhere... Commented Mar 6, 2014 at 16:09

2 Answers 2

1

Since SharePoint Content database connection string is stored in Windows Registry, the following example demonstrates how to read Data Sourcefrom a SharePoint Content Db connection string:

    /// <summary>
    /// Get SharePoint Content Db DataSource (SharePoint 2010)
    /// </summary>
    /// <returns></returns>
    private static string ReadContentDbDataSource()
    {
        string connectionString = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\Secure\ConfigDb", "dsn", null);
        var builder = new SqlConnectionStringBuilder(connectionString);
        return builder.DataSource;
    } 
3
  • How supportable is this? If this isn't documented anywhere then MSFT may update this location at any time through an update.
    – James Love
    Commented Mar 6, 2014 at 20:53
  • 2
    It is referenced in official documentation, for example here technet.microsoft.com/en-us/library/cc678863(v=office.15).aspx ;) The only part that changes is subkey that corresponds to SharePoint version, for example for SharePoint 2013 it is 15.0 Commented Mar 6, 2014 at 21:05
  • Nicely found, just had to ask the standard supportability questions :)
    – James Love
    Commented Mar 7, 2014 at 8:25
0
Public connectMe = new System.Data.SqlClient.SqlConnection("Integrated Security=SSPI;Initial Catalog=TestDatabase;Data Source=(local);")

This connection string's options can be dissected as:

  • Integrated Security=SSPI; – This means we want to connect using Windows authentication

  • Initial Catalog=TestDatabase; – This means the database we want to first connect to is named “TestDatabase”.

  • Data Source=(local); – This means that we want to connect to the SQL Server instance located on the local machine.

http://blogs.msdn.com/b/sql_protocols/archive/2008/09/19/understanding-data-source-local-in-sql-server-connection-strings.aspx

aslong as the database name is the same its fine! the key is local i think what your looking for!

1
  • 4
    This isn't quite what the asker is looking for. They're looking for something that could be re-used on other farms. This particular option only works if the databases are hosted on the WFE server. In a typical multi-server farm, this won't work.
    – Ben Wyatt
    Commented Mar 6, 2014 at 18:18

Your Answer

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

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