Script 方法 (ScriptingOptions)
產生可用來重新建立資料庫的 Transact-SQL 指令碼,如指令碼選項所指定。
命名空間: Microsoft.SqlServer.Management.Smo
組件: Microsoft.SqlServer.Smo (在 Microsoft.SqlServer.Smo.dll 中)
語法
'宣告
Public Function Script ( _
scriptingOptions As ScriptingOptions _
) As StringCollection
'用途
Dim instance As Database
Dim scriptingOptions As ScriptingOptions
Dim returnValue As StringCollection
returnValue = instance.Script(scriptingOptions)
public StringCollection Script(
ScriptingOptions scriptingOptions
)
public:
virtual StringCollection^ Script(
ScriptingOptions^ scriptingOptions
) sealed
abstract Script :
scriptingOptions:ScriptingOptions -> StringCollection
override Script :
scriptingOptions:ScriptingOptions -> StringCollection
public final function Script(
scriptingOptions : ScriptingOptions
) : StringCollection
參數
- scriptingOptions
型別:Microsoft.SqlServer.Management.Smo. . :: . .ScriptingOptions
用來指定指令碼選項的 ScriptingOptions 物件值。
傳回值
型別:System.Collections.Specialized. . :: . .StringCollection
StringCollection 系統物件值,包含指令碼中的 Transact-SQL 陳述式清單。
實作
備註
The Script method generates a set of Transact-SQL statements that are used to create the database. This method generates a script that can be used to create the database only. The whole database, including dependent objects such as tables, can be scripted by using the Scripter object.
The scriptingOptions parameter is used to filter information in the generated script.
產生的指令碼可能包含未記載的內部程序;完整的指令碼輸出必須包含這些程序。
範例
VB
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2008R2")
'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
PowerShell
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2008R2")
$scrp = New-Object Microsoft.SqlServer.Management.Smo.Scripter($srv)
$scrp.Options.ScriptDrops = $FALSE
$scrp.Options.WithDependencies = $TRUE
Foreach ($tb in $db.Tables)
{
$smoObjects = $tb.Urn
If ($tb.IsSystemObject -eq $FALSE)
{
$sc = $scrp.Script($smoObjects)
Foreach ($st in $sc)
{
Write-Host $st
}
}
}