存储过程的输出参数不更新

Stored procedure's output parameters not updating

我创建了一个 linq2sql 项目,其中有一个调用 SP 的扩展方法。 这种扩展方法有几个特点-

扩展方法-

public partial class TestLinq2SqlDataContext : DataContext
{
public IMultipleResults ExceuteProcedure(string spName, IEnumerable<SqlParameter> parameters, SqlConnection sqlConnection, out SqlDataReader reader)
{
    reader = null;
    try
    {
        sqlConnection.Open();
        var cmd = new SqlCommand
            {
                Connection = sqlConnection,
                CommandText = spName,
                CommandType = CommandType.StoredProcedure
            };

        cmd.Parameters.AddRange(parameters.ToArray());
        reader = cmd.ExecuteReader();
        return Translate(reader);
    }
    catch (Exception)
    {
    }
    return null;
}
}

我在SP以下打电话-

CREATE PROCEDURE USP_OutPutParameterCheck(
   @Id int OUTPUT,
   @Name nvarchar(50) OUTPUT)
AS 
BEGIN   
    SET @Id = 12 SET @Name = N'NameSet for OutputParameter'
END

我的 C# 代码是

public static void Main(){
context = new TestLinq2SqlDataContext();

#region USP_OutPutParameterCheck

var connection1 = context.Connection as SqlConnection;
SqlDataReader dataReader1;
var outParam1 = new SqlParameter
{
    Direction = ParameterDirection.Output,
    Value = "Abc",
    DbType = DbType.String,
    ParameterName = "@Name"
};
var outParam2 = new SqlParameter
{
    Direction = ParameterDirection.Output,
    Value = 1,
    DbType = DbType.Int32,
    ParameterName = "@Id"
};
var parameters11 = new[]
    {
        outParam1,
        outParam2
    };
var data21 = context.ExceuteProcedure("USP_OutPutParameterCheck", parameters11, connection1, out dataReader1);
}

现在,当我在调试模式下检查输出参数的值时,我得到了完美的 @Id 值 但是对于 @Name 参数我只得到 'N' 值而不是 'NameSet for OutputParameter'

谁能帮我解决我哪里出错了?

谢谢

更新:

在调试模式下查看参数值时添加屏幕截图 -

我认为您必须指定 outParam 1 的大小。

参见:https://msdn.microsoft.com/en-us/library/system.data.common.dbparameter.size(v=vs.110).aspx

For bidirectional and output parameters, and return values, you must set the value of Size. This is not required for input parameters, and if not explicitly set, the value is inferred from the actual size of the specified parameter when a parameterized statement is executed.