Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Berlaku untuk:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
SQL database di Microsoft Fabric
Pembuatan skrip dalam SMO dikendalikan oleh Scripter objek dan objek turunannya, atau metode Skrip pada objek individual. Objek Scripter mengontrol pemetaan hubungan dependensi untuk objek pada instans Microsoft SQL Server.
Pembuatan skrip tingkat lanjut dengan menggunakan Scripter objek dan objek turunannya adalah proses fase tiga:
Discovery
Pembuatan daftar
Pembuatan skrip
Fase penemuan DependencyWalker menggunakan objek . Mengingat daftar objek URN, DiscoverDependencies metode DependencyWalker objek mengembalikan DependencyTree objek untuk objek dalam daftar URN. Parameter fParents Boolean digunakan untuk memilih apakah induk atau anak dari objek yang ditentukan akan ditemukan. Pohon dependensi dapat dimodifikasi pada tahap ini.
Dalam fase pembuatan daftar, pohon diteruskan dan daftar yang dihasilkan dikembalikan. Daftar objek ini dalam urutan pembuatan skrip dan dapat dimanipulasi.
Fase pembuatan daftar menggunakan WalkDependencies metode untuk mengembalikan DependencyTree. DependencyTree dapat dimodifikasi pada tahap ini.
Pada fase ketiga dan terakhir, skrip dihasilkan dengan daftar dan opsi pembuatan skrip yang ditentukan. Hasilnya dikembalikan sebagai StringCollection objek sistem. Dalam fase ini, nama objek dependen kemudian diekstrak dari kumpulan DependencyTree Item objek dan properti seperti NumberOfSiblings dan FirstChild.
Example
Untuk menggunakan contoh kode apa pun yang disediakan, Anda harus memilih lingkungan pemrograman, templat pemrograman, dan bahasa pemrograman untuk membuat aplikasi Anda. Untuk informasi selengkapnya, lihat Membuat Proyek SMO Visual C# di Visual Studio .NET.
Contoh kode ini memerlukan pernyataan Impor untuk namespace System.Collections.Specialized. Sisipkan ini dengan pernyataan Impor lainnya, sebelum deklarasi apa pun dalam aplikasi.
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.Collections.Specialized
Membuat Skrip Dependensi untuk Database di Visual Basic
Contoh kode ini menunjukkan cara menemukan dependensi dan melakukan iterasi melalui daftar untuk menampilkan hasilnya.
' 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
Membuat Skrip Dependensi untuk Database di Visual C#
Contoh kode ini menunjukkan cara menemukan dependensi dan melakukan iterasi melalui daftar untuk menampilkan hasilnya.
// 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("--");
}
}
}
}
Membuat Skrip Dependensi untuk Database di PowerShell
Contoh kode ini menunjukkan cara menemukan dependensi dan melakukan iterasi melalui daftar untuk menampilkan hasilnya.
# 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)
}