【问题标题】:Stored procedure OUTPUT string result is only one char instead of full string存储过程输出字符串结果只有一个字符而不是完整字符串
【发布时间】:2019-06-16 19:56:11
【问题描述】:

这段代码有问题,我想要的返回变量是“Sơn”而不是“S”

我的代码:

SentoDB.RunProcedure("usp_GetNameWithIdCategory");
SqlParameter CategoryID = new SqlParameter("CategoryID", SqlDbType.Int);
CategoryID.Value = Int32.Parse(item.Value);
SqlParameter result = new SqlParameter("result", SqlDbType.NVarChar);
result.Direction = ParameterDirection.Output;
result.Value = string.Empty;
SentoDB.AddParameters(CategoryID);
SentoDB.AddParameters(result);
SentoDB.LoadProcedure();

变量 result.Value.ToString() 是 'S' 而不是 "Sơn"

public void RunProcedure(string procedureName)
{
    cmd = new SqlCommand(procedureName, connect);
    cmd.CommandType = CommandType.StoredProcedure;
}

public void LoadProcedure()
{
    connect.Open();
    cmd.ExecuteNonQuery();
    connect.Close();
}

SQL 中的存储过程:

CREATE PROCEDURE usp_GetNameWithIdCategory 
     (@CategoryID INT, @result NVARCHAR(MAX) OUTPUT)
AS
BEGIN
    SELECT @result = Cate.Name 
    FROM Category AS Cate 
    WHERE Cate.CategoryID = @CategoryID
END

【问题讨论】:

    标签: c# sql-server winforms sqlcommand


    【解决方案1】:

    尝试为参数添加大小。 在添加大小之前,我对 varbinary 有同样的问题。

    【讨论】:

    • 你的意思是SqlParameter result = new SqlParameter("result", SqlDbType.NVarChar);在这行。
    • 是的,有一个带有 size 参数的实现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2019-10-12
    • 2016-02-08
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多