Search code examples
c#sql-servermonomonomac

MonoMac Console Application Returns Only First Character of nvarchar(max)


I have a console application that I run on Mac OS X through Mono. Although it executes correctly on Windows OS, it only returns the first character of an nvarchar(max) variable. Here is the C# code:

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

And here is the Stored Procedure:

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

Has anyone else witnessed this behavior and/or know how to work around it?

EDIT: Here is my Mono Version Information:

MonoDevelop 3.0.3.2
Runtime:
Mono 2.10.9 (tarball)
GTK 2.24.10
GTK# (2.12.0.0)
Package version: 210090011


Solution

  • I found a workaround to this. Instead of using a stored procedure that returns a string, I used one that returns a SqlDataReader. It then allows me a string with the full nvarchar(max) value. Hence, in 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;
    

    And the stored procedure:

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