In this blog we are going to see how to access database and insert records using Logic App. Azure Logic App provides many inbuild connectors and SQL server is one of them. Using this SQL Database connector, we can easily perform CRUD operations. Having said that, Logic app has other DB connectors as well. In our case, we will use SQL server Database connector.
Let’s get
started.
Create a SQL Server Database Instance:
First, we
have to create a SQL server DB instance so that we can connect that from Logic APP.
We can choose the SQL server service in the Azure resources and provide some
details like server name, location, username and password as mentioned below.
Once we have that in place, we can whitelist our IP address to access it from our machine.
I have created a table with the name of “emp_details” and we will use this table to store information from the Logic APP.
Please make EmpID as primary key as we need it for next tutorial (I haven’t made it as primary key here in above screenshot but that has been changed with primary key.)
Create a Logic APP:
Let’s
create a Logic APP, I have given “db-access-demo” and choose Trigger as HTTP.
Use method as POST so that we can post a body from HTTP URL to insert records
in database.
As a payload schema use below example:
{
"EmpId":"string",
"EmpName":" string ",
"EmpAddress":" string ",
"Qualification":" string "
}
Click on
the Next step and search for “sql server” in connector search area as mentioned
below. Once that is visible choose “Insert row (V2)” Action.
It will ask you to create a connection, where you have to provide database connection details like username, password, server url etc. once provided click on create.
If all good, then you can see the details visible in droop down like database name and table name.
Choose “emp_details” as table and select all the parameter as we are going to insert all 4 details of the employee.Select and map the parameter which is coming from the HTTP POST body in the request to the Database column as per below screenshot.
Add a “Response” connector, where we will provide the HTTP response body, for now we are using static one.
Save the logic app and try running it from the postman, you should be getting 200 success response similar to below screenshot
You can use the similar example that should match with your DB table structure and schema that you defined in HTTP connector
{
"EmpId":"111",
"EmpName":"Shri",
"EmpAddress":"Wellington",
"Qualification":"IT"
}
Since we haven’t received any error these records should be inserted into the Database. So execute query to see the record and you should see that records have been inserted from the Logic APP
You can also see in the Logic APP logs to analyzed that what happened in each connector.
No comments:
Post a Comment