SQL XML 欄位值

下載 ADO.NET

SQL Server 支援 xml 資料類型,開發人員可以使用 SqlCommand 類別的標準行為擷取包含此類型的結果集。 xml 資料行的擷取方式就如同擷取任何資料行 (例如,擷取到 SqlDataReader),但如果您想要將資料行的內容當做 XML 使用,則必須使用 XmlReader

範例

下列主控台應用程式會從 SqlDataReader 執行個體中 AdventureWorks 資料庫的 Sales.Store 資料表選取兩個資料列,每個資料列都包含一個 xml 資料行。 針對每個資料列,會使用 xmlGetSqlXml 方法來讀取 SqlDataReader 資料行的值。 此值會儲存於 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;
            }
        }
    }
}