Description:
Here we are
going to see how to make an Azure Function app with SQL output Binding
by practical
Prerequisite:
- Azure
Subscription
- Visual Studio
2022
- Knowledge of CRUD
using C# with API is enough
-
Azure Functions are a serverless solution
that allows you to write less code, maintain less infrastructure,
and save on costs. Azure SQL bindings for Azure Functions make
integrating your SQL data with serverless applications easier than ever. Sql
Outout Binding will help to SAVE data to database More information we
can get from here Hope this is enough, let’s move into the practical session
Before going to main section, first we should have an Azure Sql
Database which I have already created, if you wana know about it please use this link to learn,
then after I have created a table below is the structure
of that
CREATE TABLE [dbo].[tblEmployee](
[employeeid] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](50) NOT NULL,
[lastName] [varchar](50) NULL,
[dob] [date] NOT NULL,
[sex] [char](10) NOT NULL,
CONSTRAINT
[PK_tblEmployee] PRIMARY KEY CLUSTERED
(
[employeeid] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
How to create Azure Functions from VS 2022?
1)
Open Visual Studio and choose Azure Functions by simply
typing function from the search menu and click Next
2) Type project name DemoFunctionAppSqlOutputBind and your preferred location then click Next
3) Select .Net 6.0 LTS Function as SQL output binding Connection
String as DbConnectionString table as [dbo].[tblEmployee] then click Next
4) Once the project is created then VS2022 automatically open to configure screens to connect the Sql DB, from there choose Azure SQL Database
5) One more time it will force you to Auth so, please do it with
azure subscription account then after it will list down the existing DB from
there we are going to choose jtestdb click Next
6) On the next screen Database connection string name as DbConnectionString
Database connection user name as adminj password {Your PWD} Connection
string value is automatically taken from our earlier screen selection so, let
it be then click Next
7) At the end we could see the changes going to make in our project by category so, just click Finish
8) Then after made some minor code changes they are
a)
Create a folder called Models from
the solution explorer
b)
Under Models create a class
called Employee it structure like below
public class Employee
{
public string firstName
{ get; set; }
public string lastName {
get; set; }
public DateTime
dob { get; set; }
public string sex { get; set; }
}
Then from Function1 code changed like
below
9) Next we are going to publish this simple project into Azure Portal
so, right click the project from Solution Explorer and click Publish
a) There choose Azure and click Next
b) Then choose Azure Function App (Windows) and click Next
c) From the next window select the Azure service which we have created already and click Finish
d) Now the configurations are completed; from the main screen Publish button then after it will take few seconds to deploy so, wait and see the logs in output window, when its completed then we could see the message as Function app is ready
10) Next, open your browser and login to www.azure.com then go to our Function app, in our case mine is azureSqlOutputbinding open it, in the left pane click Functions like below
11) From there click the deployed Function app which we did from VS2022
12) Then from the left page click Code + Test
13) Then click Test/Run from the top panel
14) Then from the right side a vertical pop up appears there choose
HTTP as POST then at the BODY paste
below JSON
{
"firstName" : "Jeyaseelan",
"lastName" : "Jeyabaskaran",
"dob" : "1990-12-25",
"sex" : "male"
}
15) Then after click Run button, that will use our azure function to post data to tblEmployee table
16) That’s in, in just 2 seconds we have seen the result code as 200 like below
17) To cross check, I have connected the azure db from local SSM and seen that the data is inserted
18) The same POST operation I have tried from Postman as well with
service key like below and confirmed that the data is successfully posted to
Azure Sql DB
Conclusion
It
seems that we have to do multiple steps but, Azure SQL Creation, Azure Function
app creation, Azure Function app configurations in VS2022 are just one
time work. So, we did the POST operation in few line of code and I no
need of any server also the performance is fast. I hope this helped you to
improve your app development speed.