Try Now
More in this section

Forums / General Discussions / Connection Strings in Code?

Connection Strings in Code?

2 posts, 0 answered
  1. AFTR
    AFTR avatar
    22 posts
    05 Oct 2010
    10 Mar 2011
    Link to this post

    I added a connection string to Sitefinity by going to Administration --> Settings --> Advanced --> Data --> Connection Strings.

    I am creating some custom user controls that I need to interact with a database other than the one Sitefinity uses to store all its data.

    How can I access the connection string I created above in code?

  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    01 Feb 2016
    10 Mar 2011
    Link to this post
    Hello Network,

    Bellow is some quick sample code which you can use to access connection strings defined in the data configurations:
    ConfigManager configManager = Config.GetManager();
    configManager.Provider.SuppressSecurityChecks = true;
    DataConfig dataConfig = configManager.GetSection<DataConfig>();
    //get the sitefinity element in the collection
    ConnStringSettings Sitefinity = dataConfig.ConnectionStrings["Sitefinity"];
    //get the connection string
    string connectionString = Sitefinity.ConnectionString;
    PagesConfig pagesConfig = configManager.GetSection<PagesConfig>();

    Radoslav Georgiev
    the Telerik team
2 posts, 0 answered