類別 Transfer 是公用程式類別,可提供傳送對象和數據的工具。
資料庫架構中的物件會藉由在目標伺服器上執行產生的腳本來傳輸。 Table 數據會隨著動態建立的 DTS 套件傳輸。
物件 Transfer 包含 DMO 中物件的所有功能 Transfer ,以及其他 SQL Server 功能。 不過,在 SQL Server 2012 的 SMO 中, Transfer 物件會使用 SQLBulkCopy API 來傳輸數據。 此外,用來執行數據傳輸的方法和屬性位於 物件上 Transfer ,而不是 Database 物件。 將功能從實例類別移至公用程式類別與較輕的物件模型一致,因為只有在需要時才會載入特定工作的程序代碼。
Transfer物件不支援將數據傳輸到小於 SQL Server 實例版本的目標資料庫CompatibilityLevel。
範例
若要使用提供的任何程式代碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱《SQL Server 在線叢書》中的<如何:在Visual Studio .NET 中建立Visual Basic SMO 專案>或<如何:在Visual Studio .NET 中建立 Visual C# SMO 專案>。
在 Visual Basic 中將架構和數據從一個資料庫傳輸到另一個資料庫
此程式代碼範例示範如何使用 物件,將架構和數據從某個資料庫傳輸到另一個 Transfer 資料庫。
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 2008R2 database
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'Create a new database that is to be destination database.
Dim dbCopy As Database
dbCopy = New Database(srv, "AdventureWorks2012Copy")
dbCopy.Create()
'Define a Transfer object and set the required options and properties.
Dim xfr As Transfer
xfr = New Transfer(db)
xfr.CopyAllTables = True
xfr.Options.WithDependencies = True
xfr.Options.ContinueScriptingOnError = True
xfr.DestinationDatabase = "AdventureWorks2012Copy"
xfr.DestinationServer = srv.Name
xfr.DestinationLoginSecure = True
xfr.CopySchema = True
'Script the transfer. Alternatively perform immediate data transfer with TransferData method.
xfr.ScriptTransfer()
在 Visual C 中將架構和數據從一個資料庫傳輸到另一個資料庫#
此程式代碼範例示範如何使用 物件,將架構和數據從某個資料庫傳輸到另一個 Transfer 資料庫。
{
Server srv;
srv = new Server();
//Reference the AdventureWorks2012 database
Database db;
db = srv.Databases["AdventureWorks2012"];
//Create a new database that is to be destination database.
Database dbCopy;
dbCopy = new Database(srv, "AdventureWorks2012Copy");
dbCopy.Create();
//Define a Transfer object and set the required options and properties.
Transfer xfr;
xfr = new Transfer(db);
xfr.CopyAllTables = true;
xfr.Options.WithDependencies = true;
xfr.Options.ContinueScriptingOnError = true;
xfr.DestinationDatabase = "AdventureWorks2012Copy";
xfr.DestinationServer = srv.Name;
xfr.DestinationLoginSecure = true;
xfr.CopySchema = true;
//Script the transfer. Alternatively perform immediate data transfer
// with TransferData method.
xfr.ScriptTransfer();
}
在 PowerShell 中將架構和數據從一個資料庫傳輸到另一個資料庫
此程式代碼範例示範如何使用 物件,將架構和數據從某個資料庫傳輸到另一個 Transfer 資料庫。
#Connect to the local, default instance of SQL Server.
#Get a server object which corresponds to the default instance
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server
#Reference the AdventureWorks2012 database.
$db = $srv.Databases["AdventureWorks2012"]
#Create a database to hold the copy of AdventureWorks
$dbCopy = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Database -ArgumentList $srv, "AdventureWorksCopy"
$dbCopy.Create()
#Define a Transfer object and set the required options and properties.
$xfr = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Transfer -ArgumentList $db
#Set this objects properties
$xfr.CopyAllTables = $true
$xfr.Options.WithDependencies = $true
$xfr.Options.ContinueScriptingOnError = $true
$xfr.DestinationDatabase = "AdventureWorksCopy"
$xfr.DestinationServer = $srv.Name
$xfr.DestinationLoginSecure = $true
$xfr.CopySchema = $true
"Scripting Data Transfer"
#Script the transfer. Alternatively perform immediate data transfer with TransferData method.
$xfr.ScriptTransfer()