Aracılığıyla paylaş


Komut dosyası oluşturma

smo komut dosyası denetlenmektedir Scripter nesnesi ve alt nesnelerine veya Script yöntem üzerinde tek tek nesneler.The Scripter object controls the mapping out of dependency relationships for objects on an instance of Microsoft SQL Server.

komut dosyası kullanan kullanan kullanan dosyası kullanarak gelişmiş Scripter nesnesi ve alt nesnelerine olan üç aşaması İşlem:

  1. Bulma

  2. Listesi oluşturma

  3. Komut dosyası oluşturma

Keşif aşaması kullanır DependencyWalker nesne.Nesneleri, bir urn listesi verilen DiscoverDependencies yöntem, DependencyWalker nesnesi döndürür bir DependencyTree nesne nesneleri urn listesi.Boole fParents bulunabilmesi için veya belirtilen nesnenin alt ekranlara uygun olup olmadığını seçin için kullanılan parametre.Bağımlılık ağacı, bu aşamada değiştirilebilir.

Listede oluşturma aşaması, ağaç geçilen ve ortaya çıkan liste döndürülür.Bu nesne liste sırası komut dosyası kullanan kullanan kullanan dosyası çalıştırma ve yönetilebilir.

Listesi oluşturma aşamaları kullanımı WalkDependencies dönmek için yöntem bir DependencyTree.The DependencyTree can be modified at this stage.

Üçüncü ve son aşamasında belirtilen liste ve komut dosyası kullanan kullanan kullanan dosyası seçeneklerini komut dosyası kullanan kullanan kullanan kullanan dosyası oluşturulur.Sonuç olarak döndürülen bir StringCollection Sistem nesnesi.Bu aşamada, bağımlı nesne adlarını öğelerden sonra ayıklanmış koleksiyon , DependencyTree nesne ve özellikler gibi NumberOfSiblings ve FirstChild.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir.Daha fazla bilgi için bkz: Nasıl yapılır: Visual Studio'da Visual Basic smo proje oluşturun.NET veya Nasıl yapılır: Bir Visual C# smo Project Visual Studio'da oluşturun.NET.

Bu kod örneği gerektirir bir Imports deyim System.Collections.Specialized ad.Bu diğer Imports deyimleri, uygulamadaki tüm bildirimleri önce yerleştirin.

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

Visual Basic'te bir veritabanı için bağımlılıkları komut dosyası oluşturma

Bu kod örneği, bağımlılıkları keşfedin ve sonuçlar görüntülemek için liste boyunca yinelemek gösterilmiştir.

' 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] = "AdventureWorksLT2008R2"   ' 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

Bağımlılıkları komut dosyası için bir veritabanında Visual C#

Bu kod örneği, bağımlılıkları keşfedin ve sonuçlar görüntülemek için liste boyunca yinelemek gösterilmiştir.

// 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 = "AdventureWorksLT2008R2"; // 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("--");
         }
      } 
   }
}

Bağımlılıkları komut dosyası için PowerShell veritabanında

Bu kod örneği, bağımlılıkları keşfedin ve sonuçlar görüntülemek için liste boyunca yinelemek gösterilmiştir.

# 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 AdventureWorks2008.

CD Databases\AdventureWorks2008R2\Tables

foreach ($Item in Get-ChildItem)
 {  
 $scrp.Script($Item)
 }