共用方式為


適用於 Azure Functions 的適用於 MySQL 的 Azure 資料庫輸入系結

當函式執行時,適用於 MySQL 的 Azure 資料庫 輸入系結會從資料庫擷取數據,並將它傳遞給函式的輸入參數。

如需安裝和設定的相關信息,請參閱 概觀

重要

本文使用索引標籤來支援多個版本的 Node.js 程式設計模型。 v4 模型已正式推出,旨在為 JavaScript 和 TypeScript 開發人員提供更靈活且更直覺的體驗。 如需 v4 模型運作方式的更多詳細資料,請參閱 Azure Functions Node.js 開發人員指南。 若要深入了解 v3 與 v4 之間的差異,請參閱移轉指南

範例

您可以使用下列其中一種 C# 模式來建立 C# 函式:

  • 隔離的背景工作模型:在與運行時間隔離的背景工作進程中執行的已編譯 C# 函式。 需要隔離的背景工作進程,才能支援在 .NET 和 .NET Framework 的長期支援 (LTS) 和非 LTS 版本上執行的 C# 函式。
  • 同進程模型:在與 Azure Functions 運行時間相同的進程中執行的已編譯 C# 函式。
  • C# 文稿:主要用於在 Azure 入口網站 中建立 C# 函式。

GitHub 存放庫中提供 適用於 MySQL 的 Azure 資料庫 輸入系結的更多範例。

本區段包含下列範例:

這些範例會參考 Product 類別和對應的資料庫資料表:

namespace AzureMySqlSamples.Common
{
    public class Product
    {
        public int? ProductId { get; set; }

        public string Name { get; set; }

        public int Cost { get; set; }

        public override bool Equals(object obj)
    }
}
DROP TABLE IF EXISTS Products;

CREATE TABLE Products (
  ProductId int PRIMARY KEY,
  Name varchar(100) NULL,
  Cost int NULL
);

HTTP 觸發程式,依標識符從查詢字串取得數據列

下列範例顯示擷取單一記錄的 C# 函式。 此函式是由 使用查詢字串來指定標識碼的 HTTP 要求 所觸發。 該識別碼用來擷取具有所指定查詢的 Product 記錄。

注意

HTTP 查詢字串參數會區分大小寫。

using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.MySql;
using Microsoft.Azure.Functions.Worker.Http;
using AzureMySqlSamples.Common;

namespace AzureMySqlSamples.InputBindingIsolatedSamples
{
    public static class GetProductById
    {
        [Function(nameof(GetProductById))]
        public static IEnumerable<Product> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproducts/{productId}")]
            HttpRequestData req,
            [MySqlInput("select * from Products where ProductId = @productId",
                "MySqlConnectionString",
                parameters: "@ProductId={productId}")]
            IEnumerable<Product> products)
        {
            return products;
        }
    }
}

HTTP 觸發程式,從路由參數取得多個數據列

下列範例顯示 C# 函式 ,可擷取查詢傳回的數據列。 此函式是由 使用路由數據來指定查詢參數值的 HTTP 要求 所觸發。 該參數用來篩選 Product 指定查詢中的記錄。

using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.MySql;
using Microsoft.Azure.Functions.Worker.Http;
using AzureMySqlSamples.Common;

namespace AzureMySqlSamples.InputBindingIsolatedSamples
{
    public static class GetProducts
    {
        [Function(nameof(GetProducts))]
        public static IEnumerable<Product> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproducts")]
            HttpRequestData req,
            [MySqlInput("select * from Products",
                "MySqlConnectionString")]
            IEnumerable<Product> products)
        {
            return products;
        }
    }
}

HTTP 觸發程序,刪除資料列

下列範例示範 C# 函 式,其會使用來自 HTTP 要求的查詢參數的輸入來執行預存程式。

預存程式 DeleteProductsCost 必須在 MySQL 資料庫上建立。 在此範例中,預存程式會根據 參數的值,刪除單一記錄或所有記錄。

DROP PROCEDURE IF EXISTS DeleteProductsCost;

Create Procedure DeleteProductsCost(cost INT)
BEGIN
  DELETE from Products where Products.cost = cost;
