Aracılığıyla paylaş


xml Şeması kullanma

xml programlama SMO, xml veri türleri, xml ad alanları ve basit xml veri türü sütunlarındaki endeksleme sunmak için sınırlıdır.

Microsoft SQL ServerYerel depolama için xml belge örnekleri sağlar. xml Şemaları xml belgeleri veri bütünlüğünü doğrulamak için kullanılan karmaşık xml veri türlerini tanımlamanıza izin. xml şemasında tanımlanan XmlSchemaCollectionnesnesini.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir. Daha fazla bilgi için, bkz. Visual Studio'da Visual Basic smo proje oluşturun.NET veya Visual Studio'da Visual C# smo proje oluşturun.NET.

Visual Basic'te bir xml şeması oluşturma

Bu kod örneği kullanarak bir xml şeması oluşturma gösterilmiştir XmlSchemaCollectionnesnesini. Textxml şema koleksiyonu tanımlayan özelliği, birkaç çift tırnak işaretleri içerir. Bunlar are replaced by chr(34)dize.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Reference the AdventureWorks2012 2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'Define an XmlSchemaCollection object by supplying the parent database and name arguments in the constructor.
Dim xsc As XmlSchemaCollection
xsc = New XmlSchemaCollection(db, "MySampleCollection")
xsc.Text = "<schema xmlns=" + Chr(34) + "http://www.w3.org/2001/XMLSchema" + Chr(34) + "  xmlns:ns=" + Chr(34) + "http://ns" + Chr(34) + "><element name=" + Chr(34) + "e" + Chr(34) + " type=" + Chr(34) + "dateTime" + Chr(34) + "/></schema>"
'Create the XML schema collection on the instance of SQL Server.
xsc.Create()

Bir xml şeması Visual C# içinde oluşturma

Bu kod örneği kullanarak bir xml şeması oluşturma gösterilmiştir XmlSchemaCollectionnesnesini. Textxml şema koleksiyonu tanımlayan özelliği, birkaç çift tırnak işaretleri içerir. Bunlar are replaced by chr(34)dize.

{
            //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();
        }

{
            //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 içinde xml şeması oluşturma

Bu kod örneği kullanarak bir xml şeması oluşturma gösterilmiştir XmlSchemaCollectionnesnesini. Textxml şema koleksiyonu tanımlayan özelliği, birkaç çift tırnak işaretleri içerir. Bunlar are replaced by chr(34)dize.

#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()

#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()