This sample shows how to create REST API using Azure Function that read data from Azure SQL Database using FOR JSON clause.
About this sample
Before you begin
Run this sample
Sample details
Related links
- Applies to: Azure SQL Database, SQL Server 2016 (or higher)
- Key features: FOR JSON clause in SQL Server 2016/Azure SQL Database
- Programming Language: C#
- Authors: Jovan Popovic
To run this sample, you need the be able to create Azure SQL Database and Azure Function.
To run this sample, you need to download source code from SQL Server GitHub account, or copy the content of files directly from GitHub using browser.
- Create Azure SQL Database using Azure Portal, SQL Server Management Studio, or other tools.
-
Create Azure Function using Azure Portal. In the list of templates choose C#/Http Webhook as a type.
-
Add data-access NuGet package. Click on the Files link on the righ-hand side, and upload [project.json[(azure-function/project.json) file into your Azure Function. This file contains a reference to the Data Access library that will be used to get the data from Azure SQL Database.
-
Setup connection to your database. Click on manage link in Azure Function, and open settings of your Azure Function application. Scroll down to the connection string section, add a key azure-db-connection and put the connection string to your dataase as a value.
-
Modify C# code in your Azure Function (Run.csx file). Put the code in the run.csx file in your Azure Function.
- Modify query in the code to create different REST API.
In this sample is created one Azure Function that is called via URL, calls Azure SQL Database, and returns query result formatted as JSON. This is can be used to implement of REST API using Azure Function on Azure SQL Database. Azure Function returns response to the caller using HttpResponseMessage class.
var httpStatus = HttpStatusCode.OK;
string body =
await (new QueryMapper(ConnectionString)
.OnError(ex => { httpStatus = HttpStatusCode.InternalServerError; }))
.GetStringAsync("select * from sys.objects for json path");
return new HttpResponseMessage() { Content = new StringContent(body), StatusCode = httpStatus };
QueryMapper is a class that maps results of SQL Query to some result. In this example, QueryMapper uses GetStringAsync method to asynchrously execute SQL query and map results to string that will be returned as a result of REST API call. On the QueryMapper object is added OnError handler that will set Internal Server Error code in the response if some error happens during the query execution (this is optional setting).
You can find more information about the technologies that are used in this sample on these locations:
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
These samples and templates are all licensed under the MIT license. See the license.txt file in the root.
Email questions to: [sqlserversamples@microsoft.com](mailto: sqlserversamples@microsoft.com).