END
namespace AzureMySqlSamples.InputBindingSamples
{
    public static class GetProductsStoredProcedure
    {
        [FunctionName(nameof(GetProductsStoredProcedure))]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproducts-storedprocedure/{cost}")]
            HttpRequest req,
            [MySql("DeleteProductsCost",
                "MySqlConnectionString",
                commandType: System.Data.CommandType.StoredProcedure,
                parameters: "@Cost={cost}")]
            IEnumerable<Product> products)
        {
            return new OkObjectResult(products);
        }
    }
}

GitHub 存放庫中提供 適用於 MySQL 的 Azure 資料庫 輸入系結的更多範例。

本區段包含下列範例:

這些範例會參考 Product 類別和對應的資料庫資料表:

package com.function.Common;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Product {
    @JsonProperty("ProductId")
    private int ProductId;
    @JsonProperty("Name")
    private String Name;
    @JsonProperty("Cost")
    private int Cost;

    public Product() {
    }
DROP TABLE IF EXISTS Products;

CREATE TABLE Products (
  ProductId int PRIMARY KEY,
  Name varchar(100) NULL,
  Cost int NULL
);

HTTP 觸發程式,取得多個數據列

下列範例示範 HTTP 要求觸發的 Java 函式中適用於 MySQL 的 Azure 資料庫輸入系結。 系結會從查詢讀取,並在 HTTP 回應中傳回結果。

package com.function;

import com.function.Common.Product;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.mysql.annotation.CommandType;
import com.microsoft.azure.functions.mysql.annotation.MySqlInput;

import java.util.Optional;

public class GetProducts {
    @FunctionName("GetProducts")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET},
                authLevel = AuthorizationLevel.ANONYMOUS,
                route = "getproducts}")
                HttpRequestMessage<Optional<String>> request,
            @MySqlInput(
                name = "products",
                commandText = "SELECT * FROM Products",
                commandType = CommandType.Text,
                connectionStringSetting = "MySqlConnectionString")
                Product[] products) {

        return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
    }
}

HTTP 觸發程式,依標識符從查詢字串取得數據列

下列範例示範 HTTP 要求觸發的 Java 函式中適用於 MySQL 的 Azure 資料庫輸入系結。 系結會從查詢字串中由參數篩選的查詢讀取,並傳回 HTTP 回應中的數據列。

public class GetProductById {
    @FunctionName("GetProductById")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET},
                authLevel = AuthorizationLevel.ANONYMOUS,
                route = "getproducts/{productid}")
                HttpRequestMessage<Optional<String>> request,
            @MySqlInput(
                name = "products",
                commandText = "SELECT * FROM Products WHERE ProductId= @productId",
                commandType = CommandType.Text,
                parameters = "@productId={productid}",
                connectionStringSetting = "MySqlConnectionString")
                Product[] products) {

        return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
    }
}

HTTP 觸發程序,刪除資料列

下列範例示範 HTTP 要求觸發的 Java 函式中適用於 MySQL 的 Azure 資料庫輸入系結。 系結會使用來自 HTTP 要求的查詢參數的輸入來執行預存程式。

必須在資料庫上建立預存程式 DeleteProductsCost 。 在此範例中,預存程式會根據 參數的值,刪除單一記錄或所有記錄。

DROP PROCEDURE IF EXISTS DeleteProductsCost;

Create Procedure DeleteProductsCost(cost INT)
BEGIN
  DELETE from Products where Products.cost = cost;
END
public class DeleteProductsStoredProcedure {
    @FunctionName("DeleteProductsStoredProcedure")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET},
                authLevel = AuthorizationLevel.ANONYMOUS,
                route = "Deleteproducts-storedprocedure/{cost}")
                HttpRequestMessage<Optional<String>> request,
            @MySqlInput(
                name = "products",
                commandText = "DeleteProductsCost",
                commandType = CommandType.StoredProcedure,
                parameters = "@Cost={cost}",
                connectionStringSetting = "MySqlConnectionString")
                Product[] products) {

        return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products).build();
    }
}

GitHub 存放庫中提供 適用於 MySQL 的 Azure 資料庫 輸入系結的更多範例。

本區段包含下列範例:

這些範例會參考資料庫資料表:

DROP TABLE IF EXISTS Products;

CREATE TABLE Products (
  ProductId int PRIMARY KEY,
  Name varchar(100) NULL,
  Cost int NULL
);

HTTP 觸發程式,取得多個數據列

