共用方式為


指令碼

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse Analytics

SMO 中的腳本是由 Scripter 物件及其子物件所控制,或 個別物件上的 Script 方法所控制。 物件 Scripter 會控制 Microsoft SQL Server 實例上物件的相依性關聯性對應。

使用 Scripter 物件及其子物件進行進階腳本處理是三個階段程式:

  1. 探索

  2. 清單產生

  3. 產生腳本

探索階段會使用 DependencyWalker 物件。 指定物件的 URN 清單, DiscoverDependencies 物件的 方法 DependencyWalker 會傳回 DependencyTree URN 清單中物件的 物件。 布林 值 fParents 參數可用來選取要探索指定物件的父系或子系。 此階段可以修改相依性樹狀結構。

在清單產生階段中,會傳入樹狀結構,並傳回產生的清單。 此物件清單是以腳本順序排列,而且可以操作。

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

在第三個階段和最後階段,會使用指定的清單和腳本選項產生腳本。 結果會以 StringCollection 系統物件的形式傳回。 在這個階段中,會從 物件的 Items 集合 DependencyTree 擷取相依物件名稱,以及 和 FirstChildNumberOfSiblings 屬性。

範例

若要使用提供的任何程式碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱 在 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 中編寫資料庫的相依性腳本

此程式碼範例示範如何探索相依性,並逐一查看清單以顯示結果。

' compile with:   
' /r:Microsoft.SqlServer.Smo.dll   
' /r:Microsoft.SqlServer.ConnectionInfo.dll   
' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll   
  
Imports Microsoft.SqlServer.Management.Smo  
Imports Microsoft.SqlServer.Management.Sdk.Sfc  
  
Public Class A  
   Public Shared Sub Main()  
      ' database name  
      Dim dbName As [String] = "AdventureWorksLT2012"   ' database name  
  
      ' Connect to the local, default instance of SQL Server.   
      Dim srv As New Server()  
  
      ' Reference the database.    
      Dim db As Database = srv.Databases(dbName)  
  
      ' Define a Scripter object and set the required scripting options.   
      Dim scrp As New Scripter(srv)  
      scrp.Options.ScriptDrops = False  
      scrp.Options.WithDependencies = True  
      scrp.Options.Indexes = True   ' To include indexes  
      scrp.Options.DriAllConstraints = True   ' to include referential constraints in the script  
  
      ' Iterate through the tables in database and script each one. Display the script.  
      For Each tb As Table In db.Tables  
         ' check if the table is not a system table  
         If tb.IsSystemObject = False Then  
            Console.WriteLine("-- Scripting for table " + tb.Name)  
  
            ' Generating script for table tb  
            Dim sc As System.Collections.Specialized.StringCollection = scrp.Script(New Urn() {tb.Urn})  
            For Each st As String In sc  
               Console.WriteLine(st)  
            Next  
            Console.WriteLine("--")  
         End If  
      Next  
   End Sub  
End Class  

在 Visual C 中編寫資料庫的相依性腳本#

此程式碼範例示範如何探索相依性,並逐一查看清單以顯示結果。

// compile with:   
// /r:Microsoft.SqlServer.Smo.dll   
// /r:Microsoft.SqlServer.ConnectionInfo.dll   
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll   
  
using System;  
using Microsoft.SqlServer.Management.Smo;  
using Microsoft.SqlServer.Management.Sdk.Sfc;  
  
public class A {  
   public static void Main() {   
      String dbName = "AdventureWorksLT2012"; // database name  
  
      // Connect to the local, default instance of SQL Server.   
      Server srv = new Server();  
  
      // Reference the database.    
      Database db = srv.Databases[dbName];  
  
      // Define a Scripter object and set the required scripting options.   
      Scripter scrp = new Scripter(srv);  
      scrp.Options.ScriptDrops = false;  
      scrp.Options.WithDependencies = true;  
      scrp.Options.Indexes = true;   // To include indexes  
      scrp.Options.DriAllConstraints = true;   // to include referential constraints in the script  
  
      // Iterate through the tables in database and script each one. Display the script.     
      foreach (Table tb in db.Tables) {   
         // check if the table is not a system table  
         if (tb.IsSystemObject == false) {  
            Console.WriteLine("-- Scripting for table " + tb.Name);  
  
            // Generating script for table tb  
            System.Collections.Specialized.StringCollection sc = scrp.Script(new Urn[]{tb.Urn});  
            foreach (string st in sc) {  
               Console.WriteLine(st);  
            }  
            Console.WriteLine("--");  
         }  
      }   
   }  
}  

在 PowerShell 中編寫資料庫的相依性腳本

此程式碼範例示範如何探索相依性,並逐一查看清單以顯示結果。

# Set the path context to the local, default instance of SQL Server.  
CD \sql\localhost\default  
  
# Create a Scripter object and set the required scripting options.  
$scrp = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Scripter -ArgumentList (Get-Item .)  
$scrp.Options.ScriptDrops = $false  
$scrp.Options.WithDependencies = $true  
$scrp.Options.IncludeIfNotExists = $true  
  
# Set the path context to the tables in AdventureWorks2022.  
  
CD Databases\AdventureWorks2022\Tables  
  
foreach ($Item in Get-ChildItem)  
 {    
 $scrp.Script($Item)  
 }