【问题标题】:MonoMac Console Application Returns Only First Character of nvarchar(max)MonoMac 控制台应用程序仅返回 nvarchar(max) 的第一个字符
【发布时间】:2012-07-08 13:33:38
【问题描述】:

我有一个控制台应用程序,我通过 Mono 在 Mac OS X 上运行。尽管它在 Windows 操作系统上正确执行,但它只返回 nvarchar(max) 变量的第一个字符。这是 C# 代码:

SqlConnection myConnection = new SqlConnection(Variables.connectionString());
SqlCommand myCommand = new SqlCommand("IndexPageDetailsGet", myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parameterIndexPageID = new SqlParameter("@IndexPageID", SqlDbType.Int);
parameterIndexPageID.Value = indexPageID;
myCommand.Parameters.Add(parameterIndexPageID);

SqlParameter parameterIndexPageText = new SqlParameter("@IndexPageText", SqlDbType.NVarChar, -1);
parameterIndexPageText.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterIndexPageText);

myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();

return (string)parameterIndexPageText.Value; // Only returns the first character

这里是存储过程:

ALTER PROCEDURE [dbo].[IndexPageDetailsGet]
(
    @IndexPageID int,
    @IndexPageText nvarchar(max) OUTPUT
)
AS SELECT
    @IndexPageText = IndexPageText
FROM
    IndexPages
WHERE
    IndexPageID = @IndexPageID

有没有其他人目睹过这种行为和/或知道如何解决它?

编辑:这是我的 Mono 版本信息:

MonoDevelop 3.0.3.2
运行时:
单声道 2.10.9(压缩包)
GTK 2.24.10
GTK# (2.12.0.0)
包版本:210090011

【问题讨论】:

  • 代码看起来不错 - 只是一个疯狂的猜测 - 您是否尝试将参数的最大大小设置为 Int.MaxValue(而不是 -1)?类似new SqlParameter("@IndexPageText", SqlDbType.NVarChar, Int.MaxValue);
  • 谢谢。我尝试了您的建议,但出现异常:未处理的异常:System.Data.SqlClient.SqlException:传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确。参数 6(“@IndexPageText”):数据类型 0xE7 的数据长度或元数据长度无效。

标签: c# sql-server mono monomac


【解决方案1】:

我找到了解决方法。我没有使用返回字符串的存储过程,而是使用了返回 SqlDataReader 的存储过程。然后它允许我使用完整的 nvarchar(max) 值的字符串。因此,在 C# 中:

string indexPageText = string.Empty;

SqlConnection myConnection = new SqlConnection(Variables.connectionString());
SqlCommand myCommand = new SqlCommand("IndexPagesGetByIndexPageID", myConnection);

myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parameterIndexPageID = new SqlParameter("@IndexPageID", SqlDbType.Int);
parameterIndexPageID.Value = indexPageID;
myCommand.Parameters.Add(parameterIndexPageID);

myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

while (result.Read())
{
    indexPageText = (string)result["IndexPageText"];
    break;
}
result.Close();

indexPageDetails.indexPageText = indexPageText;

还有存储过程:

ALTER PROCEDURE
    [dbo].[IndexPagesGetByIndexPageID]
(
    @IndexPageID int
)
AS SELECT
    *
FROM
    IndexPages
WHERE
    IndexPageID = @IndexPageID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2011-08-27
    • 2012-10-23
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多