Asp.net Difference between appSettings and Connection Strings in web.config

In previous versions of ASP.NET, connection strings were stored in the appSettings. In ASP.NET 2.0, features, such as Session, Membership, Personalization, and Role Manager, rely on connection strings that are stored in the connectionStrings element. You can also use the connectionStrings element to store connection strings for your own applications.

The difference is that the connectionString section gives you strongly typed access to your connection strings through the ConfigurationManager class. It’s meant for connection strings specifically. The connectionStrings element specifies a collection of database connection strings, as name/value pairs, for ASP.NET applications and features.

A connectionString object is an XML node that has specific attributes to set; and semantically it refers to a database connection string.

Example:
<connectionStrings>
<clear/>
<add name=”LocalSqlServer” connectionString=”Data Source=(local);Initial Catalog=(DBName);Integrated Security=True” providerName=”System.Data.SqlClient” />
</connectionStrings>

You’ll notice it has a few different attributes:

  • name
  • connectionString : This has a specific string inside of it, it needs an Initial Catalog, a security mechanism (in this case Integrated Security
  • providerName

AppSettings is meant to store general settings in web.config is used to store server names, file paths, and other miscellaneous settings needed by an application.. You can use it to store connection strings also, but I recommend not doing that since there is a specific element for it in connectionStrings.

appSettings is just a user-defined Key-value pair that allows you to… well… set application settings.

It can be anything:

Example:
<appSettings>
<add key=”Email” value=”abc@abc.com”/>
<add key=”MasterKey” value=”True”/>
<add key=”GoogleAPI” value=”1234567890-AA”/>
</appSettings>

In many cases, it would just be odd to put the connectionString in a key-value pair like appSettings (semantically and programmatically). As well as it would make it more difficult to encrypt the connectionString when you need to.

Share this:
Share

Leave a Reply

Your email address will not be published. Required fields are marked *