【问题标题】:Azure function to connect to both blob and sql storage连接到 blob 和 sql 存储的 Azure 函数
【发布时间】:2017-09-12 13:36:18
【问题描述】:

我有一个我认为是 Azure 函数的完美介绍测试用例。

我们在 Azure 中有一个 SQL 存储(SQL 数据库)。

使用 C#,VS 2017,更新 Azure 工作流项。

我们还有一个 Blob 存储。 每天晚上我都需要一个进程来访问 SQL 数据库,根据标准收集一组记录,然后处理它们生成一个文件,该文件将存储在 blob 存储中。

我似乎无法克服完成这两项任务的困难。所有的教程似乎都是最基本的类型。

我在 VS 2017 中创建了一个函数,但第一步只是连接到 SQL 数据库。

我去添加新项目:ADO.NET 实体数据模型,它的行为就像它创建的模型正确但没有数据上下文?

因此,我决定尝试下一步 - 创建 blob 并仅使用硬编码的示例数据。再次...找不到关于如何做到这一点的好指南。

我在 local.setting.json 文件中有一个“AzureWebJobsStorage”设置。

我下面有个定时器功能:

 public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");



    }

只要指向正确的方向,我就会继续努力……

【问题讨论】:

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


    【解决方案1】:

    所以,你需要一个函数

    这是我的功能草图:

    [FunctionName("Function1")]
    public static void Run(
        [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, 
        [Blob("mycontainer/myblob.txt", FileAccess.Write)] out string outputBlob,
        TraceWriter log)
    {
        var str = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
        using (var conn = new SqlConnection(str))
        {
            conn.Open();
            var text = "SELECT count(*) FROM MyData";
    
            using (var cmd = new SqlCommand(text, conn))
            {
                // Execute the command and log the # rows affected.
                var rows = cmd.ExecuteScalar();
                outputBlob = $"{rows} rows found";
            }
        }
    }
    

    您的问题有点开放,因此请随时更新它,提供有关您的挣扎的更多详细信息。

    【讨论】:

      猜你喜欢
      • 2022-08-03
      • 2020-11-13
      • 1970-01-01
      • 2021-07-11
      • 2017-09-17
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多