下列範例顯示 HTTP 要求所觸發的「適用於 MySQL 的 Azure 資料庫」輸入系結。 系結會從查詢讀取,並在 HTTP 回應中傳回結果。

import { app, HttpRequest, HttpResponseInit, input, InvocationContext } from '@azure/functions';

const mysqlInput = input.generic({
    commandText: 'select * from Products',
    commandType: 'Text',
    connectionStringSetting: 'MySqlConnectionString',
});

export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    context.log('HTTP trigger and MySQL input binding function processed a request.');
    const products = context.extraInputs.get(mysqlInput);
    return {
        jsonBody: products,
    };
}

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    extraInputs: [mysqlInput],
    handler: httpTrigger1,
});
const { app, input } = require('@azure/functions');

const mysqlInput = input.generic({
    type: 'mysql',
    commandText: 'select * from Products where Cost = @Cost',
    parameters: '@Cost={Cost}',
    commandType: 'Text',
    connectionStringSetting: 'MySqlConnectionString'
})

app.http('GetProducts', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    route: 'getproducts/{cost}',
    extraInputs: [mysqlInput],
    handler: async (request, context) => {
        const products = JSON.stringify(context.extraInputs.get(mysqlInput));

        return {
            status: 200,
            body: products
        };
    }
});

HTTP 觸發程式,依標識符從查詢字串取得數據列

下列範例顯示 HTTP 要求所觸發的「適用於 MySQL 的 Azure 資料庫」輸入系結。 系結會從查詢字串中由參數篩選的查詢讀取,並傳回 HTTP 回應中的數據列。

import { app, HttpRequest, HttpResponseInit, input, InvocationContext } from '@azure/functions';

const mysqlInput = input.generic({
    commandText: 'select * from Products where ProductId= @productId',
    commandType: 'Text',
    parameters: '@productId={productid}',
    connectionStringSetting: 'MySqlConnectionString',
});

export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    context.log('HTTP trigger and MySQL input binding function processed a request.');
    const products = context.extraInputs.get(mysqlInput);
    return {
        jsonBody: products,
    };
}

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    extraInputs: [mysqlInput],
    handler: httpTrigger1,
});
const { app, input } = require('@azure/functions');

const mysqlInput = input.generic({
    type: 'mysql',
    commandText: 'select * from Products where ProductId= @productId',
    commandType: 'Text',
    parameters: '@productId={productid}',
    connectionStringSetting: 'MySqlConnectionString'
})

app.http('GetProducts', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    route: 'getproducts/{productid}',
    extraInputs: [mysqlInput],
    handler: async (request, context) => {
        const products = JSON.stringify(context.extraInputs.get(mysqlInput));

        return {
            status: 200,
            body: products
        };
    }
});

HTTP 觸發程序,刪除資料列

下列範例顯示 HTTP 要求所觸發的「適用於 MySQL 的 Azure 資料庫」輸入系結。 系結會使用來自 HTTP 要求的查詢參數的輸入來執行預存程式。

必須在資料庫上建立預存程式 DeleteProductsCost 。 在此範例中,預存程式會根據 參數的值,刪除單一記錄或所有記錄。

DROP PROCEDURE IF EXISTS DeleteProductsCost;

Create Procedure DeleteProductsCost(cost INT)
BEGIN
  DELETE from Products where Products.cost = cost;
END
import { app, HttpRequest, HttpResponseInit, input, InvocationContext } from '@azure/functions';

const mysqlInput = input.generic({
    commandText: 'DeleteProductsCost',
    commandType: 'StoredProcedure',
    parameters: '@Cost={cost}',
    connectionStringSetting: 'MySqlConnectionString',
});

export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    context.log('HTTP trigger and MySQL input binding function processed a request.');
    const products = context.extraInputs.get(mysqlInput);
    return {
        jsonBody: products,
    };
}

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    extraInputs: [mysqlInput],
    handler: httpTrigger1,
});
const { app, input } = require('@azure/functions');

const mysqlInput = input.generic({
    type: 'mysql',
    commandText: 'DeleteProductsCost',
    commandType: 'StoredProcedure',
    parameters: '@Cost={cost}',
    connectionStringSetting: 'MySqlConnectionString'
})

