Azure函数参数绑定

jvidinwx  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(102)

我正在编写一个azure函数,以JSON格式接收搜索参数,从请求体中提取值并将其传递到存储过程中。我已经能够做到这一点没有SQL绑定,但我试图这样做与SQL绑定,以减少代码。但是,我无法在没有错误的情况下将参数传递到存储过程。
下面是我用硬编码的@caseNo参数编写的工作代码(带有SQL绑定)。但是,我想用Case数据中的caseNo替换硬编码

`namespace FunctionApp1

{
    public static class Function1
    {
    
    [FunctionName("Function1")]

    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,              

        [Sql("ETL.CaseSearch",
        commandType: System.Data.CommandType.StoredProcedure,
        parameters: "@caseNo=123456",
        connectionStringSetting: "SqlConnectionString")]
        
        IEnumerable<Object> result,
        ILogger log)
    {

        var receivedContent = await new StreamReader(req.Body).ReadToEndAsync();
        Case data = JsonConvert.DeserializeObject<Case>(receivedContent);

        var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(result);            

        log.LogInformation("C# HTTP trigger with SQL Input Binding function processed a            request.");            
        

        return new JsonResult(jsonString);

    }
}

}`

aij0ehis

aij0ehis1#

可以通过以下方式在SQL绑定中传递多个参数-

[FunctionName("Function1")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log,
[Sql(commandText: "dbo.SearchCaseNo", commandType: System.Data.CommandType.StoredProcedure,
    parameters: "@CaseNumber={Query.CaseNo},@Name={Query.Name},@City={Query.City}",
    connectionStringSetting: "SqlConnectionString")]
        IEnumerable<Object> result)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    var receivedContent = await new StreamReader(req.Body).ReadToEndAsync();
    Case data = JsonConvert.DeserializeObject<Case>(receivedContent);

    var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(result);

    log.LogInformation("C# HTTP trigger with SQL Input Binding function processed a request.");

    return new JsonResult(jsonString);
}

AFAIK,在SQL绑定中传递case数据参数是不可行的,因为绑定参数只接受ms docs中的字符串。

输出:

我建议你向产品团队提出一个关于在SQL绑定参数中传递类数据的请求。

相关问题