调用SQL存储过程的C#函数在从本地计算机使用时有效,但在从云中的Azure函数调用时失败

问题描述 投票:1回答:2

我创建了这个C#文件,它将JSON文档作为输入,处理数据,然后将处理后的数据发送到SQL数据库。 eventuall计划是将其作为Azure功能放入云中,并触发任何进入Azures CosmosDB的新文档。

目前,负责将信息发送到SQL的代码部分如下所示:

public void Store (PushData i)
    {
        using (SqlConnection conn = new SqlConnection("Server=<serverconnection>;DataBase=<DBName>;User ID=<ID>;Password=<PW>"))
        {
            conn.Open();

            // 1.  create a command object identifying the stored procedure
            SqlCommand cmd = new SqlCommand("ActualsCreator", conn);

            // 2. set the command object so it knows to execute a stored procedure
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            // 3. add parameter to command, which will be passed to the stored procedure
            cmd.Parameters.AddWithValue("@Date", i.Date);
            cmd.Parameters.AddWithValue("@AvailabilityTime", i.MinutesUptime);
            cmd.Parameters.AddWithValue("@EnvName", i.EnvName);
            cmd.Parameters.AddWithValue("@MeaName", i.MeaName);
            cmd.Parameters.AddWithValue("@MeaType", i.MeaType);
            cmd.Parameters.AddWithValue("@LastUpdate", i.LastUpd);
            cmd.Parameters.AddWithValue("@ClusterStatus", i.Status);
            cmd.Parameters.AddWithValue("@ResourceID", i.ResID);
            cmd.Parameters.AddWithValue("@MidnightTime", i.MinutesUptimeForMidnight);

            // execute the command
            using (SqlDataReader rdr = cmd.ExecuteReader())
            {
            }
        }
    }

当运行localy时,所有信息都会在没有问题的情况下进入SQL服务器,但是当从azure函数运行时,它将在“@MeaName”或“@EnvName”或“@ResourceID”处失败。

[Error] Exception while executing function: Functions.monitorResultFullTrigger. mscorlib: Exception has been thrown by the target of an invocation. .Net SqlClient Data Provider: Procedure or function 'ActualsCreator' expects parameter '@MeaName', which was not supplied.

无论哪一个失败,无论在代码中首先说明哪一个。这三个在所有其他类型中唯一的共同点是它们在SQL数据库中存储为nvarchar(50)。错误消息表明没有任何内容传递给参数,但完全相同的代码没有这个问题localy,并没有其他变量,但那3个也有此问题。

我的问题是什么可以导致这个?为什么它只在Azure中的Azure函数失败,为什么只有nvarchar类型。

c# function azure
2个回答
0
投票

请尝试在我们的代码中指定SqlDbType,我们可以这样做:

SqlParameter para = new SqlParameter("@MeaName", SqlDbType.NVarChar) { Value = i.MeaName };
cmd.Parameters.Add(para);

希望它会有所帮助


0
投票

尝试以下所有参数

            using (SqlConnection sqlConnection = new SqlConnection(CONNECTIONSTRING))
            {
                using (SqlCommand sqlCommand = new SqlCommand(@"ActualsCreator", sqlConnection))
                {
                   //define sqlcommandtype as SP
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                   //define induvidual parameters for the SP
                    SqlParameter Param_MeaName = sqlCommand.CreateParameter();
                    Param_MeaName.ParameterName = @"@MeaName";
                    Param_MeaName.SqlDbType = SqlDbType.NVarChar;
                    Param_MeaName.Size = 50;
                    Param_MeaName.Direction = ParameterDirection.Input;
                    Param_MeaName.Value = i.MeaName;

                    //Add the paramters in the sqlcommand
                    sqlCommand.Parameters.Add(Param_Order_Key);

                    //Open the connection
                    sqlConnection.Open();

                    //Execute the SP
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
© www.soinside.com 2019 - 2024. All rights reserved.