脚本撰写

SMO 中的脚本撰写由 Scripter 对象及其子对象控制,或由各个对象的 Script 方法控制。Scripter 对象控制 MicrosoftSQL Server 实例中对象的依赖关系的向外映射。

使用 Scripter 对象及其子对象进行的高级脚本撰写是一个由三个阶段组成的过程:

  1. 发现

  2. 生成列表

  3. 生成脚本

发现阶段使用 DependencyWalker 对象。如果给定对象的 URN 列表,则 DependencyWalker 对象的 DiscoverDependencies 方法将为该 URN 列表中的对象返回 DependencyTree 对象。布尔型 fParents 参数用于选择是要发现指定对象的父级还是要发现其子级。在此阶段可以修改依赖关系树。

在生成列表阶段,会传入该树并返回生成的列表。此对象列表是按脚本撰写顺序排列的,可以对其进行操作。

生成列表阶段使用 WalkDependencies 方法返回 DependencyTree。在此阶段可以修改 DependencyTree

第三阶段也是最后一个阶段,在此阶段中,使用指定的列表和脚本撰写选项生成脚本。结果以 StringCollection 系统对象的形式返回。在此阶段中,接下来会从 DependencyTree 对象和属性(如 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); 
         } 
      } 
   } 
}