Connection Strings

Connection Strings

configSource Attribute

  • The connectionStrings setting specifies a collection of database connection strings
  • The collections is specified as name/value pairs 
  • Saving a Web.config file restarts the web application
  • You can alternatively use the configSource attribute of individual section elements to point to a secondary configuration file that does not cause an application restart when it is changed


Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Settings
  • Sitecore.Context
  • Sitecore.Data.Items.Item

Connection Strings Configuration

  • <configuration>
    • <sitecore>
      • <settings>
        • <connectionStrings configSource="App_Config\ConnectionStrings.config">

Connection Strings C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string coreConnectStringExists = Sitecore.Configuration.Settings.ConnectionStringExists("core").ToString();
string masterConnectStringExists = Sitecore.Configuration.Settings.ConnectionStringExists("master").ToString();
string webConnectStringExists = Sitecore.Configuration.Settings.ConnectionStringExists("web").ToString();
string analyticsConnectStringExists = Sitecore.Configuration.Settings.ConnectionStringExists("analytics").ToString();
string fooConnectStringExists = Sitecore.Configuration.Settings.ConnectionStringExists("foo").ToString();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using &#58; for colon and &#63; for question markand &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#63;&#160;{1}", "Core Connection String", "<b>" + coreConnectStringExists + "</b></br>");
output.Write("{0}&#63;&#160;{1}", "Master Connection String", "<b>" + masterConnectStringExists + "</b></br>");
output.Write("{0}&#63;&#160;{1}", "Web Connection String", "<b>" + webConnectStringExists + "</b></br>");
output.Write("{0}&#63;&#160;{1}", "Analytics Connection String", "<b>" + analyticsConnectStringExists + "</b></br>");
output.Write("{0}&#63;&#160;{1}", "Foo Connection String", "<b>" + fooConnectStringExists + "</b>");

output.RenderEndTag(); //Div


Connection Strings C# Web Control Output

Core Connection String? True
Master Connection String? True
Web Connection String? True
Analytics Connection String? False
Foo Connection String? False

Sources