Defining and Using Connection Strings in Azure Websites

Microsoft Azure is a versatile cloud computing environment with not just only best performance and speed but is renowned for its flexibility too. Being a Web Developer it’s a hard time to change any of the connection strings or use it while users are using your site. It is never recommended that we should define connection strings in our code. So where should we? Well Microsoft Azure solves that problem. Today we will see how to define Connection Strings and use it in .NET Web App.

For this demo purpose I will use a basic ASP.NET website. If you are new to creating Azure Websites please follow the blog of Anish Ansari to start with it.

Defining Connection String in Azure Websites

Step 1. In your Azure Portal go to your website’s dashboard then click on Configure Tab.

Go to Configure Tab

Figure 1: Go to Configure Tab

Step 2. Scroll down to Connection Strings.

Step 3. In the connection strings list. Type a name for the connection string in text box with the placeholder text Name, type the Value or connection string of the database in the text box which has Value as placeholder text. Then at last select the type of the connection string. For our demo purpose we will use SQL Database.

Note: Our Demo Connection string is Data.,1433;Initial Catalog=DatabaseName;Integrated Security=False;User Id=username@servername;Password=password;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True

Entering Connection Strings for SQL Database

Figure 2: Entering Connection Strings for SQL Database

Step 4. Click Save on the command bar to apply all the changes.

Click Save

Figure 3: Click Save

Using a connection string from .NET

Now that we have created and saved a sample connection string of the database, we will retrieve it in our ASP.NET Site which I created earlier. I assume you have already created an Azure .NET Web App or ASP.NET site.

Step 1. Open your project and your page where you want the connection string to be retrieved. (I have it on default.aspx).

Step 2. Enter the following code snippet in default.aspx.cs page.

using System;   
using System.Configuration; // You need this declared  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace ravitestwebsite  
    public partial class _default : System.Web.UI.Page  
        protected void Page_Load(object sender, EventArgs e)  
            string key = "teststring";  
            string value = ConfigurationManager.ConnectionStrings[key].ConnectionString;  
            connectionLabel.Text = value;  

In default.aspx page add a label in the site as in the following.

<asp:Label runat="server" id="connectionLabel"></asp:Label>  

Step 3. Now publish your site to Azure and run it.

Connection String Retrieved

Figure 4: Connection String Retrieved

As shown using Connection Strings in Azure makes your task much easier and reliable, it also helps from vulnerability. You can make your site much secure with this. Now if you need to change the database you don’t need to check on the code.

You can check the source codes I provided with this article.