使用 Visual Studio Code 的 SQL 绑定扩展创建 Azure Functions
适用于:SQL Server
Azure SQL 数据库
Azure SQL 托管实例
Azure Synapse Analytics
Azure Functions 预览版中提供了对 SQL 绑定的支持,用于输入和输出绑定,从而更轻松地将 Azure SQL 数据库或 SQL Server 数据库连接到 Azure Functions。 Visual Studio Code (VS Code) 的 SQL 绑定扩展有助于使用 SQL 绑定开发 Azure Functions 的过程,并随 mssql extension for VS Code 扩展包一起自动安装。 本文介绍如何使用 Visual Studio Code 的 SQL 绑定扩展创建具有 SQL 绑定的 Azure Functions。
注意
目前,SQL 绑定扩展仅支持 C# Azure Functions。 JavaScript 和 Python Azure Functions 支持 SQL 绑定,但 SQL 绑定扩展目前不支持。
从对象资源管理器中
若要从对象资源管理器 (OE) 中的特定 Table
或 View
创建 Azure 函数,请右键单击 SQL Server 对象资源管理器中所连接服务器中的表或视图,然后选择 Create Azure Function with SQL Binding.
表 OE 命令:
视图 OE 命令:
请参阅此处的 SQL Server 对象资源管理器中通过 SQL 绑定创建 Azure 函数的更多文档内容。
从命令面板中
从命令面板中运行 MS SQL: Create Azure Function with SQL Binding
命令,以使用 SQL 绑定创建新函数。
请参阅此处的命令面板中通过 SQL 绑定创建 Azure 函数的更多文档内容。
在现有的 Azure 函数中
在编辑器中打开 C# Azure 函数,然后从命令面板中运行 MS SQL: Add SQL Binding
命令,以将 SQL 绑定添加到现有函数。
请参阅此处的更多文档内容。
使用 SQL 绑定为 Azure Functions 生成的代码
为具有 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; }
}
}
后续步骤
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