gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:16:12 36 4
gpt4 key购买 nike

我有一个 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();
}

传入的事件采用 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"
}

存储过程如下:
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

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

最佳答案

我设法通过在功能应用设置中将运行时版本更改为 ~2 来解决该问题。

这是否意味着这是运行时版本 ~3 中的一些错误,还是应该在运行时版本 ~3 中使用另一种方法来修复它?

关于c# - 从 Azure 函数调用存储过程时, 'System.Data.SqlClient.TdsParser' 的类型初始值设定项引发异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60470159/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com