SQL XML 列的值

下载 ADO.NET

SQL Server 支持 xml 数据类型,开发人员可以使用 SqlCommand 类的标准行为检索包括此类型的结果集。 可以检索 xml 列(例如,检索到 SqlDataReader 中),就像检索任何列一样;但若要以 XML 格式处理列内容,必须使用 XmlReader

示例

以下控制台应用程序从 AdventureWorks 数据库的 Sales.Store 表中选择两行数据到 实例,每行都包含一个 列。 对于每一行,xml 列的值是使用 GetSqlXmlSqlDataReader 方法进行读取。 此值存储在 XmlReader 中。 请注意,若要将内容设置为 GetSqlXml 变量,必须使用 GetValue,而不是 SqlXml 方法;GetValue 以字符串形式返回 xml 列的值。

备注

默认情况下,在安装 SQL Server 时不安装 AdventureWorks 示例数据库。 可以通过运行 SQL Server 安装程序来安装它。

using Microsoft.Data.SqlClient;
using System.Xml;
using System.Data.SqlTypes;

class Class1
{
    static void Main()
    {
        string c = "Data Source=(local);Integrated Security=true;" +
        "Initial Catalog=AdventureWorks; ";
        GetXmlData(c);
        Console.ReadLine();
    }

    static void GetXmlData(string connectionString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();

            // The query includes two specific customers for simplicity's 
            // sake. A more realistic approach would use a parameter
            // for the CustomerID criteria. The example selects two rows
            // in order to demonstrate reading first from one row to 
            // another, then from one node to another within the xml column.
            string commandText =
                "SELECT Demographics from Sales.Store WHERE " +
                "CustomerID = 3 OR CustomerID = 4";

            SqlCommand commandSales = new SqlCommand(commandText, connection);

            SqlDataReader salesReaderData = commandSales.ExecuteReader();

            //  Multiple rows are returned by the SELECT, so each row
            //  is read and an XmlReader (an xml data type) is set to the 
            //  value of its first (and only) column. 
            int countRow = 1;
            while (salesReaderData.Read())
            //  Must use GetSqlXml here to get a SqlXml type. 
            //  GetValue returns a string instead of SqlXml. 
            {
                SqlXml salesXML =
                    salesReaderData.GetSqlXml(0);
                XmlReader salesReaderXml = salesXML.CreateReader();
                Console.WriteLine("-----Row " + countRow + "-----");

                //  Move to the root. 
                salesReaderXml.MoveToContent();

                //  We know each node type is either Element or Text.
                //  All elements within the root are string values. 
                //  For this simple example, no elements are empty. 
                while (salesReaderXml.Read())
                {
                    if (salesReaderXml.NodeType == XmlNodeType.Element)
                    {
                        string elementLocalName =
                            salesReaderXml.LocalName;
                        salesReaderXml.Read();
                        Console.WriteLine(elementLocalName + ": " +
                            salesReaderXml.Value);
                    }
                }
                countRow = countRow + 1;
            }
        }
    }
}