Bagikan melalui


Menggunakan Skema XML

Berlaku untuk: SQL ServerAzure SQL Database Azure SQL Managed Instance Azure Synapse Analytics

Pemrograman XML di SMO terbatas pada penyediaan tipe data XML, namespace XML, dan pengindeksan sederhana pada kolom tipe data XML.

Microsoft SQL Server menyediakan penyimpanan asli untuk instans dokumen XML. Skema XML memungkinkan Anda menentukan jenis data XML yang kompleks, yang dapat digunakan untuk memvalidasi dokumen XML untuk memastikan integritas data. Skema XML ditentukan dalam XmlSchemaCollection objek.

Contoh

Untuk menggunakan contoh kode apa pun yang disediakan, Anda harus memilih lingkungan pemrograman, templat pemrograman, dan bahasa pemrograman untuk membuat aplikasi Anda. Untuk informasi selengkapnya, lihat Membuat Proyek SMO Visual C# di Visual Studio .NET.

Membuat Skema XML di Visual Basic

Contoh kode ini menunjukkan cara membuat skema XML dengan menggunakan XmlSchemaCollection objek . Properti Text , yang menentukan koleksi skema XML, berisi beberapa tanda kutip ganda. Ini digantikan oleh chr(34) string.

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

Membuat Skema XML di Visual C#

Contoh kode ini menunjukkan cara membuat skema XML dengan menggunakan XmlSchemaCollection objek . Properti Text , yang menentukan koleksi skema XML, berisi beberapa tanda kutip ganda. Ini digantikan oleh chr(34) string.

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

Membuat Skema XML di PowerShell

Contoh kode ini menunjukkan cara membuat skema XML dengan menggunakan XmlSchemaCollection objek . Properti Text , yang menentukan koleksi skema XML, berisi beberapa tanda kutip ganda. Ini digantikan oleh chr(34) string.

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