Build applications to interact with Azure SQL server database

Introduction: All my articles are compiled into simple steps with detailed screen shots and elaborative description. By following these steps even, a novice programmer should be able to replicate scenario. Please go through the steps and provide your valuable feedback to make my next articles more clearer and intent.

Note: I took help of various internet sources while researching this item. Credit goes to internet and Microsoft communities. This is one of the topics in Azure certification.

This is my fourth article on Azure.

Please follow below links for my previous Azure articles

Azure WebApps

Azure Logic App

Azure Event Grids

After completing this blog, you will be able to do

  • Create and deploy SQL Server database in azure
  • How to develop applications to pull data from sql server

Sample Description: We are going to create a dot net core application which reads data from sql server, pulls images from blob container and displays data to user

Step 1: Open Azure portal (https://portal.azure.com)

Step 2: Create a new resource group by clicking on Resource Groups icon on home page.

What is Resource Group? – Resource group is a container which holds all resources for an azure application. It’s a kind of grouping and managing resources.

Step 3: Create a storage account as shown below. Later we will create 2 blob containers to store images and sql server backup file.

Step 4: Create a blob container and name it as images. Upload few images from your local machine. These images will be retrieved by a dot net core application in later steps. Access level I choose as anonymous so that I can access this blob from outside.

Step 5: Create another blob and name it as databases. Choose access level as private, so that you can not access this from outside. We will use this blob to store sql back up file. In later steps we will use this back up file and restore a database.

Step 6: Now we need to create  a sql server database. Click on Create Resource > Search for “Azure Sql” > Select & Click on Create. Then select Sql databases and create

Step 7: Create SQL database as shown below. In this step we will create Test database(which we never use). The main purpose of this step to create Sql server instance. Later we import a sql server database in this server. If you want you can Test database to create resources like tables, functions & stored procedures.

First, we need to create a database server. Click on Create new option under server field and create a new database server as shown below.

Step 8: While creating database in next step select below 2 options which provides access to this database.

Step 9: Select the database server we created in previous step and select “Firewalls and virtual networks” under security tab. Then click on Add Client IP button, which adds IP address of the client. This option is mandatory to provide access of this database server. Then save the changes.

Step 10: In this step we will create a new database by importing the back up file we uploaded in earlier step. Select database server and Click on Import database option.

Step 11: Select Storage account and select the database file from blob container.

Step 12:  To verify database restore, select the database and run below queries in sql query editor

Step 13: Go to Connection Strings tab and grab the ADO.NET connection string. Update the password with actual password and keep it secure. We will use this connection string later in code.

Step 14: Go to the image blob we created earlier and grab the URL

Step 15: Now we need to work on code side. By using visual studio code, open the Adventures work solution. As my focus is on azure, not going to details of dot net core application. You can get this code from Microsoft official git hub.

When you open application, it might ask to restore the packages, select and restore packages. Then open terminal and run “dotnet build” command, which will complete the restore and fix other errors (if exists).

Step 16: Open “Application.json” and assign the value of images blob we created in earlier steps to “BlobContainerUrl” property. This property will be used by application, to connect to blob and retrieve images.

Also create another property in application json to hold sql connection string we copied from the Azure sql database we created.

Your application json looks like below.

Step 17: Below is the snap shot of my sql context class which is responsible to get data from azure sql database. As I mentioned earlier, I am not going put more focus on code part. Please let me know if more explanation required for code.

Step 18: Below is the snap shot of UI, which gets images from blob container and displays to user.

Step 19: Now its time to build and deploy our code. Open new terminal and run dotnet build command.

Step 20: Navigate to web folder using command prompt and run “dotnet run” command. This command will run and deploy application locally in port number 5000.

Step 21: Open your browser and run the URL “http://localhost:5000/”. Application has been installed in your local, which is pulling data from Azure SQL and Blob data sources.

Happy Coding!!!