Megosztás a következőn keresztül:


XML-sémák használata

A következőkre vonatkozik:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSQL-adatbázis a Microsoft Fabricben

Az XML-programozás az SMO-ban xml-adattípusok, XML-névterek és egyszerű indexelés xml-adattípus-oszlopokon való biztosítására korlátozódik.

A Microsoft SQL Server natív tárolót biztosít az XML-dokumentumpéldányokhoz. Az XML-sémák lehetővé teszik összetett XML-adattípusok meghatározását, amelyek az XML-dokumentumok ellenőrzésére használhatók az adatintegritás biztosítása érdekében. Az XML-séma az XmlSchemaCollection objektumban van definiálva.

Example

A megadott kód példájának használatához ki kell választania a programozási környezetet, a programozási sablont és azt a programozási nyelvet, amelyben létre szeretné hozni az alkalmazást. További információ: Visual C# SMO-projekt létrehozása a Visual Studio .NET-.

XML-séma létrehozása a Visual Basicben

Ez a példakód bemutatja, hogyan hozhat létre XML-sémát az XmlSchemaCollection objektum használatával. Az Text XML-sémagyűjteményt meghatározó tulajdonság több kettős idézőjelet tartalmaz. Ezeket a chr(34) sztring váltja fel.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'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()

XML-séma létrehozása a Visual C-ben#

Ez a példakód bemutatja, hogyan hozhat létre XML-sémát az XmlSchemaCollection objektum használatával. Az Text XML-sémagyűjteményt meghatározó tulajdonság több kettős idézőjelet tartalmaz. Ezeket a chr(34) sztring váltja fel.

{  
            //Connect to the local, default instance of SQL Server.   
            Server srv = default(Server);  
            srv = new Server();  
            //Reference the AdventureWorks2022 database.   
            Database db = default(Database);  
            db = srv.Databases["AdventureWorks2022"];  
            //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();  
        }  

XML-séma létrehozása a PowerShellben

Ez a példakód bemutatja, hogyan hozhat létre XML-sémát az XmlSchemaCollection objektum használatával. Az Text XML-sémagyűjteményt meghatározó tulajdonság több kettős idézőjelet tartalmaz. Ezeket a chr(34) sztring váltja fel.

#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["AdventureWorks2022"]  
  
#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()