简体   繁体   English

使用 SQL 客户端将时间触发的 Azure Function 与数据库连接

[英]Connect time-triggered Azure Function with Database using SQL Client

I am connecting time-triggered Azure Function with SQL Server using SQL client but I am not getting any data. I am connecting time-triggered Azure Function with SQL Server using SQL client but I am not getting any data.

Here is my code:这是我的代码:

local.settings.json : local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "DefaultConnection": "Data Source=; Initial Catalog=;User ID=;Password=;MultipleActiveResultSets=True;Persist Security Info=True;"        
  }
}

Function1.cs : Function1.cs

public class Function1
{
   [FunctionName("Function1")]
   public static async Task Run([TimerTrigger("0 45 14 * * *")]TimerInfo myTimer, ILogger log)
   {
     var sqlConnection = Environment.GetEnvironmentVariable("DefaultConnection");
     using (SqlConnection conn = new SqlConnection(sqlConnection))
     {
      conn.Open();
      var text = "SELECT * from UserMaster where UserId=1234";
      //This query has around 50 data in the database but still getting no data in it.
      
      using (SqlCommand cmd = new SqlCommand(text, conn))
      {
        using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
        {
          while (reader.Read())
          {
             log.LogInformation($"{reader.GetString(0)}{reader.GetString(1)} rows selected");
             Console.WriteLine("{0} {1}", reader.GetString(0), reader.GetString(1));
          }
        }
      }
     conn.Close();
    }
   log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
  }
}

I am not getting what is wrong with it.我不明白它有什么问题。 Please suggest请建议

I think you can use public override object this[string name] { get; }我认为您可以使用public override object this[string name] { get; } public override object this[string name] { get; } instead. public override object this[string name] { get; }而不是。 Here's my code and it worked well, you may compare it with yours.这是我的代码,它运行良好,您可以将其与您的代码进行比较。

 using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Function0602
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<List<tinyTest>> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            List<tinyTest> list = new List<tinyTest>();
            var str = "connect_string";
            using (SqlConnection conn = new SqlConnection(str))
            {
                conn.Open();
                var text = "select * from tinyTest;";
                using (SqlCommand cmd = new SqlCommand(text, conn))
                {
                    using (SqlDataReader reader = await cmd.ExecuteReaderAsync())
                    {
                        while (reader.Read())
                        {
                            tinyTest res = new tinyTest();
                            //SqlDataReader skd provide the method of reader["column_name"]
                            res.user_id = (string)reader["user_id"];
                            res.user_name = (string)reader["user_name"];
                            res.age = (int)reader["age"];
                            list.Add(res);
                        }
                    }
                }
                conn.Close();
            }
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            return list;
        }
    }

    public class tinyTest {
        public string user_id { get; set; }
        public string user_name { get; set; }
        public int age { get; set; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 DBContext 将 SQL 服务器数据库与时间触发的 Azure Function 连接起来 - Connecting SQL Server database with time-triggered Azure Function using DBContext 对时间触发的 Azure 函数进行单元测试 - Unit Test a Time Triggered Azure Function 无法使用C#中的AAD连接到Azure SQL数据库 - Cannot connect to Azure SQL database using AAD from C# 如何使用客户端Windows凭据连接到Web中的SQL Server数据库 - How to connect to SQL Server database in web using client windows credentials 如何使用客户端中的SQLite存储将枚举存储在SQL Azure数据库中? - How to store enums in a SQL Azure Database using SQLite store in client? 调试使用事件网格触发的 Azure Function - Debug an Azure Function that is triggered using an Event Grid Azure数据工厂- Azure function连接ZAC5C74B64B4B835AZ2EF2F181数据库不工作 - Azure data factory- Azure function to connect sql database does not work 时间触发 Azure 函数 - Time triggered Azure Functions 如何使用已在Azure中注册的应用程序使用实体框架数据库第一种方法连接到Azure SQL DB - How to connect to Azure SQL DB using entity framework database first approach using already registered app in Azure Azure Function 无法连接到 azure ZAC5C74B64B4B8352AF2F181AFFB5AC 数据库。 Package 错误 - Azure Function can't connect to azure sql database. Package error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM