Aracılığıyla paylaş


Scripting

Şunlar için geçerlidir:SQL ServerAzure SQL VeritabanıAzure SQL Yönetilen ÖrneğiMicrosoft Fabric'de Azure Synapse AnalyticsSQL veritabanı

SMO'da betik oluşturma, nesne ve alt nesneleri veya tek tek nesnelerdeki Scripter yöntemi tarafından denetlenmektedir. nesnesi, Scripter Microsoft SQL Server örneğindeki nesneler için bağımlılık ilişkilerinin eşlemesini denetler.

Nesnesini ve alt nesnelerini kullanarak Scripter gelişmiş betik oluşturma üç aşamalı bir işlemdir:

  1. Discovery

  2. Liste üretimi

  3. Yazı üretimi

Bulma aşaması nesnesini kullanır DependencyWalker . Nesnelerin URN listesi verüldüğünde, DiscoverDependencies nesnenin DependencyWalker yöntemi URN listesindeki nesneler için bir DependencyTree nesne döndürür. Boole fParents parametresi, belirtilen nesnenin üst öğelerinin veya alt öğelerinin bulunup bulunmayacağını seçmek için kullanılır. Bağımlılık ağacı bu aşamada değiştirilebilir.

Liste oluşturma aşamasında ağaç geçirilir ve sonuçta elde edilen liste döndürülür. Bu nesne listesi betik sırasına göredir ve değiştirilebilir.

Liste oluşturma aşamaları, bir WalkDependenciesdöndürmek için yöntemini kullanırDependencyTree. DependencyTree bu aşamada değiştirilebilir.

Üçüncü ve son aşamada, belirtilen liste ve betik seçenekleriyle bir betik oluşturulur. Sonuç bir StringCollection sistem nesnesi olarak döndürülür. Bu aşamada bağımlı nesne adları, ve gibi DependencyTreeNumberOfSiblingsnesne ve özelliklerin FirstChild Items koleksiyonundan ayıklanır.

Example

Sağlanan herhangi bir kod örneğini kullanmak için programlama ortamını, programlama şablonunu ve uygulamanızın oluşturulacağı programlama dilini seçmeniz gerekir. Daha fazla bilgi için bkz. Visual Studio .NET'te Visual C# SMO Projesi Oluşturma.

Bu kod örneği, System.Collections.Specialized ad alanı için imports deyimi gerektirir. Bunu uygulamadaki bildirimlerden önce diğer Imports deyimleriyle ekleyin.

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

Visual Basic'te Veritabanı için Bağımlılıkları Betik Oluşturma

Bu kod örneği, bağımlılıkları bulmayı ve sonuçları görüntülemek için listede yinelemeyi gösterir.

' 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'de Bir Veritabanı için Bağımlılıkları Betik Oluşturma#

Bu kod örneği, bağımlılıkları bulmayı ve sonuçları görüntülemek için listede yinelemeyi gösterir.

// 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'de Veritabanı için Bağımlılıkları Betik Oluşturma

Bu kod örneği, bağımlılıkları bulmayı ve sonuçları görüntülemek için listede yinelemeyi gösterir.

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