app.http('httpTrigger1', {
    methods: ['POST'],
    authLevel: 'anonymous',
    route: 'DeleteProductsByCost',
    extraInputs: [mysqlInput],
    handler: async (request, context) => {
        const products = JSON.stringify(context.extraInputs.get(mysqlInput));

        return {
            status: 200,
            body: products
        };
    }
});

GitHub 存放庫中提供 適用於 MySQL 的 Azure 資料庫 輸入系結的更多範例。

本區段包含下列範例:

這些範例會參考資料庫資料表:

DROP TABLE IF EXISTS Products;

CREATE TABLE Products (
  ProductId int PRIMARY KEY,
  Name varchar(100) NULL,
  Cost int NULL
);

HTTP 觸發程式,取得多個數據列

下列範例示範 function.json 檔案中的適用於 MySQL 的 Azure 資料庫輸入系結,以及 HTTP 要求觸發的 PowerShell 函式。 系結會從查詢讀取,並在 HTTP 回應中傳回結果。

下列範例是系結function.json檔案中的數據:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "Request",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get"
      ],
      "route": "getproducts/{cost}"
    },
    {
      "name": "response",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "products",
      "type": "mysql",
      "direction": "in",
      "commandText": "select * from Products",
      "commandType": "Text",
      "connectionStringSetting": "MySqlConnectionString"
    }
  ],
  "disabled": false
}

區段說明這些屬性。

下列範例是 run.ps1 檔案中 函式的範例 PowerShell 程式代碼:

using namespace System.Net

param($Request, $TriggerMetadata, $products)

Write-Host "PowerShell function with MySql Input Binding processed a request."

Push-OutputBinding -Name response -Value ([HttpResponseContext]@{
    StatusCode = [System.Net.HttpStatusCode]::OK
    Body = $products
})

HTTP 觸發程式,依標識符從查詢字串取得數據列

下列範例示範 HTTP 要求觸發的 PowerShell 函式中適用於 MySQL 的 Azure 資料庫輸入系結。 系結會從查詢字串中由參數篩選的查詢讀取,並傳回 HTTP 回應中的數據列。

下列範例是系結function.json檔案中的數據:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "Request",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get"
      ],
      "route": "getproducts/{productid}"
    },
    {
      "name": "response",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "products",
      "type": "mysql",
      "direction": "in",
      "commandText": "select * from Products where ProductId= @productId",
      "commandType": "Text",
      "parameters": "MySqlConnectionString",
      "connectionStringSetting": "MySqlConnectionString"
    }
  ],
  "disabled": false
}

區段說明這些屬性。

下列範例是 run.ps1 檔案中 函式的範例 PowerShell 程式代碼:

using namespace System.Net

param($Request, $TriggerMetadata, $products)

Write-Host "PowerShell function with MySql Input Binding processed a request."

Push-OutputBinding -Name response -Value ([HttpResponseContext]@{
    StatusCode = [System.Net.HttpStatusCode]::OK
    Body = $products
})

HTTP 觸發程序,刪除資料列

下列範例示範 function.json 檔案中的適用於 MySQL 的 Azure 資料庫輸入系結,以及 HTTP 要求觸發的 PowerShell 函式。 系結會使用來自 HTTP 要求的查詢參數的輸入來執行預存程式。

必須在資料庫上建立預存程式 DeleteProductsCost 。 在此範例中,預存程式會根據 參數的值,刪除單一記錄或所有記錄。

DROP PROCEDURE IF EXISTS DeleteProductsCost;

Create Procedure DeleteProductsCost(cost INT)
BEGIN
  DELETE from Products where Products.cost = 'cost';
END
{
  "bindings": [
    {
      "authLevel": "function",
      "name": "Request",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get"
      ],
      "route": "deleteproducts-storedprocedure/{cost}"
    },
    {
      "name": "response",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "products",
      "type": "mysql",
      "direction": "in",
      "commandText": "DeleteProductsCost",
      "commandType": "StoredProcedure",
      "parameters": "@Cost={cost}",
      "connectionStringSetting": "MySqlConnectionString"
    }
  ],
  "disabled": false
}

區段說明這些屬性。

下列範例是 run.ps1 檔案中 函式的範例 PowerShell 程式代碼:

using namespace System.Net

param($Request, $TriggerMetadata, $products)

Write-Host "PowerShell function with MySql Input Binding processed a request."

