SqlCommand.ExecuteXmlReader 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将对象 CommandText 发送到 Connection 并生成对象 XmlReader 。
public:
System::Xml::XmlReader ^ ExecuteXmlReader();
public System.Xml.XmlReader ExecuteXmlReader();
member this.ExecuteXmlReader : unit -> System.Xml.XmlReader
Public Function ExecuteXmlReader () As XmlReader
返回
一个 XmlReader 对象。
例外
如果设置为 Stream,则使用Value二进制或 VarBinary 以外的其他SqlDbType项。 有关流式处理的详细信息,请参阅 SqlClient 流式处理支持。
-或-
设置为
-或-
针对锁定行执行命令时发生异常。 使用 Microsoft .NET Framework 版本 1.0 时,不会生成此异常。
-或-
流式处理操作期间发生超时。 有关流式处理的详细信息,请参阅 SqlClient 流式处理支持。
在 SqlConnection 流式处理操作期间关闭或删除。 有关流式处理的详细信息,请参阅 SqlClient 流式处理支持。
流式处理操作期间发生错误StreamXmlReader或TextReader对象。 有关流式处理的详细信息,请参阅 SqlClient 流式处理支持。
XmlReader TextReader流式处理操作期间关闭或Stream对象。 有关流式处理的详细信息,请参阅 SqlClient 流式处理支持。
示例
以下示例创建一个并使用
private static void CreateXMLReader(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
System.Xml.XmlReader reader = command.ExecuteXmlReader();
}
}
Public Sub CreateXMLReader(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As New SqlCommand(queryString, connection)
Dim reader As System.Xml.XmlReader = command.ExecuteXmlReader
End Using
End Sub
注解
此方法返回的 XmlReader 不支持异步操作。
该 CommandText 属性通常使用有效的 FOR XML 子句指定 Transact-SQL 语句。 但是, CommandText 还可以指定一个语句,该语句返回 ntext 或 nvarchar 包含有效 XML 的数据,或用 xml 数据类型定义的列的内容。
典型 ExecuteXmlReader 查询的格式可以如下Microsoft Visual C# 示例所示:
SqlCommand command = new SqlCommand("SELECT * FROM dbo.Customers FOR XML AUTO, XMLDATA", SqlConn);
此方法还可用于检索包含 XML 数据的单行单列结果集。 在这种情况下,如果返回了多个行,该方法 ExecuteXmlReader 将附加到 XmlReader 第一行上的值,并放弃结果集的其余部分。
多个活动结果集 (MARS) 功能允许使用同一连接执行多个操作。
如果使用 ExecuteReader 或 BeginExecuteReader 访问 XML 数据,SQL Server 将返回长度大于 2,033 个字符的任何 XML 结果,每行包含 2,033 个字符。 若要避免此行为,请使用 ExecuteXmlReader 或 BeginExecuteXmlReader 读取 FOR XML 查询。