SMO의 XML 프로그래밍은 XML 데이터 형식, XML 네임스페이스 및 XML 데이터 형식 열에 대한 간단한 인덱싱을 제공하는 것으로 제한됩니다.
Microsoft SQL Server는 XML 문서 인스턴스에 대한 네이티브 스토리지를 제공합니다. XML 스키마를 사용하면 데이터 무결성을 보장하기 위해 XML 문서의 유효성을 검사하는 데 사용할 수 있는 복잡한 XML 데이터 형식을 정의할 수 있습니다. XML 스키마는 개체에 XmlSchemaCollection 정의됩니다.
예시
제공된 코드 예제를 사용하려면 프로그래밍 환경, 프로그래밍 템플릿 및 애플리케이션을 만들 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual Basic SMO 프로젝트 만들기 또는 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하세요.
Visual Basic에서 XML 스키마 만들기
이 코드 예제에서는 개체를 사용하여 XML 스키마를 XmlSchemaCollection 만드는 방법을 보여줍니다.
Text XML 스키마 컬렉션을 정의하는 속성에는 여러 개의 큰따옴표가 있습니다. 이러한 값은 문자열로 chr(34) 대체됩니다.
Visual C에서 XML 스키마 만들기#
이 코드 예제에서는 개체를 사용하여 XML 스키마를 XmlSchemaCollection 만드는 방법을 보여줍니다.
Text XML 스키마 컬렉션을 정의하는 속성에는 여러 개의 큰따옴표가 있습니다. 이러한 값은 문자열로 chr(34) 대체됩니다.
{
//Connect to the local, default instance of SQL Server.
Server srv = default(Server);
srv = new Server();
//Reference the AdventureWorks2012 database.
Database db = default(Database);
db = srv.Databases["AdventureWorks2012"];
//Define an XmlSchemaCollection object by supplying the parent
// database and name arguments in the constructor.
XmlSchemaCollection xsc = default(XmlSchemaCollection);
xsc = new XmlSchemaCollection(db, "MySampleCollection");
xsc.Text = "<schema xmlns=" + Strings.Chr(34) + "http://www.w3.org/2001/XMLSchema" + Strings.Chr(34) + " xmlns:ns=" + Strings.Chr(34) + "http://ns" + Strings.Chr(34) + "><element name=" + Strings.Chr(34) + "e" + Strings.Chr(34) + " type=" + Strings.Chr(34) + "dateTime" + Strings.Chr(34) + "/></schema>";
//Create the XML schema collection on the instance of SQL Server.
xsc.Create();
}
PowerShell에서 XML 스키마 만들기
이 코드 예제에서는 개체를 사용하여 XML 스키마를 XmlSchemaCollection 만드는 방법을 보여줍니다.
Text XML 스키마 컬렉션을 정의하는 속성에는 여러 개의 큰따옴표가 있습니다. 이러한 값은 문자열로 chr(34) 대체됩니다.
#Get a server object which corresponds to the default instance replace LocalMachine with the physical server
cd \sql\LocalHost
$srv = Get-Item default
#Reference the AdventureWorks database.
$db = $srv.Databases["AdventureWorks2012"]
#Create a new schema collection
$xsc = New-Object -TypeName Microsoft.SqlServer.Management.SMO.XmlSchemaCollection `
-argumentlist $db,"MySampleCollection"
#Add the xml
$dq = '"' # the double quote character
$xsc.Text = "<schema xmlns=" + $dq + "http://www.w3.org/2001/XMLSchema" + $dq + `
" xmlns:ns=" + $dq + "http://ns" + $dq + "><element name=" + $dq + "e" + $dq +`
" type=" + $dq + "dateTime" + $dq + "/></schema>"
#Create the XML schema collection on the instance of SQL Server.
$xsc.Create()