Push-OutputBinding -Name response -Value ([HttpResponseContext]@{
    StatusCode = [System.Net.HttpStatusCode]::OK
    Body = $products
}

GitHub 存放庫中提供 適用於 MySQL 的 Azure 資料庫 輸入系結的更多範例。

本區段包含下列範例:

這些範例會參考資料庫資料表:

DROP TABLE IF EXISTS Products;

CREATE TABLE Products (
  ProductId int PRIMARY KEY,
  Name varchar(100) NULL,
  Cost int NULL
);

注意

您必須使用適用於 Python 的 Azure Functions 1.22.0b4 版。

HTTP 觸發程式,取得多個數據列

下列範例示範 function.json 檔案中的適用於 MySQL 的 Azure 資料庫輸入系結,以及 HTTP 要求觸發的 Python 函式。 系結會從查詢讀取,並在 HTTP 回應中傳回結果。

下列範例是 function_app.py 檔案的 Python 程式代碼範例:

import azure.functions as func
import datetime
import json
import logging

app = func.FunctionApp()


@app.generic_trigger(arg_name="req", type="httpTrigger", route="getproducts/{cost}")
@app.generic_output_binding(arg_name="$return", type="http")
@app.generic_input_binding(arg_name="products", type="mysql",
                           commandText= "select * from Products",
                           command_type="Text",
                           connection_string_setting="MySqlConnectionString")
def mysql_test(req: func.HttpRequest, products: func.MySqlRowList) -> func.HttpResponse:
    rows = list(map(lambda r: json.loads(r.to_json()), products))

    return func.HttpResponse(
        json.dumps(rows),
        status_code=200,
        mimetype="application/json"
    )

HTTP 觸發程式,依標識符從查詢字串取得數據列

下列範例示範 HTTP 要求觸發的 Python 函式中適用於 MySQL 的 Azure 資料庫輸入系結。 系結會從查詢字串中由參數篩選的查詢讀取,並傳回 HTTP 回應中的數據列。

下列範例是 function_app.py 檔案的 Python 程式代碼範例:

import azure.functions as func
import datetime
import json
import logging

app = func.FunctionApp()


@app.generic_trigger(arg_name="req", type="httpTrigger", route="getproducts/{cost}")
@app.generic_output_binding(arg_name="$return", type="http")
@app.generic_input_binding(arg_name="products", type="mysql",
                           commandText= "select * from Products where ProductId= @productId",
                           command_type="Text",
                           parameters= "@productId={productid}",
                           connection_string_setting="MySqlConnectionString")
def mysql_test(req: func.HttpRequest, products: func.MySqlRowList) -> func.HttpResponse:
    rows = list(map(lambda r: json.loads(r.to_json()), products))

    return func.HttpResponse(
        json.dumps(rows),
        status_code=200,
        mimetype="application/json"
    )

HTTP 觸發程序,刪除資料列

下列範例示範 function.json 檔案中的適用於 MySQL 的 Azure 資料庫輸入系結,以及 HTTP 要求觸發的 Python 函式。 系結會使用來自 HTTP 要求的查詢參數的輸入來執行預存程式。

必須在資料庫上建立預存程式 DeleteProductsCost 。 在此範例中,預存程式會根據 參數的值,刪除單一記錄或所有記錄。

DROP PROCEDURE IF EXISTS DeleteProductsCost;

Create Procedure DeleteProductsCost(cost INT)
BEGIN
  DELETE from Products where Products.cost = cost;
END

下列範例是 function_app.py 檔案的 Python 程式代碼範例:

import azure.functions as func
import datetime
import json
import logging

app = func.FunctionApp()


@app.generic_trigger(arg_name="req", type="httpTrigger", route="getproducts/{cost}")
@app.generic_output_binding(arg_name="$return", type="http")
@app.generic_input_binding(arg_name="products", type="mysql",
                           commandText= "DeleteProductsCost",
                           command_type="StoredProcedure",
                           parameters= "@Cost={cost}",
                           connection_string_setting="MySqlConnectionString")
def mysql_test(req: func.HttpRequest, products: func.MySqlRowList) -> func.HttpResponse:
    rows = list(map(lambda r: json.loads(r.to_json()), products))

    return func.HttpResponse(
        json.dumps(rows),
        status_code=200,
        mimetype="application/json"
    )

屬性

C# 連結庫MySqlAttribute使用 屬性來宣告函式上的 MySQL 系結。 屬性具有下列屬性:

屬性內容 描述
CommandText 必要。 MySQL 查詢命令或系結所執行之預存程式的名稱。
ConnectionStringSetting 必要。 應用程式設定的名稱,其中包含執行查詢或預存程式之資料庫的連接字串。 此值不是實際的 連接字串,必須改為解析為環境變數名稱。
CommandType 必要。 值 CommandType ,用於 Text 查詢和 StoredProcedure 預存程式。
Parameters 選擇性。 在執行期間,以單一字串的形式傳遞至命令的零個或多個參數值。 必須遵循 格式 @param1=param1,@param2=param2。 參數名稱和參數值不能包含逗號 (,) 或等號 (=)。

註釋

Java 函式運行時間連結庫中,對 @MySQLInput 值來自適用於 MySQL 的 Azure 資料庫的參數使用註釋。 此批註支援下列元素:

元素 描述
commandText 必要。 MySQL 查詢命令或系結所執行之預存程式的名稱。
connectionStringSetting 必要。 應用程式設定的名稱,其中包含執行查詢或預存程式之資料庫的連接字串。 此值不是實際的 連接字串,必須改為解析為環境變數名稱。
commandType 必要。 值 CommandType ,用於 Text 查詢和 StoredProcedure 預存程式。
name 必要。 函式系結的唯一名稱。
parameters 選擇性。 在執行期間,以單一字串的形式傳遞至命令的零個或多個參數值。 必須遵循 格式 @param1=param1,@param2=param2。 參數名稱和參數值不能包含逗號 (,) 或等號 (=)。

組態

下表說明您可以在傳遞至 options 方法的物件上input.generic()設定的屬性:

屬性 描述
commandText 必要。 MySQL 查詢命令或系結所執行之預存程式的名稱。
connectionStringSetting 必要。 應用程式設定的名稱,其中包含執行查詢或預存程式之資料庫的連接字串。 此值不是實際的 連接字串,必須改為解析為環境變數名稱。 連接字串 值中的選擇性關鍵詞可用來精簡 MySQL 系結連線能力
commandType 必要。 值 CommandType ,用於 Text 查詢和 StoredProcedure 預存程式。
parameters 選擇性。 在執行期間,以單一字串的形式傳遞至命令的零個或多個參數值。 必須遵循 格式 @param1=param1,@param2=param2。 參數名稱和參數值不能包含逗號 (,) 或等號 (=)。

組態

下表說明您在 function.json 檔案中設定的系結組態屬性:

屬性 描述
type 必要。 必須設定為 mysql
direction 必要。 必須設定為 in
name 必要。 代表函式程式代碼中查詢結果的變數名稱。
commandText 必要。 MySQL 查詢命令或系結所執行之預存程式的名稱。
connectionStringSetting 必要。 應用程式設定的名稱,其中包含執行查詢或預存程式之資料庫的連接字串。 此值不是實際的 連接字串,必須改為解析為環境變數名稱。 連接字串 值中的選擇性關鍵詞可用來精簡 MySQL 系結連線能力
commandType 必要。 值 CommandType ,用於 Text 查詢和 StoredProcedure 預存程式。
parameters 選擇性。 在執行期間,以單一字串的形式傳遞至命令的零個或多個參數值。 必須遵循 格式 @param1=param1,@param2=param2。 參數名稱和參數值不能包含逗號 (,) 或等號 (=)。

當您在本機開發時,請在集合中的 local.settings.json 檔案Values中新增應用程式設定。

使用方式

屬性的建構函式會採用 MySQL 命令文字、命令類型、參數,以及連接字串設定的名稱。 此命令可以是具有命令類型的 System.Data.CommandType.Text MySQL 查詢,或是具有命令類型的 System.Data.CommandType.StoredProcedure預存程式名稱。 連接字串設定的名稱會對應至應用程式設定(在本機開發 local.settings.json 中),其中包含適用於 MySQL 的 Azure 資料庫的 連接字串

如果執行適用於 MySQL 的 Azure 資料庫輸入系結時發生例外狀況,函式程式代碼就會停止執行。 結果可能是錯誤碼,例如傳回 500 錯誤碼的 HTTP 觸發程式。