SMO 中的脚本由 Scripter 对象及其子对象 Script 或单个对象的方法控制。 该 Scripter 对象控制 MicrosoftSQL Server 实例上对象的依赖项关系映射。
使用 Scripter 对象及其子对象的高级脚本是一个三个阶段过程:
发现
列表生成
脚本生成
发现阶段使用 DependencyWalker 对象。 给定对象的 URN 列表后, DiscoverDependencies 对象的方法 DependencyWalker 将为 URN 列表中的对象返回对象 DependencyTree 。 布尔 fParents 参数用于选择是要发现指定对象的父对象还是子级。 在此阶段可以修改依赖项树。
在列表生成阶段,将传入树并返回生成的列表。 此对象列表按脚本顺序排列,可以作。
列表生成阶段使用 WalkDependencies 该方法返回一个 DependencyTree。 DependencyTree在此阶段可以修改该设置。
在第三阶段和最后阶段,使用指定的列表和脚本选项生成脚本。 结果作为 StringCollection 系统对象返回。 在此阶段中,从对象和属性的 DependencyTree Items 集合中提取依赖对象名称,例如 NumberOfSiblings 和 FirstChild。
示例:
若要使用提供的任何代码示例,必须选择要在其中创建应用程序的编程环境、编程模板和编程语言。 有关详细信息,请参阅 在 Visual Studio .NET 中创建 Visual Basic SMO 项目 ,或在 Visual Studio .NET 中创建 Visual C# SMO 项目。
此代码示例需要 Imports System.Collections.Specialized 命名空间的语句。 将此语句与其他 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 AdventureWorks2012.
CD Databases\AdventureWorks2012\Tables
foreach ($Item in Get-ChildItem)
{
$scrp.Script($Item)
}