在通过 ODBC 查询 SQL Server 时,什么会导致 SqlXmlCommand.ExecuteXmlReader 中的 AccessViolationException?

问题描述

我有一个使用 sql Server 数据库的 VB.NET 应用程序。触发错误代码类似这样:

Dim reader As XmlReader = nothing
Dim cmd As sqlXmlCommand = New sqlXmlCommand(connectionString)
cmd.ClientSideXml = False
cmd.CommandText = commandText
cmd.CommandType = sqlXmlCommandType.sql
cmd.OutputEncoding = "UTF-8"
cmd.Roottag = roottag
reader = cmd.ExecuteXmlReader()

它适用于大多数用户,但有一个问题。最后一行有时会触发此错误

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.Marshal.copyToManaged(IntPtr source,Object destination,Int32 startIndex,Int32 length)
at Microsoft.Data.sqlXml.Common.ManagedSeqStreamWrapper.Write(Int32 buf,Int32 len)
at Microsoft.Data.sqlXml.Common.UnsafeNativeMethods.IsqlXMLCommandManagedInterface.ExecutetoOutputStream()
at Microsoft.Data.sqlXml.sqlXmlCommand.innerExecute(Stream strm)
at Microsoft.Data.sqlXml.sqlXmlCommand.ExecuteStream()
at Microsoft.Data.sqlXml.sqlXmlCommand.ExecuteXmlReader()
at [my code]

正在执行的 sql 命令使用“读取未提交”,我知道这会导致查询偶尔返回损坏的数据。这可能是这条看起来令人震惊的错误消息的来源吗?或者更可能是软件堆栈问题 - ODBC 驱动程序、.NET 框架等的错误或不匹配版本?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)