当我尝试从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。
提前致谢。