Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Dotyczy:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
SQL database w usłudze Microsoft Fabric
Skrypty w funkcji Scripter SMO są kontrolowane przez obiekt i jego obiekty podrzędne lub metodę Script dla poszczególnych obiektów. Obiekt Scripter steruje mapowaniem relacji zależności dla obiektów w wystąpieniu programu Microsoft SQL Server.
Zaawansowane wykonywanie skryptów przy użyciu Scripter obiektu i jego obiektów podrzędnych jest procesem trzech faz:
Discovery
Generowanie list
Generowanie skryptów
Faza odnajdywania używa DependencyWalker obiektu . Biorąc pod uwagę listę obiektów URN, DiscoverDependencies metoda DependencyWalker obiektu zwraca DependencyTree obiekt dla obiektów na liście URN. Parametr fParents wartości logicznej służy do wybierania, czy mają zostać odnalezione elementy nadrzędne, czy elementy podrzędne określonego obiektu. Drzewo zależności można modyfikować na tym etapie.
W fazie generowania listy drzewo jest przekazywane i zwracana jest wynikowa lista. Ta lista obiektów jest w kolejności skryptów i może być manipulowana.
Fazy generowania listy używają WalkDependencies metody , aby zwrócić DependencyTreewartość . Element DependencyTree można zmodyfikować na tym etapie.
W trzeciej i ostatniej fazie skrypt jest generowany skrypt z określoną listą i opcjami skryptów. Wynik jest zwracany jako obiekt systemowy StringCollection . W tej fazie nazwy obiektów zależnych są następnie wyodrębniane z kolekcji DependencyTree Items obiektu i właściwości, takich jak NumberOfSiblings i FirstChild.
Example
Aby użyć dowolnego podanego przykładu kodu, musisz wybrać środowisko programowania, szablon programowania i język programowania, w którym ma zostać utworzona aplikacja. Aby uzyskać więcej informacji, zobacz Create a Visual C# SMO Project in Visual Studio .NET(Tworzenie projektu SMO w programie Visual Studio .NET).
Ten przykładowy kod wymaga instrukcji Import dla przestrzeni nazw System.Collections.Specialized. Wstaw to z innymi instrukcjami Import przed wszelkimi deklaracjami w aplikacji.
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.Collections.Specialized
Wykonywanie skryptów zależności dla bazy danych w Visual Basic
W tym przykładzie kodu pokazano, jak odnaleźć zależności i iterować po liście, aby wyświetlić wyniki.
' 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
Wykonywanie skryptów zależności dla bazy danych w programie Visual C#
W tym przykładzie kodu pokazano, jak odnaleźć zależności i iterować po liście, aby wyświetlić wyniki.
// 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("--");
}
}
}
}
Wykonywanie skryptów zależności dla bazy danych w programie PowerShell
W tym przykładzie kodu pokazano, jak odnaleźć zależności i iterować po liście, aby wyświetlić wyniki.
# 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)
}