허당 레몬도리
Published 2010. 1. 18. 10:12
xml 이용한 SQL Command 가. 언어유형/C#

using System.Data;
using System.Data.SqlClient;
using System.Xml;

class CommandXmlTest
{
    public static void main()
    {
        string conStr = "Server=localhost;user id=sa;password=wpsjfpxj;database=northwind";
        string xquery = "select * from Address FOR XML AUTO, ELEMENT";
        SqlConnection conn = new SqlConnection(conStr);
        conn.Open();
        SqlCommand xcomm = new SqlCommand(xquery, conn);
        XmlReader reader = null;
        try
        {
            reader = xcomm.ExecuteXmlReader();
            // 노드의 tag와 text만 출력
            // 노드가 있다면 true 노드가 없다면 false
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        Console.Write("<{0}>", reader.Name);
                        break;
                    case XmlNodeType.Text:
                        Console.Write(reader.Value.Trim());
                        break;
                    case XmlNodeType.EndElement:
                        Console.WriteLine("</{0}>", reader.Name);
                        break;
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            reader.Close();
            conn.Close();
        }
    }
}

 

레퍼런스 : 소설같은 C#

profile

허당 레몬도리

@LemonDory

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!