次の方法で共有


スクリプト

SMO でのスクリプティングは、Scripter オブジェクトおよびその子オブジェクトによって、または個々のオブジェクトの Script メソッドによって制御されます。Scripter オブジェクトは、MicrosoftSQL Server のインスタンスのオブジェクトに対する依存関係リレーションシップからのマッピングを制御します。

Scripter オブジェクト、およびその子オブジェクトを使用する高度なスクリプティング プロセスには、次の 3 つのフェーズがあります。

  1. 検索

  2. リスト生成

  3. スクリプト生成

検索フェーズでは、DependencyWalker オブジェクトが使用されます。オブジェクトの URN リストが指定されている場合、DependencyWalker オブジェクトの DiscoverDependencies メソッドは、URN リスト内のオブジェクトに対応する DependencyTree オブジェクトを返します。ブール型 fParents パラメータは、指定されたオブジェクトの親または子を検索するかどうかを選択するために使用します。依存関係ツリーはこの段階で変更することができます。

リスト生成フェーズでは、このツリーが渡され、結果リストが返されます。このオブジェクト リストは記述順であり、変更することもできます。

リスト生成フェーズでは、WalkDependencies メソッドを使用して DependencyTree を返します。DependencyTree はこの段階で変更することができます。

3 番目の最後のフェーズでは、指定されたリストとスクリプティング オプションを使用してスクリプトが生成されます。結果は 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); 
         } 
      } 
   } 
}