共用方式為


指令碼

在 SMO 中,指令碼是由 Scripter 物件及其子物件控制,或是由個別物件的 Script 方法控制。Scripter 物件會在 MicrosoftSQL Server 的執行個體上根據物件的相依關聯性來控制對應。

使用 Scripter 物件及其子物件所進行的進階指令碼作業是三階段的程序:

  1. 探索

  2. 產生清單

  3. 產生指令碼

探索階段會使用 DependencyWalker 物件。如果已給定物件 URN 清單,則 DependencyWalker 物件的 DiscoverDependencies 方法會針對 URN 清單中的物件傳回 DependencyTree 物件。布林值 fParents 參數會用於選取要探索所指定物件的父代或子系。在這個階段可以修改相依性樹狀目錄。

在產生清單階段會傳入樹狀目錄並傳回產生的清單。這個物件清單會以指令碼作業順序排列,且可以操作。

產生清單階段會使用 WalkDependencies 方法傳回 DependencyTree。在這個階段可以修改 DependencyTree

在第三個也就是最後階段時,會產生具有指定清單和指令碼選項的指令碼。結果會以 StringCollection 系統物件傳回。接下來在這個階段中會從 DependencyTree 物件的 Items 集合以及 NumberOfSiblingsFirstChild 之類的屬性擷取相依物件的名稱。

範例

如果要使用所提供的任何程式碼範例,您必須選擇用於建立應用程式的程式設計環境、程式設計範本和程式設計語言。如需詳細資訊,請參閱<如何:在 Visual Studio .NET 中建立 Visual Basic SMO 專案>或<如何:在 Visual Studio .NET 中建立 Visual C# SMO 專案>。

這個程式碼範例需要針對 System.Collections.Specialized 命名空間使用 Imports 陳述式。請在應用程式中,將這個陳述式與其他的 Imports 陳述式一同插入任何宣告之前。

Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.Collections.Specialized

針對 Visual Basic 中的資料庫撰寫相依性的指令碼

這個程式碼範例示範如何探索相依性,並反覆運算清單以顯示結果。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a Scripter object and set the required scripting options.
Dim scrp As Scripter
scrp = New Scripter(srv)
scrp.Options.ScriptDrops = False
scrp.Options.WithDependencies = True
'Iterate through the tables in database and script each one. Display the script.
'Note that the StringCollection type needs the System.Collections.Specialized namespace to be included.
Dim tb As Table
Dim smoObjects(1) As Urn
For Each tb In db.Tables
    smoObjects = New Urn(0) {}
    smoObjects(0) = tb.Urn
    If tb.IsSystemObject = False Then
        Dim sc As StringCollection
        sc = scrp.Script(smoObjects)
        Dim st As String
        For Each st In sc
            Console.WriteLine(st)
        Next
    End If
Next

針對 Visual C# 中的資料庫撰寫相依性的指令碼

這個程式碼範例示範如何探索相依性,並反覆運算清單以顯示結果。

//Connect to the local, default instance of SQL Server. 
{ 
   Server srv = default(Server); 
   srv = new Server(); 
   //Reference the AdventureWorks database. 
   Database db = default(Database); 
   db = srv.Databases("AdventureWorks"); 
   //Define a Scripter object and set the required scripting options. 
   Scripter scrp = default(Scripter); 
   scrp = new Scripter(srv); 
   scrp.Options.ScriptDrops = false; 
   scrp.Options.WithDependencies = true; 
   //Iterate through the tables in database and script each one. Display the script. 
   //Note that the StringCollection type needs the System.Collections.Specialized namespace to be included. 
   Table tb = default(Table); 
   Urn[] smoObjects = new Urn[2]; 
   foreach ( tb in db.Tables) { 
      smoObjects = new Urn[1]; 
      smoObjects(0) = tb.Urn; 
      if (tb.IsSystemObject == false) { 
         StringCollection sc = default(StringCollection); 
         sc = scrp.Script(smoObjects); 
         string st = null; 
         foreach ( st in sc) { 
            Console.WriteLine(st); 
         } 
      } 
   } 
}