共用方式為


使用適用於Visual Studio Code的 SQL 系結擴充功能建立 Azure Functions

適用於:SQL ServerAzure SQL 資料庫Azure SQL 受控執行個體Azure Synapse Analytics

Azure Functions 對 SQL 綁定 的支援在預覽中提供,適用於輸入與輸出綁定。 SQL 綁定讓將 Azure SQL 資料庫或 SQL Server 資料庫連接到 Azure Functions 變得更容易。 Visual Studio Code 的 SQL 繫結擴充功能簡化了使用 SQL 繫結開發 Azure Functions 的過程,並會隨 MSSQL 擴充功能套件 自動安裝。

本文說明如何使用 Visual Studio Code 的 SQL 系結延伸模組 來建立具有 SQL 系結的 Azure Functions。

注意

目前,SQL 系結擴充功能僅支援 C# Azure Functions。 JavaScript 和 Python Azure Functions 支援 SQL 系結,但目前 SQL 系結擴充功能不支援。

從物件總管

要在物件總管中從特定資料表或檢視建立 Azure 函式,請在 SQL Server 物件總管中右鍵點擊連接伺服器的表格或檢視表,然後選擇 「建立 Azure 函式與 SQL 綁定」。

表格物件瀏覽器指令

從資料表新增 SQL 繫結的物件總管捷徑功能表螢幕擷取畫面。

檢視物件探索器指令

從檢視新增 SQL 繫結的物件總管捷徑功能表螢幕擷取畫面。

欲了解更多資訊,請參閱 透過物件總管建立 Azure 函式,使用 Visual Studio Code 的 SQL Bindings 擴充功能

從命令面板

要建立帶有 SQL 綁定的新函式,請從指令面板執行 MS SQL:用 SQL 綁定建立 Azure 函 式指令。

使用命令面板建立新的 Azure 函式並使用 SQL 綁定的截圖。

如需詳細資訊,請參閱 透過命令選擇區,使用Visual Studio Code 的SQL系結擴充功能建立 Azure Functions。

在現有的 Azure 函數中

若要將 SQL 綁定加入現有函式,請在編輯器中開啟 C# Azure 函式。 接著,從指令調色盤執行 MS SQL: Add SQL Binding 指令。

使用 Command Palette 為現有 Azure 函式加上 SQL 綁定的截圖。

如需詳細資訊,請參閱 透過命令選擇區,使用Visual Studio Code 的SQL系結擴充功能建立 Azure Functions。

使用 SQL 系結為 Azure 函式產生的程式代碼

使用 SQL 輸入綁定生成的 Azure 函數程式碼

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public static class dboEmployees
    {
        // Visit https://aka.ms/sqlbindingsinput to learn how to use this input binding
    [FunctionName("dboEmployees")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            [Sql("SELECT * FROM [dbo].[Employees]",
            CommandType = System.Data.CommandType.Text,
            ConnectionStringSetting = "SqlConnectionString")] IEnumerable<Object> result,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger with SQL Input Binding function processed a request.");

            return new OkObjectResult(result);
        }
    }
}

Azure 函式中 SQL 輸出綁定所產生的程式碼

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public static class dboEmployees
    {
        // Visit [https://aka.ms/sqlbindingsoutput] to learn how to use this output binding
        [FunctionName("dboEmployees")]
        public static CreatedResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "addtodoitem")] HttpRequest req,
            [Sql("[dbo].[Test2]", ConnectionStringSetting = "NewSQLConnectionString")] out ToDoItem output,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger with SQL Output Binding function processed a request.");

            output = new ToDoItem
            {
                Id = "1",
                Priority = 1,
                Description = "Hello World"
            };

            return new CreatedResult($"/api/addtodoitem", output);
        }
    }

    public class ToDoItem
    {
        public string Id { get; set; }
        public int Priority { get; set; }
        public string Description { get; set; }
    }
}