ExecuteXMLReader 메서드를 사용하여 SQL 쿼리 실행
ExecuteToStream 메서드 대신 SqlXmlCommand 개체의 ExecuteXmlReader 메서드를 사용하여 명령을 실행할 수 있습니다. 이 메서드는 결과를 추가로 처리(이 예의 경우 요소 또는 특성의 이름과 값 인쇄)하는 데 사용할 수 있는 XmlReader 개체를 반환합니다.
[!참고]
코드에서 연결 문자열에 Microsoft SQL Server 인스턴스의 이름을 지정해야 합니다.
using System;
using Microsoft.Data.SqlXml;
using System.IO;
using System.Xml;
class Test
{
static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks2012;Integrated Security=SSPI";
public static int testParams()
{
SqlXmlParameter p;
XmlReader Reader;
XmlTextWriter tw;
SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
cmd.CommandText = "select FirstName, LastName from Person.Person where LastName = ? For XML Auto";
p = cmd.CreateParameter();
p.Value = "Achong";
Reader = cmd.ExecuteXmlReader();
tw = new XmlTextWriter(Console.Out);
Reader.MoveToContent();
tw.WriteNode(Reader, false);
tw.Flush();
tw.Close();
Reader.Close();
return 0;
}
static int Main(string[] args)
{
testParams();
return 0;
}
}
응용 프로그램을 테스트하려면
Microsoft .NET Framework가 컴퓨터에 설치되어 있는지 확인합니다.
이 항목에 나오는 C# 코드(DocSample.cs)를 폴더에 저장합니다.
코드를 컴파일합니다. 다음을 사용하여 명령 프롬프트에서 코드를 컴파일합니다.
csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs
이렇게 하면 실행 파일(DocSample.exe)이 만들어집니다.
명령 프롬프트에서 DocSample.exe를 실행합니다.