共用方式為


使用 XML 結構描述

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 結構描述

此程式碼範例示範如何使用 XmlSchemaCollection 物件來建立 XML 結構描述。定義 XML 結構描述集合的 Text 屬性包含數個雙引號。這些會由 chr(34) 字串取代。

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

在 Visual C# 中建立 XML 結構描述

此程式碼範例示範如何使用 XmlSchemaCollection 物件來建立 XML 結構描述。定義 XML 結構描述集合的 Text 屬性包含數個雙引號。這些會由 chr(34) 字串取代。

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

此程式碼範例示範如何使用 XmlSchemaCollection 物件來建立 XML 結構描述。定義 XML 結構描述集合的 Text 屬性包含數個雙引號。這些會由 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["AdventureWorks2008R2"]

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