This post is about Azure SQL output binding for Azure Functions - this feature is preview right now. The output binding helps us to write data to SQL Server in Azure Functions. In the earlier post, we explored the Input binding which helps to read data from SQL Server. Azure function now offers SQL trigger as well, we will be discussing this in a later post.
We will be first creating a function which writes data to SQL database using the Output binding. In the create function screen, we need to choose the SQL output binding, set the SQL connection string name and finally the table name.
We can use the same Notes table we created last time to write data - since we already configured the dependencies, we can un select the Configure SQL output binding connection option. This will create an azure function like this. Sometimes we need to add reference of System.IO and Newtonsoft.Json to fix the compilation issues.
Please note, we wanted to write Notes object, but Azure Function gives a template with a ToDoItem class, we need to change it to Note class to make the function work properly. Here is the updated version of the code.
Now we can run the Azure function and execute a POST request like this.
If we want to insert data to multiple tables, we can do something like this. In this example we are inserting the data to access log table as well.
We can use multiple records from HTTP request and use it as well, in the DeserializeObject method, we need to use an array instead of single object and insert data using for loop.
This way we can use the SQL Server output binding in Azure Functions to write data to SQL Server database. In the next blog post we will explore the SQL Trigger option.