1

我有一个控制台应用程序,我通过 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

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

编辑:这是我的单声道版本信息:

MonoDevelop 3.0.3.2
运行时:
Mono 2.10.9 (tarball)
GTK 2.24.10
GTK# (2.12.0.0)
包版本:210090011

4

1 回答 1

0

我找到了解决方法。我没有使用返回字符串的存储过程,而是使用了返回 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
于 2012-07-08T05:33:38.010 回答