c# - 通过过程调用时 ADO.NET 中的整数输出参数空问题

标签 c# sql-server ado.net int out

当我尝试从 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 不传递/设置/初始化输出参数 = 0。

提前致谢。

最佳答案

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

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

关于c# - 通过过程调用时 ADO.NET 中的整数输出参数空问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52207953/

相关文章:

c# - 一次在十个图片框上显示多张图片

c# - 设计策略 : Query and Update data across 2 different databases

sql-server - 如何修复 "The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator."

oracle - 对 EntityFramework 和 ado.net 存储过程使用相同的事务

c# - T4Toolbox 删除 GeneratedCodeAttribute

c# - 为什么自动实现的属性必须同时定义 get 和 set 访问器

c# - 使用 SqlDataReader 和字符串数组

c# - 从 C# 调用存储过程时出错

c# - Postgres 数据库在 ADO.NET 实体数据模型中不可用

c# - 在 Bot Framework 中获取 Skype 身份?