[英]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.