SqlCommand.ExecuteXmlReader 方法

CommandText 发送到 Connection 并生成一个 XmlReader 对象。

**命名空间:**System.Data.SqlClient
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Function ExecuteXmlReader As XmlReader
用法
Dim instance As SqlCommand
Dim returnValue As XmlReader

returnValue = instance.ExecuteXmlReader
public XmlReader ExecuteXmlReader ()
public:
XmlReader^ ExecuteXmlReader ()
public XmlReader ExecuteXmlReader ()
public function ExecuteXmlReader () : XmlReader

返回值

一个 XmlReader 对象。

异常

异常类型 条件

SqlException

在对锁定的行执行该命令期间发生了异常。如果使用的是 Microsoft .NET Framework 1.0 版,将不会生成该异常。

备注

CommandText 属性通常指定一个包含有效 FOR XML 子句的 Transact-SQL 语句。但是,CommandText 还可以指定返回包含有效 XML 的 ntextnvarchar 数据的语句,或指定用 xml 数据类型定义的列的内容。

典型的 ExecuteXmlReader 查询可以按下面的 Microsoft Visual C# 示例进行格式化:

SqlCommand command = new SqlCommand("SELECT * FROM dbo.Customers FOR XML AUTO, XMLDATA", SqlConn);

提示

这种类型的查询仅在 Microsoft SQL Server 2000 或更高版本中可用。有关更多信息,请参见“SQL Server 联机丛书”中的“使用 FOR XML 子句的指南”。

在 SQL Server 2005 中使用时,还可以同时使用此方法检索包含 XML 数据的单行、单列结果集。这种情况下,如果返回了多个行,则 ExecuteXmlReader 方法将 XmlReader 附加到第一行上的值,并将丢弃结果集的其余部分。

如果您使用 SQL Server 2005 之前的 SQL Server 版本,在使用 XmlReader 时,关联的 SqlConnection 将忙于为 XmlReader 服务。当处于此状态时,除了关闭 SqlConnection 外,不能对其执行其他任何操作。除非调用 XmlReaderClose 方法,否则会一直处于此状态。从 SQL Server 2005 开始,多活动结果集 (MARS) 功能允许多个操作使用同一连接。

如果您使用 ExecuteReaderBeginExecuteReader 访问 XML 数据,SQL Server 将以多行(每行 2,033 个字符)方式返回长度大于 2,033 个字符的所有 XML 结果。若要避免发生此行为,请使用 ExecuteXmlReaderBeginExecuteXmlReader 读取 FOR XML 查询。有关更多信息,请参见位于 https://support.microsoft.com/default.aspx?ln=zh-cn. 上的 Microsoft 知识库中的文章 Q310378“PRB: XML Data Is Truncated When You Use SqlDataReader”(PRB:使用 SqlDataReader 时 XML 数据被截断)。

示例

下面的示例创建一个 SqlCommand,然后使用 ExecuteXmlReader 执行它。给该示例传递两个字符串:一个字符串是 Transact-SQL FOR XML SELECT 语句,另一个字符串用于连接数据源。

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
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();
    }
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

SqlCommand 类
SqlCommand 成员
System.Data.SqlClient 命名空间

其他资源

在 ADO.NET 中连接和检索数据
使用 SQL Server .NET Framework 数据提供程序