|
| 1 | +--- |
| 2 | +title: Create Azure Functions with the SQL bindings extension for Visual Studio Code |
| 3 | +description: Use the mssql extension for Visual Studio Code to edit and run Transact-SQL scripts for SQL Server on Linux. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.prod: sql |
| 6 | +ms.technology: tools-other |
| 7 | +author: VasuBhog |
| 8 | +ms.author: vabhog |
| 9 | +ms.reviewer: drskwier |
| 10 | +ms.date: 8/24/2022 |
| 11 | +--- |
| 12 | + |
| 13 | +# Create Azure Functions with the SQL bindings extension for Visual Studio Code |
| 14 | + |
| 15 | +[!INCLUDE [SQL Server ASDB, ASDBMI, ASDW ](../../includes/applies-to-version/sql-asdb-asdbmi-asa.md)] |
| 16 | + |
| 17 | +Azure Functions support for [SQL bindings](https://aka.ms/sqlbindings) is available in preview for input and output bindings, making connecting an Azure SQL database or SQL Server database to Azure Functions easier. The SQL bindings extension for Visual Studio Code (VS Code) facilitates the process of developing Azure Functions with SQL bindings and is automatically installed with the [mssql extension for VS Code](https://aka.ms/mssql-marketplace) extension pack. This article shows how the [SQL bindings extension](https://marketplace.visualstudio.com/items?itemName=ms-mssql.sql-bindings-vscode) for Visual Studio Code can be used to create Azure Functions with SQL bindings. |
| 18 | + |
| 19 | +## Getting Started with SQL Bindings |
| 20 | + |
| 21 | +> [!NOTE] |
| 22 | +> Currently, the SQL bindings extension only supports C# Azure Functions. JavaScript and Python Azure Functions support SQL bindings but are not supported by the SQL bindings extension at this time. |
| 23 | +
|
| 24 | +### From the Object Explorer |
| 25 | +To create an Azure Function from a specific `Table` or `View` in object explorer (OE), right-click on a table or view from a connected server in SQL Serve object explorer and select `Create Azure Function with SQL Binding.` |
| 26 | + |
| 27 | +Table OE Command: |
| 28 | +:::image type="content" alt-text="Screenshot of object explorer context menu to add a SQL binding from Table." source="./media/create-azure-function-with-mssql/create-function-table-object-explorer.png"::: |
| 29 | + |
| 30 | +View OE Command: |
| 31 | +:::image type="content" alt-text="Screenshot of object explorer context menu to add a SQL binding from View." source="./media/create-azure-function-with-mssql/create-function-view-object-explorer.png"::: |
| 32 | + |
| 33 | +If you haven't yet created the Azure Function project, a VS Code prompt will appear to aid in creating a new Azure Function project. |
| 34 | + |
| 35 | +:::image type="content" alt-text="VS Code notification to create a new azure function project since none were found in folder" source="./media/create-azure-function-with-mssql/create-azure-function-project.png"::: |
| 36 | + |
| 37 | +The extension will then ask you to select the folder where you want to create the Azure Function. |
| 38 | + |
| 39 | +:::image type="content" alt-text="Prompt to choose folder to create azure function with SQL binding to" source="./media/create-azure-function-with-mssql/select-folder-to-use-for-function.png"::: |
| 40 | + |
| 41 | +If you're creating an Azure Function with SQL binding from a table, the extension will prompt you to select the binding type to use, either an `Input` (Retrieves data from a database) or `Output` (Save data to a database) binding. |
| 42 | + |
| 43 | +> [!NOTE] |
| 44 | +> Azure Function with SQL Binding from a `View` is only supported for `Input` bindings. |
| 45 | +
|
| 46 | +:::image type="content" alt-text="Prompt to select binding type" source="./media/create-azure-function-with-mssql/binding-type-prompt.png"::: |
| 47 | + |
| 48 | +The extension will then prompt you to enter the function name to be used for the Azure Function. |
| 49 | + |
| 50 | +:::image type="content" alt-text="Prompt to enter function name" source="./media/create-azure-function-with-mssql/function-name-prompt.png"::: |
| 51 | + |
| 52 | +If you already have connection strings stored in the local.settings.json, then the extension will prompt you to select the connection string to use for the Azure Function or create a new connection string. |
| 53 | + |
| 54 | +:::image type="content" alt-text="Prompt to select connection string setting" source="./media/create-azure-function-with-mssql/create-new-sql-connection-string-setting.png"::: |
| 55 | + |
| 56 | +If you `Create new local app setting` then the extension will prompt you to enter the connection string name and value. |
| 57 | + |
| 58 | +:::image type="content" alt-text="Prompt to enter connection string" source="./media/create-azure-function-with-mssql/enter-connection-string-setting-name.png"::: |
| 59 | + |
| 60 | +If you're creating the `Azure Function with SQL Binding` to an existing Azure Function project, then the extension will prompt you whether you would like to include the password for the connection string in the local.settings.json file. |
| 61 | + |
| 62 | +:::image type="content" alt-text="Prompt to save the password to the SQL connection string" source="./media/create-azure-function-with-mssql/password-prompt.png"::: |
| 63 | + |
| 64 | +## <a name="passwordprompt"/></a> |
| 65 | +If `Yes`, then the password will be saved to the local.settings.json file. If `No` then the extension will warn you that the password won't be saved to the local.settings.json file, and you'll need to manually add the password later to the local.settings.json file. |
| 66 | + |
| 67 | +:::image type="content" alt-text="Warning to add password to SQL connection string later manually" source="./media/create-azure-function-with-mssql/do-not-save-password.png"::: |
| 68 | + |
| 69 | +The extension will then prompt you to provide the namespace for the Azure Function. |
| 70 | +:::image type="content" alt-text="Warning to add password to SQL connection string later manually" source="./media/create-azure-function-with-mssql/namespace-for-function.png"::: |
| 71 | + |
| 72 | +A progress notification will appear to indicate that the Azure Function is being created. |
| 73 | + |
| 74 | +If you're creating a brand new Azure function project with SQL binding, then the extension will prompt whether you would like to include the password for the connection string in the local.settings.json file (discussed [here](#passwordprompt)). |
| 75 | + |
| 76 | +### From the Command Palette |
| 77 | +Run the `MS SQL: Create Azure Function with SQL Binding` command from the command palette to create a new function with a SQL binding. |
| 78 | + |
| 79 | +:::image type="content" alt-text="VS Code notification to create a new azure function project since none were found in folder" source="./media/create-azure-function-with-mssql/create-azure-function-using-command-palette.png"::: |
| 80 | + |
| 81 | +The extension will then prompt you to select the object type to insert (`Input binding`) or upsert into (`Output binding`), either a `Table` or `View`. |
| 82 | + |
| 83 | +:::image type="content" alt-text="Prompt to select the object type" source="./media/create-azure-function-with-mssql/select-object-type.png"::: |
| 84 | + |
| 85 | +Then the extension will prompt you to select a connection profile to use for the Azure Function or create a connection profile. |
| 86 | + |
| 87 | +:::image type="content" alt-text="Prompt for connection profile" source="./media/create-azure-function-with-mssql/prompt-for-connection-profile.png"::: |
| 88 | + |
| 89 | +Once you either select a connection profile or create a new connection profile, the extension will prompt you to select the database from the selected connection to use for the Azure Function. |
| 90 | + |
| 91 | +:::image type="content" alt-text="Prompt for connection profile" source="./media/create-azure-function-with-mssql/select-database.png"::: |
| 92 | + |
| 93 | +Once you select a database, the extension will prompt you to select a table, or view to use or to enter a table or view to query or upsert into. This prompt is based on the object type you selected earlier. |
| 94 | + |
| 95 | +:::image type="content" alt-text="Prompt for table" source="./media/create-azure-function-with-mssql/select-table.png"::: |
| 96 | + |
| 97 | +:::image type="content" alt-text="Prompt for view" source="./media/create-azure-function-with-mssql/select-view.png"::: |
| 98 | + |
| 99 | +The extension will then prompt you to enter the function name to be used for the Azure Function. |
| 100 | + |
| 101 | +:::image type="content" alt-text="Prompt to enter function name" source="./media/create-azure-function-with-mssql/function-name-prompt.png"::: |
| 102 | + |
| 103 | +If you already have connection strings stored in the local.settings.json, then the extension will prompt you to select the connection string to use for the Azure Function or create a new connection string. |
| 104 | + |
| 105 | +:::image type="content" alt-text="Prompt to select connection string setting" source="./media/create-azure-function-with-mssql/create-new-sql-connection-string-setting.png"::: |
| 106 | + |
| 107 | +If you `Create new local app setting` then the extension will prompt you to enter the connection string name and value. |
| 108 | + |
| 109 | +:::image type="content" alt-text="Prompt to enter connection string" source="./media/create-azure-function-with-mssql/enter-connection-string-setting-name.png"::: |
| 110 | + |
| 111 | +If you're creating the `Azure Function with SQL Binding` to an existing Azure Function project, then the extension will prompt you whether you would like to include the password for the connection string in the local.settings.json file. |
| 112 | + |
| 113 | +:::image type="content" alt-text="Prompt to save the password to the SQL connection string" source="./media/create-azure-function-with-mssql/password-prompt.png"::: |
| 114 | + |
| 115 | +## <a name="passwordprompt2"/></a> |
| 116 | +If `Yes`, then the password will be saved to the local.settings.json file. If `No` then the extension will warn you that the password won't be saved to the local.settings.json file, and you'll need to manually add the password later to the local.settings.json file. |
| 117 | + |
| 118 | +:::image type="content" alt-text="Warning to add password to SQL connection string later manually" source="./media/create-azure-function-with-mssql/do-not-save-password.png"::: |
| 119 | + |
| 120 | +The extension will then prompt you to provide the namespace for the Azure Function. |
| 121 | +:::image type="content" alt-text="Warning to add password to SQL connection string later manually" source="./media/create-azure-function-with-mssql/namespace-for-function.png"::: |
| 122 | + |
| 123 | +A progress notification will appear to indicate that the Azure Function is being created. |
| 124 | + |
| 125 | +If you're creating a brand new Azure function project with SQL binding, then the extension will prompt whether you would like to include the password for the connection string in the local.settings.json file (discussed [here](#passwordprompt2)). |
| 126 | + |
| 127 | + |
| 128 | +### In an existing Azure Function |
| 129 | +Open the C# Azure Function in an editor and then run the `MS SQL: Add SQL Binding` command from the command palette to add a SQL binding to an existing function. |
| 130 | +:::image type="content" alt-text="VS Code notification to create a new azure function project since none were found in folder" source="./media/create-azure-function-with-mssql/add-sql-binding-command-palette.png"::: |
| 131 | + |
| 132 | +The extension will then prompt you to select Azure function in the current file to add the SQL binding to. |
| 133 | +:::image type="content" alt-text="VS Code notification to create a new azure function project since none were found in folder" source="./media/create-azure-function-with-mssql/select-azure-functions-in-file.png"::: |
| 134 | + |
| 135 | +If you're creating an Azure Function with SQL binding from a table, the extension will prompt you to select the binding type to use, either an `Input` (Retrieves data from a database) or `Output` (Save data to a database) binding. |
| 136 | + |
| 137 | +# TO DO |
| 138 | + |
| 139 | +## Generate Code for Azure Functions with SQL Bindings |
| 140 | + |
| 141 | +**The code generated for SQL Input Binding:** |
| 142 | + |
| 143 | +```csharp |
| 144 | +using System; |
| 145 | +using System.Collections.Generic; |
| 146 | +using Microsoft.AspNetCore.Http; |
| 147 | +using Microsoft.AspNetCore.Mvc; |
| 148 | +using Microsoft.Azure.WebJobs; |
| 149 | +using Microsoft.Azure.WebJobs.Extensions.Http; |
| 150 | +using Microsoft.Extensions.Logging; |
| 151 | + |
| 152 | +namespace Company.Function |
| 153 | +{ |
| 154 | + public static class dboEmployees |
| 155 | + { |
| 156 | + // Visit https://aka.ms/sqlbindingsinput to learn how to use this input binding |
| 157 | + [FunctionName("dboEmployees")] |
| 158 | + public static IActionResult Run( |
| 159 | + [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, |
| 160 | + [Sql("SELECT * FROM [dbo].[Employees]", |
| 161 | + CommandType = System.Data.CommandType.Text, |
| 162 | + ConnectionStringSetting = "SqlConnectionString")] IEnumerable<Object> result, |
| 163 | + ILogger log) |
| 164 | + { |
| 165 | + log.LogInformation("C# HTTP trigger with SQL Input Binding function processed a request."); |
| 166 | + |
| 167 | + return new OkObjectResult(result); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +**The code generated for SQL Output Binding:** |
| 174 | + |
| 175 | +```csharp |
| 176 | +using System; |
| 177 | +using System.Threading.Tasks; |
| 178 | +using Microsoft.AspNetCore.Http; |
| 179 | +using Microsoft.AspNetCore.Mvc; |
| 180 | +using Microsoft.Azure.WebJobs; |
| 181 | +using Microsoft.Azure.WebJobs.Extensions.Http; |
| 182 | +using Microsoft.Extensions.Logging; |
| 183 | + |
| 184 | +namespace Company.Function |
| 185 | +{ |
| 186 | + public static class dboEmployees |
| 187 | + { |
| 188 | + // Visit [https://aka.ms/sqlbindingsoutput] to learn how to use this output binding |
| 189 | + [FunctionName("dboEmployees")] |
| 190 | + public static CreatedResult Run( |
| 191 | + [HttpTrigger(AuthorizationLevel.Function, "get", Route = "addtodoitem")] HttpRequest req, |
| 192 | + [Sql("[dbo].[Test2]", ConnectionStringSetting = "NewSQLConnectionString")] out ToDoItem output, |
| 193 | + ILogger log) |
| 194 | + { |
| 195 | + log.LogInformation("C# HTTP trigger with SQL Output Binding function processed a request."); |
| 196 | + |
| 197 | + output = new ToDoItem |
| 198 | + { |
| 199 | + Id = "1", |
| 200 | + Priority = 1, |
| 201 | + Description = "Hello World" |
| 202 | + }; |
| 203 | + |
| 204 | + return new CreatedResult($"/api/addtodoitem", output); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + public class ToDoItem |
| 209 | + { |
| 210 | + public string Id { get; set; } |
| 211 | + public int Priority { get; set; } |
| 212 | + public string Description { get; set; } |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
0 commit comments