简体   繁体   English

如何通过C#代码将长字符串存储在varchar(MAX)列中?

[英]How to store a long string in a varchar(MAX) column by C# code?

All related subjects I saw in this site discuss stored procedures and alike. 我在该站点上看到的所有相关主题都讨论了存储过程等。 I have a C# program that receives a string (not important from where, maybe file or uploaded) and tries to insert it into a varchar(MAX) column. 我有一个C#程序,该程序接收一个字符串(不重要,可能来自文件,也可能不上传),然后尝试将其插入varchar(MAX)列。 My code is like this: 我的代码是这样的:

command.Parameters.AddWithValue("@pxslt", mer.XsltTemplate);

It raised an error: 它引发了一个错误:

String or binary data would be truncated. 字符串或二进制数据将被截断。

I changed it to: 我将其更改为:

command.Parameters.Add("@pxslt", SqlDbType.).Value = mer.XsltTemplate;

but still it raises the same error. 但仍然会引发相同的错误。

What is the solution? 解决办法是什么? The size of my string is about 8100 characters. 我的字符串大小约为8100个字符。

You can use this syntax: 您可以使用以下语法:

command.Parameters.Add("@pxslt", SqlDbType.VarChar, -1).Value = mer.XsltTemplate;

NB: -1 equates to (MAX). 注意:-1等于(MAX)。

I got it. 我知道了。 The error raises because of another field. 由于另一个字段而引起错误。 This was a field varchar(15) and the program tries to insert a string of 20 chars. 这是一个字段varchar(15),程序尝试插入20个字符的字符串。 I also noticed that varchar(max) works even without casting to DbType. 我还注意到,即使不将其转换为DbType,varchar(max)也可以工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM