从 Azure 函数调用存储过程时,“System.Data.SqlClient.TdsParser”的类型初始值设定项引发异常错误

Tho*_*mas 9 .net c# azure-sql-database azure-functions

我有一个Azure Event hub智能电表的读数。我正在尝试使用将Azure Function仪表读数写入 Azure SQL DB。我在 Azure SQL DB 中创建了一个目标表和一个存储过程来解析 JSON 并将内容存储在表中。我已经成功测试了存储过程。

然而,当我从 Azure 函数调用它时,出现错误:“System.Data.SqlClient.TdsParser”的类型初始值设定项引发异常。出于测试目的,我尝试从 Azure 函数执行一个简单的 SQL select 语句,但这给出了相同的错误。我现在迷失了,因为我尝试了很多选择但没有运气。这是Azure功能代码:

#r "Microsoft.Azure.EventHubs"

using System;
using System.Text;
using System.Data;
using Microsoft.Azure.EventHubs;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;

public static async Task Run(string events, ILogger log)
{
    var exceptions = new List<Exception>();

    try
      {
            if(String.IsNullOrWhiteSpace(events))
                return;
            try{
                string ConnString = Environment.GetEnvironmentVariable("SQLAZURECONNSTR_azure-db-connection-meterreadevents", EnvironmentVariableTarget.Process);

                using(SqlConnection conn = new SqlConnection(ConnString))
                {
                    conn.Execute("dbo.ImportEvents", new { Events = events }, commandType: CommandType.StoredProcedure);
                }

            } catch (Exception ex) {
                log.LogInformation($"C# Event Hub trigger function exception: {ex.Message}");
            }
        }
        catch (Exception e)
        {
            // We need to keep processing the rest of the batch - capture this exception and continue.
            // Also, consider capturing details of the message that failed to process so it can be processed again later.
            exceptions.Add(e);
        }

    // Once processing of the batch is complete if any messages in the batch failed process throw an exception so that there is a record of the failure.

    if (exceptions.Count > 1)
        throw new AggregateException(exceptions);

    if (exceptions.Count == 1)
        throw exceptions.Single();
}
Run Code Online (Sandbox Code Playgroud)

传入的事件采用 JSON 格式,如下所示

{ 
   "current_consumption":450,
   "back_low":0.004,
   "current_back":0,
   "total_high":13466.338,
   "gas":8063.749,
   "current_rate":"001",
   "total_low":12074.859,
   "back_high":0.011,
   "timestamp":"2020-02-29 22:21:14.087210"
}
Run Code Online (Sandbox Code Playgroud)

存储过程如下:

CREATE PROCEDURE [dbo].[ImportEvents]
@Events NVARCHAR(MAX)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON

    -- Insert statements for procedure here
    INSERT INTO dbo.MeterReadEvents

    SELECT * FROM OPENJSON(@Events) WITH (timestamp datetime2, current_consumption int, current_rate nchar(3), current_back int, total_low numeric(8, 3), back_high numeric(8, 3), total_high numeric(8, 3), gas numeric(7, 3), back_low numeric(8, 3))
END
Run Code Online (Sandbox Code Playgroud)

我添加了 SQL AZURE 类型的连接字符串,并{your password}通过字符串中的实际密码进行了更改。关于如何解决此问题或如何获得更多日志记录的任何想法,因为错误非常普遍?

Ali*_*eit 7

我设法通过重新安装来修复此异常Microsoft.Data.SqlClient.SNI。然后cleanrebuild你的项目。


Mor*_*ork 0

尝试连接到本地 SQL,使用 SQL 探查器,检查您发送的内容,以及 SQL 尝试对正在执行的命令执行的操作。

复制您的代码非常困难,因为,我显然没有您的 Azure SQL :) 所以我建议,尝试执行存储过程中的每个步骤,作为直接查询。

看看这是否有效,然后尝试将语句包装到称为背对背的存储过程中,并使其发挥作用。然后将这些命令组合成一个命令,并摆弄它直到它正常工作;)

获取对 Azure SQL 执行的最简单的查询,以便确保您的连接有效。(就像只是简单地选择某件事)

因为如果没有更多信息,很难为您提供帮助。