SQL XML 열 값

ADO.NET 다운로드

SQL Server는 xml 데이터 형식을 지원하며 개발자는 SqlCommand 클래스의 표준 동작을 사용하여 이 형식을 포함하는 결과 집합을 검색할 수 있습니다. xml 열은 다른 열과 마찬가지 방식으로 검색할 수 있지만(예: SqlDataReader로) 열의 내용을 XML로 작업하려면 XmlReader를 사용해야 합니다.

예시

다음 콘솔 애플리케이션에서는 xml 데이터베이스의 Sales.Store 테이블에서 인스턴스까지 각각 SqlDataReader 열이 포함된 두 개의 행을 선택합니다. 각 행에서 xml 열의 값은 GetSqlXmlSqlDataReader 메서드를 사용하여 읽습니다. 값은 XmlReader에 저장되어 있습니다. GetSqlXmlGetValue 열의 값을 문자열로 반환하므로 내용을 SqlXml 변수로 설정하려면 GetValue 메서드보다는 xml을 사용해야 합니다.

참고 항목

AdventureWorks 샘플 데이터베이스는 SQL Server를 설치할 때 기본적으로 설치되지 않으며 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;
            }
        }
    }
}

다음 단계