Transfer クラスは、オブジェクトとデータを転送するツールを提供するユーティリティ クラスです。
データベース スキーマ内のオブジェクトは、ターゲット サーバーで生成されたスクリプトを実行することによって転送されます。 Table データは動的に作成された DTS パッケージで転送されます。
Transfer オブジェクトには、DMO 内のTransfer オブジェクトのすべての機能と追加の SQL Server 機能が含まれています。 ただし、SQL Server 2012 の SMO では、 Transfer オブジェクトは SQLBulkCopy API を使用してデータを転送します。 また、データ転送の実行に使用されるメソッドとプロパティは、Database オブジェクトではなく、Transfer オブジェクトに存在します。 特定のタスクのコードは必要なときにのみ読み込まれるため、インスタンス クラスからユーティリティ クラスへの機能の移動は、より軽量なオブジェクト モデルと一致します。
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()