Canarys | IT Services


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.

Leave a Reply

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

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.