使用很长(10000 个字符)的字符串作为 SqlParameter 值
我正在尝试向数据库执行插入命令,其中一列的类型为 nvarchar(MAX)。插入命令是使用.NET SqlCommand 类创建的,每个参数由一个SqlParameter 对象表示。
我的命令总是被执行,但是当我传递长度很大(10000+个字符)的字符串并且该字符串用作插入后映射到 nvarchar(MAX) 类型列的 SqlParameter 的值时,该特定列将保留为空。我重复一遍,没有抛出异常,执行了 INSERT 命令,但列为空。
以下示例是我尝试创建参数的方法:
// message is the large string variable
// first solution
insertCommand.Parameters.Add("@message", SqlDbType.NVarChar, -1);
// second solution
insertCommand.Parameters.Add("@message", SqlDbType.NVarChar, message.Length);
// third solution
insertCommand.Parameters.Add("@message", SqlDbType.NVarChar, message.Length * 2);
这些解决方案都没有给出结果。如果有人知道问题是什么,请告诉我。
我正在使用 MS SQL Server 2008。
提前致谢。
I am trying to execute an insert command into the database and one of the columns is of type nvarchar(MAX). Insert command is created by using .NET SqlCommand class and each of the parameters is represented by one SqlParameter object.
My command gets executed always, but when I pass string which length is large (10000+ characters) and which is used as a value for the SqlParameter mapped to the column of type nvarchar(MAX) after the insertion, that particular column is left empty. I repeat, no exception is thrown, INSERT command is executed, but column is empty.
Following examples are how I've tried to create parameter:
// message is the large string variable
// first solution
insertCommand.Parameters.Add("@message", SqlDbType.NVarChar, -1);
// second solution
insertCommand.Parameters.Add("@message", SqlDbType.NVarChar, message.Length);
// third solution
insertCommand.Parameters.Add("@message", SqlDbType.NVarChar, message.Length * 2);
None of these solution gave result. If anyone knows what is the problem please tell me.
I am using MS SQL Server 2008.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 MSDN
这是ntext:
另请查看此
According to MSDN
Here's ntext:
also check out this
您是否尝试过调用
AddWithValue(String name, Object value)
?Have you tried calling
AddWithValue(String name, Object value)
?另一个尝试建议:使用
SqlDbType.NText
而不是SqlDbType.NVarChar
。Another suggestion to try: use
SqlDbType.NText
instead ofSqlDbType.NVarChar
.一个建议..您是否尝试过使用SqlDbType.XML...没有必要在另一端解析xml...您可以简单地将值存储在varchar中。
One Suggesstion.. Have you tried using SqlDbType.XML... There is no necessary to parse the xml in the other end... you can simply store the value in varchar.
还会有问题吗
如果您显式创建 SqlParameter,例如:然后将其添加到参数中,
?另外,如果您正在调用存储过程,只需确保存储过程的参数也声明为 nvarchar(max),或者如果您不使用存储过程,请尝试使用它。我一直在推动 varbinary(max) blob,没有任何问题,所以也许二进制文件会得到更好的处理。
Do you still have the problem if you explicitly create SqlParameter, like:
and then add it to parameters?
Also if you are invoking a sproc, just to make sure that sproc's param is also declared as nvarchar(max), or if you are not using a sproc try to use it. I've been pushing varbinary(max) blobs without any issues so maybe a binary gets better treatment.