表值参数中的 ADO.NET XML 类型列
这实际上不是一个问题,这是一个我找不到任何帮助的问题,所以我想我应该发布我最终找到的解决方案,以防其他人遇到它。
问题出在我传递给 SQL Server 2008 存储过程的 TVP 参数中的 XML 类型列。当我调用 ExecuteNonQuery() 时,我收到一个错误,类似于
附加信息:XML解析:(行号blah blah)无法切换编码(blah blah)
这是因为XML照常被指定为UTF-8
但 .NET 中的字符串是 UTF-16。解决方案是在 DataTable 中将带有 XML 的列声明为字节数组:
hitImport.Columns.Add("Answer", typeof(byte[]));
然后在添加行时获取 UTF-8 字节:
row["Answer"] = Encoding.UTF8.GetBytes(assignment.Answer);
ADO.NET 引擎知道如何将字节转换为 XML。希望对某人有帮助!
This isn't actually a question, it's a problem which I couldn't find any help on, so I thought I'd post the solution I eventually found in case someone else encounters it.
The problem was with an XML-typed column in a TVP parameter I was passing to a SQL Server 2008 stored proc. When I called ExecuteNonQuery() I got an error that was like
Additional information: XML parsing: (line number blah blah) unable to switch the encoding (blah blah)
This is because the XML as usual, was designated as UTF-8
<?xml version="1.0" encoding="UTF-8" ?>
but strings in .NET are UTF-16. The solution was to declare the column with XML as a byte array in the DataTable:
hitImport.Columns.Add("Answer", typeof(byte[]));
and then get the UTF-8 bytes when adding the row:
row["Answer"] = Encoding.UTF8.GetBytes(assignment.Answer);
The ADO.NET engine knows how to convert the bytes to XML. Hope that helps someone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论