1

当我尝试从INT类型为 SQL Server 存储过程的输出值时遇到一个问题,然后我得到 NULL 值 ☹ 。

这是存储过程:

CREATE PROCEDURE dbo.NULLISSUE 
    @input VARCHAR(10),
    @output INT = 0 OUTPUT
AS
BEGIN
    IF @input >= '1'
    BEGIN
        SET @output = @output + 1

        RETURN(0)
    END
END

这是.NET代码:

using (var connection = new SqlConnection(connectionString))
{
    using (var command = new SqlCommand())
    {
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "dbo.NULLISSUE";

        command.Parameters.Clear();
        command.Parameters.AddWithValue("@input", "1");

        command.Parameters.AddWithValue("@output", SqlDbType.Int);
        command.Parameters["@output"].Value = 0;
        command.Parameters["@output"].Direction = ParameterDirection.Output;

        command.CommandTimeout = 3000;

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();

        Console.WriteLine(command.Parameters["@output"].Value);
        //null value ?
        Console.ReadLine();
    }
}

ISNULL(@output, 0)在过程中进行了修复,但不确定为什么可以在 .NET 端完成以及为什么 ADO.NET 不传递/设置/初始化 OUTPUT 参数 = 0。

提前致谢。

4

1 回答 1

4

@outputParameterDirection.InputOutput如果您希望它使用客户端中设置的初始值,则必须是。

目前,由于它的ParameterDirection.Output值被忽略,它默认NULL在过程中并NULL + anything导致NULL.

于 2018-09-06T15:59:13.427 回答