|
| 1 | +# Rest API with Azure Functions and Azure SQL Database |
| 2 | + |
| 3 | +This sample shows how to create REST API using Azure Function that read data from Azure SQL Database using FOR JSON clause. |
| 4 | + |
| 5 | +## Contents |
| 6 | + |
| 7 | +[About this sample](#about-this-sample)<br/> |
| 8 | +[Before you begin](#before-you-begin)<br/> |
| 9 | +[Run this sample](#run-this-sample)<br/> |
| 10 | +[Sample details](#sample-details)<br/> |
| 11 | +[Related links](#related-links)<br/> |
| 12 | + |
| 13 | +<a name=about-this-sample></a> |
| 14 | + |
| 15 | +## About this sample |
| 16 | + |
| 17 | +- **Applies to:** Azure SQL Database, SQL Server 2016 (or higher) |
| 18 | +- **Key features:** FOR JSON clause in SQL Server 2016/Azure SQL Database |
| 19 | +- **Programming Language:** C# |
| 20 | +- **Authors:** Jovan Popovic |
| 21 | + |
| 22 | +<a name=before-you-begin></a> |
| 23 | + |
| 24 | +## Before you begin |
| 25 | + |
| 26 | +To run this sample, you need the be able to create Azure SQL Database and Azure Function. |
| 27 | + |
| 28 | +<a name=run-this-sample></a> |
| 29 | + |
| 30 | +## Run this sample |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +### Setup Azure SQL Database |
| 35 | + |
| 36 | +1. Create Azure SQL Database using Azure Portal, SQL Server Management Studio, or other tools. |
| 37 | + |
| 38 | +### Setup Azure Function |
| 39 | + |
| 40 | +1. Create Azure Function using Azure Portal. In the list of templates choose C#/Http Webhook as a type. |
| 41 | + |
| 42 | +2. 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. |
| 43 | + |
| 44 | +3. 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. |
| 45 | + |
| 46 | +4. Modify C# code in your Azure Function (Run.csx file). Put the code in the [run.csx](azure-function/run.csx) file in your Azure Function. |
| 47 | + - Modify query in the code to create different REST API. |
| 48 | + |
| 49 | +<a name=sample-details></a> |
| 50 | + |
| 51 | +## Sample |
| 52 | + |
| 53 | +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. |
| 54 | +Azure Function returns response to the caller using HttpResponseMessage class. |
| 55 | + |
| 56 | +``` |
| 57 | +var httpStatus = HttpStatusCode.OK; |
| 58 | +string body = |
| 59 | + await (new QueryMapper(ConnectionString) |
| 60 | + .OnError(ex => { httpStatus = HttpStatusCode.InternalServerError; })) |
| 61 | + .GetStringAsync("select * from sys.objects for json path"); |
| 62 | +
|
| 63 | +return new HttpResponseMessage() { Content = new StringContent(body), StatusCode = httpStatus }; |
| 64 | +``` |
| 65 | + |
| 66 | +**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). |
| 67 | + |
| 68 | + |
| 69 | +<a name=related-links></a> |
| 70 | + |
| 71 | +## Related Links |
| 72 | + |
| 73 | +You can find more information about the technologies that are used in this sample on these locations: |
| 74 | +- [JSON support in Azure SQL Database](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-json-features). |
| 75 | +- [Webhooks in Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-a-web-hook-or-api-function). |
| 76 | + |
| 77 | +## Code of Conduct |
| 78 | +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. |
| 79 | + |
| 80 | +## License |
| 81 | +These samples and templates are all licensed under the MIT license. See the license.txt file in the root. |
| 82 | + |
| 83 | +## Questions |
| 84 | +Email questions to: [sqlserversamples@microsoft.com](mailto: sqlserversamples@microsoft.com). |
0 commit comments