Azure Function : A quick start with database - Blogs

Azure Function : A quick start with database

Azure Function is a solution for running small piece of code or a "Function" in cloud. You can write just the code need for the problem without worrying about the infrastructure or the application to run it. Function can make your development more productive and you can choose your development language of choice such as C#, F#, node.js, java or PHP.

Pay only for the time your code runs. Azure Function lets you develop Serverless application on Microsoft Azure.

Feature of Azure Function:

  • Choice of Language
  • Pay-per-use pricing model
  • Bring your own dependencies
  • Integrated Security
  • Simplified integration
  • Flexible Development
  • Open Source

Function is great solution for processing data, integration system and working with internet-of-things(IoT) and building simple APIs and Microservices.

Function provide templates to get you started with key scenarios.

  • Http Triggers
  • Timer Triggers
  • GitHub Webhook
  • Generic Webhook
  • CosmosDB Trigger
  • Blob Trigger
  • Queue Trigger
  • Event Hub Trigger
  • Service bus Queue Trigger
  • Service bus Topic Trigger

Here i'm going to explain how to create simple Azure Function in Azure Portal.

Go to Azure Portal, click on New then search for Function App.


Select the Function app in Result shown. Then click on Create Button.


Provide Required details and click on Create


App will be created and the app details will be shown.


Hover cursor on Functions and click on + icon and choose webhook+Api and language C# click on Create this function button5

You will see the following screen containing a Function. Notice that Get function URL on the right  top corner of the screen.


Click on that and copy the URL in your browser address bar and hit Enter. You will see the following message. "Please pass a name on the query string or in the request body" 

Now append this &name=Azure at the end of the URL and hit enter. You will see the following message. "Hello azure".

Now create a database in Azure

Go to:  Resource Groups >> Add >>

Select SQL Database

Click on "Create" Button

Provide all the required details and click on create button.

Once the database is created go to that database, click on show connection string and copy the connection string, it will be required.



Provide your username and password which you have given while creating SQL Database.

Connect to your database using SQL Server or Visual studio. Create table using following script.

CREATE TABLE [dbo].[LogRequest]( [ID] [int] IDENTITY(1,1) NOT NULL, [Log] [varchar](max) NULL)

Now go to your function app. Click on [+ New Function ] at the top.


Select "Http Trigger" function and language C#. Provide function name as "HttpTriggerDatabase" then click on create.


Once the function is created, Go to that function and Click on "View Files" on the Right side Blade. To create a file, Click on [+ Add] Button. Give a file name as "Project.json" and Hit Enter [ ↵ ] Paste the following in that file.

 "frameworks": {
	      "dependencies": {
	        "Dapper": "1.42.0",

Click "Save" Button. Now open your function and paste the following code.



	using Microsoft.Azure.WebJobs.Host;
	using System.Configuration;
	using System.Data.SqlClient;
	using System.Net;
	using System.Net.Http;
	using System.Threading.Tasks;
	using Dapper;
	        public static async Task Run(HttpRequestMessage req, TraceWriter log)
	            log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
	            var successful = true;
	                var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
	                using (SqlConnection connection = new SqlConnection(cnnString))
	                    var rLog = await req.Content.ReadAsAsync();
	                    // insert a log to the database
	                    string query = "INSERT INTO [dbo].[LogRequest] ([Log])  VALUES (@Log)";
	                    using (SqlCommand cmd = new SqlCommand(query, connection))
	                        cmd.Parameters.AddWithValue("@Log", "Log1");
	                successful = false;
	            return !successful
	                ? req.CreateResponse(HttpStatusCode.BadRequest, "Unable to process your request!")
	                : req.CreateResponse(HttpStatusCode.OK, "Data saved successfully!");
	        public class LogRequest
	            public int Id { get; set; }
	            public string Log { get; set; }

Now configure your database connection for your azure function.



Go to Application Setting. Scroll down up to "Connection String"


Give connection name as "SqlConnection" and paste your copied connection string in the value field. Select "SQL Azure" form the dropdown. Click save.

Go to your function and Click on Run button. You can see the output "Data saved successfully"


You can copy the URL from Get function URL  and browse, you will see the same output on the screen.

Thank you.



3 Strategies to Optimize Cloud Costs

Strategies to Reduce Cloud expenses and related best practices.

Read More >

Azure App Service Scaling Features And Capabilities

How to scale the application in Azure App Service. There are two workflows for scaling, scale up and...

Read More >

Azure App Service (Build And Host Web Apps, Mobile-Back Ends Without Managing Infrastructure)

In this blog we will see what is Azure App Service, and it’s  features and why do we use it, we can...

Read More >

Introduction to Azure DevOps Server (TFS) and Azure DevOps Services (VSTS)

In this blog we will talk about introduction to Azure DevOps Sever earlier called as Team Foundation...

Read More >

DevOpSmartBoard - Azure Pipelines

DevOpSmartBoard -The Ultimate End-To-End and One Stop Dashboard solution for all the reporting needs...

Read More >

DevOpSmartBoard- Multiple Team Capacity

DevOpSmartBoard -The Ultimate End-To-End and One Stop Dashboard solution for all the reporting needs...

Read More >

VMSS deployment Using Custom VM Image

In this blog we will see the VMSS update by using an image with Rolling upgrade policy.Prerequisites...

Read More >

Azure DevOps - “Sign-in required” issue while trying to assign user to a work item

Scenario – You are trying to assign a work item to your colleague but unable to assign as it shows ...

Read More >

Roles in DevOps Mobile App Development

DevOps can be defined as an approach to enable seamless application delivery from inception to produ...

Read More >

What is DevOps and why Enterprises are serious about it?

The term DevOps is becoming increasingly familiar as more and more companies are interested in imple...

Read More >


Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive

Contact Us
  • *
  • *