Visual Studio Code 用の SQL バインド拡張機能を使った Azure 関数を作成する

適用対象: SQL Server Azure SQL データベース Azure SQL Managed Instance Azure Synapse Analytics

SQL バインドに対する Azure 関数サポートは、入力バインドと出力バインドについてのみプレビューで利用できます。これにより、Azure SQL データベースへの接続、または SQL Server データベースから Azure 関数への接続が容易になります。 Visual Studio Code (VS Code) 用の SQL バインド拡張機能を使用すると、SQL バインドを使った Azure 関数の開発プロセスが容易になります。これは、mssql for Visual Studio Code 拡張機能パックと共に自動的にインストールされます。 この記事では、Visual Studio Code 用の SQL バインド拡張機能を使用して、SQL バインドを使った Azure 関数を作成する方法について説明します。

注意

現在、SQL バインド拡張機能では C# Azure 関数のみがサポートされています。 JavaScript と Python の Azure 関数は SQL バインドをサポートしていますが、現在のところ、SQL バインド拡張機能ではサポートされていません。

オブジェクト エクスプローラーから

オブジェクト エクスプローラー (OE) で特定の Table または View から Azure 関数を作成するには、SQL Server オブジェクト エクスプローラーで、接続されているサーバーからテーブルまたはビューを右クリックし、Create Azure Function with SQL Binding. を選択します

テーブルの OE コマンド: オブジェクト エクスプローラーのコンテキスト メニューでテーブルから SQL バインドを追加しようとしている画面のスクリーンショット。

ビューの OE コマンド: オブジェクト エクスプローラーのコンテキスト メニューでビューから SQL バインドを追加しようとしている画面のスクリーンショット。

SQL バインドを使った Azure 関数を SQL Server オブジェクト エクスプローラーから作成する方法については、こちらを参照してください。

コマンド パレットから

コマンド パレットから MS SQL: Create Azure Function with SQL Binding コマンドを実行して、SQL バインドを使った新しい関数を作成します。

VS Code コマンド パレット コマンド

SQL バインドを使った Azure 関数をコマンド パレットから作成する方法については、こちらを参照してください。

既存の Azure 関数内

エディターで C# Azure 関数を開き、コマンド パレットから MS SQL: Add SQL Binding コマンドを実行して、SQL バインドを既存の関数に追加します。

VS Code コマンド パレット コマンド

詳しくは、こちらのドキュメントを参照してください。

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);
        }
    }
}

SQL 出力バインドを使った Azure 関数用に生成されたコード:

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; }
    }
}

次のステップ