简体   繁体   English

使用Dapper获取nvarchar(max)会返回一个修剪为4000个字符的字符串。这种行为可以改变吗?

[英]Using Dapper to get nvarchar(max) returns a string trimmed to 4000 characters. Can this behaviour be changed?

I have a SQL Server data table which stores a JSON string in one of its columns. 我有一个SQL Server数据表,它在其中一个列中存储JSON字符串。 The JSON string is a serialised .net object and the data typically exceeds 4000 characters. JSON字符串是序列化的.net对象,数据通常超过4000个字符。

I have a simple stored procedure which I use to retrieve the data: 我有一个简单的存储过程,我用它来检索数据:

    @StageID int,
    @Description varchar(250) = null OUTPUT,
    @Program nvarchar(max) = null OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    SELECT @Program = StageProgram, @Description = Description 
    FROM StageProgram 
    WHERE StageID = @StageID;

    RETURN 0;
END 

I am using the data type nvarchar(max) for the column. 我正在使用数据类型nvarchar(max)作为列。 When I serialise the .net object to JSON and write it to the database using Dapper, I find that the full string is correctly stored in the database. 当我将.net对象序列化为JSON并使用Dapper将其写入数据库时​​,我发现完整的字符串正确存储在数据库中。

However, when I attempt to retrieve the string I find that it is trimmed to 4000 characters, discarding the rest of the data. 但是,当我尝试检索字符串时,我发现它被修剪为4000个字符,丢弃其余的数据。

Here is the relevant code: 这是相关代码:

DynamicParameters p = new DynamicParameters();

p.Add("@StageID", Properties.Settings.Default.StageID, DbType.Int32, ParameterDirection.Input);
p.Add("@Description", "", DbType.String, direction: ParameterDirection.Output);
p.Add("@Program", "", DbType.String, direction: ParameterDirection.Output);
p.Add("@ReturnValue", DbType.Int32, direction: ParameterDirection.ReturnValue);               

try
{
     int stageID = Properties.Settings.Default.StageID;
     connection.Execute(sql, p, commandType: CommandType.StoredProcedure);                 
     json = p.Get<string>("@Program");
     int r = p.Get<int>("@ReturnValue");                    
}

When I run this, the string json is trimmed to 4000 characters. 当我运行它时,字符串json被修剪为4000个字符。

If I use the built in .net SQL Server connection to retrieve it instead (using a query rather than a stored procedure for simplicity), the full data is correctly returned: 如果我使用内置的.net SQL Server连接来检索它(为了简单起见使用查询而不是存储过程),正确返回完整数据:

SqlCommand getProgram = new SqlCommand("SELECT StageProgram FROM StageProgram WHERE StageID = 1;");
getProgram.Connection = connection;
string json = Convert.ToString(getProgram.ExecuteScalar());

Is an experienced Dapper user able to provide an explanation for this behaviour? 经验丰富的Dapper用户是否能够为此行为提供解释?

Can it be changed? 可以改变吗?

4000 characters is the default length for a DBString in Dapper : 4000个字符是Dapper中DBString的默认长度

来自上述链接的代码图片

To get the full text, you just need to set the size parameter: 要获取全文,只需设置size参数:

p.Add("@Program", "", DbType.String, direction: ParameterDirection.Output, size:int.MaxValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Entity Framework不为nvarchar(MAX)字段返回修剪后的字符串? - Why does Entity Framework not return a trimmed string for a nvarchar(MAX) field? 如何从外部 API 使用 Json 字符串? Json 返回转义字符或额外字符。 - How to consume Json string from external API? Json returns with escaped or extra characters. String Unlimited仍然限制为4000个字符? - String Unlimited still limited to 4000 characters? 使用 ClosedXML 单元格被修剪 - Using ClosedXML cells get trimmed ncarchar(max)上限为4000个字符时,无法存储HTML页面 - cant store an HTML page when ncarchar(max) is capped at 4000 characters String.Replace方法将忽略带有特殊字符的大小写。 - String.Replace method ignores case with special characters. 重定向URI不能包含换行符。(查询字符串) - Redirect URI cannot contain newline characters.(Query String) Dapper不反序列化nvarchar类型 - Dapper not deserializing nvarchar type 参数化查询&#39;((@id nvarchar(4000),@ name nvarchar(4000),@ project nvarchar(4000)&#39;)期望参数&#39;@id&#39;,未提供 - The parameterized query '(@id nvarchar(4000),@name nvarchar(4000),@project nvarchar(4000)' expects the parameter '@id', which was not supplied 模型类属性字符串长度为 nvarchar max - model class attribute string length as nvarchar max
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM