Tutorial: Build a Sample App with Azure SQL Database

This tutorial guides you through building a simple web application that connects to and interacts with Azure SQL Database. We'll cover setting up your database, writing application code, and deploying your solution.

Prerequisites

Step 1: Create an Azure SQL Database Instance

First, we need to set up a SQL Database instance in Azure. This will be the backend for our application.

  1. Navigate to the Azure portal.
  2. Search for "SQL databases" and select it.
  3. Click "Create".
  4. Select a resource group or create a new one.
  5. Provide a database name (e.g., MySampleDB).
  6. Choose a server. You can create a new logical server or use an existing one.
  7. Configure server settings, including server admin login and password. Store these credentials securely.
  8. Select a compute and storage configuration. For this tutorial, the basic tier is sufficient.
  9. Review and create the database.

Step 2: Configure Firewall Rules

To allow your application to connect to the Azure SQL Database, you need to configure firewall rules.

  1. Once your database is deployed, navigate to its overview page in the Azure portal.
  2. Under "Settings", select "Firewalls and virtual networks".
  3. Click "Add a client IP" to add your current IP address. This is convenient for local development. For production, consider more secure connectivity options like private endpoints.
  4. Click "Save".
Note: For security best practices in production environments, avoid allowing all connections and instead configure specific IP addresses or use Azure Private Link.

Step 3: Connect to the Database and Create a Table

Now, let's connect to the database and create a simple table to store our application data.

You can use tools like Azure Data Studio, SQL Server Management Studio (SSMS), or programmatically connect.

Using Azure Data Studio (Recommended)

  1. Download and install Azure Data Studio.
  2. Open Azure Data Studio and create a new connection.
  3. Use the following connection details:
    • Server: Your logical server name (e.g., mysqldbserver.database.windows.net)
    • Authentication type: SQL Login
    • User name: Your server admin login
    • Password: Your server admin password
    • Database: Select your created database (e.g., MySampleDB)
  4. Click "Connect".
  5. Once connected, open a new query editor and run the following T-SQL script to create a TodoItems table:

CREATE TABLE TodoItems (
    Id INT PRIMARY KEY IDENTITY(1,1),
    Description NVARCHAR(255) NOT NULL,
    IsCompleted BIT DEFAULT 0
);

INSERT INTO TodoItems (Description) VALUES ('Learn Azure SQL Database');
INSERT INTO TodoItems (Description) VALUES ('Build a sample app');
INSERT INTO TodoItems (Description) VALUES ('Deploy to Azure');
            

Step 4: Build Your Sample Application

This section provides a conceptual overview. Specific code examples depend on your chosen technology stack. We'll assume a simple .NET Core web API for demonstration.

Conceptual Steps:

Tip: Consider using an Object-Relational Mapper (ORM) like Entity Framework Core for .NET, Sequelize for Node.js, or SQLAlchemy for Python to simplify database interactions.

Step 5: Run and Test Your Application

Run your application locally and test its functionality. Ensure you can add, retrieve, update, and delete data from your Azure SQL Database.

Next Steps

Deploy to Azure App Service

Learn how to deploy your completed application to Azure App Service for a fully managed web hosting experience.

Deploy a web app to Azure App Service

Secure Your Connection String

Explore best practices for managing secrets and connection strings using Azure Key Vault.

Secure application secrets with Azure Key Vault

Performance Tuning

Discover techniques to optimize the performance of your Azure SQL Database.

Performance best practices for Azure SQL Database