【问题标题】:How to pass Parameters to Azure functions如何将参数传递给 Azure 函数
【发布时间】:2018-08-02 12:35:07
【问题描述】:

我有 Azure timer function 将数据从我的本地数据库复制到 Azure 托管数据库。目前我已经在函数中硬编码了表名。

如果硬编码,参数是否可以作为输入传递给函数?

public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, TraceWriter log) {
            string srcConnection = @"on premises connecting string";
            string destConnection = @"Azure managed instance connection string";

            string srcTable = "SourceTableName"; //am trying to make this as parameter
            string destTable = "DestinationTableName"; //am trying to make this as parameter
            string tmpTable = "select top 0 * into #DestTable from " + destTable;

            using(SqlConnection
                        srcConn = new SqlConnection(srcConnection),
                        destConn = new SqlConnection(destConnection)
                    ) {
                using(SqlCommand
                        srcGetCmd = new SqlCommand(srcTable, srcConn)
                    ) {
                    srcConn.Open();

                    destConn.Open();

                    SqlCommand cmd = new SqlCommand(tmpTable, destConn);
                    cmd.CommandTimeout = 180;
                    cmd.ExecuteNonQuery();
                    log.Info($"Temp table generated at: {DateTime.Now}");

                    SqlDataReader reader = srcGetCmd.ExecuteReader();
                    log.Info($"Source data loaded at: {DateTime.Now}");

                    using(SqlBulkCopy bulk = new SqlBulkCopy(destConn)) {
                        bulk.DestinationTableName = "#DestTable";
                        bulk.WriteToServer(reader);
                    }

                    string mergeSql = @"<sql logic to insert/Update/delete the data>";

                    cmd.CommandText = mergeSql;
                    cmd.CommandTimeout = 180;
                    cmd.ExecuteNonQuery();
                    log.Info($"Data update from temp table to destination at: {DateTime.Now}");

                    //Execute the command to drop temp table
                    cmd = new SqlCommand("drop table #DestTable", destConn);
                    cmd.CommandTimeout = 180;
                    cmd.ExecuteNonQuery();
                    log.Info($"Drop temp table at: {DateTime.Now}");

                    srcConn.Close();
                    destConn.Close();
                }
            }
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

        }

你可以看到我已经硬编码了表名,这可以作为参数吗?

【问题讨论】:

    标签: c# azure azure-sql-database azure-functions


    【解决方案1】:

    最简单的方法是使用这些名称(例如“SourceTableName”)添加应用程序设置,然后从Environment 获取它们:

    string srcTable = Environment.GetEnvironmentVariable("SourceTableName");
    

    要真正使它成为一个参数,您需要创建一个自定义绑定,类似于我在Authoring a Custom Binding for Azure Functions 中所做的。可能有点矫枉过正。

    【讨论】:

    • 谢谢,会查看并回复您。
    猜你喜欢
    • 2022-10-07
    • 2015-10-30
    • 1970-01-01
    • 2013-01-27
    • 2021-08-15
    • 2016-05-20
    • 2016-11-17
    • 2020-02-09
    相关资源
    最近更新 更多