Aracılığıyla paylaş


Veri aktarma

The Transfer class is a utility class that provides tools to transfer objects and data.

Objects in the database schema are transferred by executing a generated script on the target server.Table data is transferred with a dynamically created DTS package.

The Transfer object contains all the functionality of the Transfer objects in DMO and additional SQL Server functionality.Ancak, içinde smo, Transfer nesnesi kullanan dts aktarma veri.Ayrıca, veri aktarımı gerçekleştirmek için kullanılan özellikler ve yöntemler çalıştırabilen Transfer yerine nesne Database nesne.Belirli görevler için kodu yalnızca gerekli olduğunda yüklendiği hareketli örnek sınıflarının yardımcı program sınıfları için daha açık bir nesne modeli ile tutarlı işlevdir.

The Transfer object does not support data transfers to a target database that has a CompatibilityLevel less than the version of the instance of SQL Server.

Özellikle,

  • SQL Server 2005ve SQL Server 2008 ile veritabanlarına veri aktarımını destekleyen CompatibilityLevel özellik Version80.

  • SQL Server 2005ve SQL Server 2008 çalışan veritabanlarına veri aktarımını destekleyen SQL Server sürüm 7.0.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir. Daha fazla bilgi için SQL Server Boks Online'da "How to: Create a Visual Basic SMO Project in Visual Studio .NET" (Nasıl Yapılır: Visual Studio .NET içinde Visual Basic SMO Projesi Oluşturma) veya "How to: Create a Visual C# SMO Project in Visual Studio .NET" (Nasıl Yapılır: Visual Studio .NET içinde Visual C# SMO Projesi Oluşturma) konularına bakın.

Şemayı ve verileri bir veritabanından başka bir Visual Basic aktarma

Bu kod örneği, şemayı ve verileri bir veritabanından başka bir için aktarımı yapmak gösterilmiştir Transfer nesne.

'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")
'Create a new database that is to be destination database.
Dim dbCopy As Database
dbCopy = New Database(srv, "AdventureWorks2008R2Copy")
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 = "AdventureWorks2008R2Copy"
xfr.DestinationServer = srv.Name
xfr.DestinationLoginSecure = True
xfr.CopySchema = True
'Script the transfer. Alternatively perform immediate data transfer with TransferData method.
xfr.ScriptTransfer()

Şema ve veri Visual C# [NULL]'taki bir başka bir veritabanından aktarma

Bu kod örneği, şemayı ve verileri bir veritabanından başka bir için aktarımı yapmak gösterilmiştir Transfer nesne.

{
            Server srv;
            srv = new Server();
            //Reference the AdventureWorks2008R2 database 
            Database db;
            db = srv.Databases["AdventureWorks2008R2"];
            //Create a new database that is to be destination database. 
            Database dbCopy;
            dbCopy = new Database(srv, "AdventureWorks2008R2Copy");
            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 = "AdventureWorks2008R2Copy";
            xfr.DestinationServer = srv.Name;
            xfr.DestinationLoginSecure = true;
            xfr.CopySchema = true;
            //Script the transfer. Alternatively perform immediate data transfer 
            // with TransferData method. 
            xfr.ScriptTransfer();
        } 

Şema ve veri bir veritabanından diğerine PowerShell aktarma

Bu kod örneği, şemayı ve verileri bir veritabanından başka bir için aktarımı yapmak gösterilmiştir Transfer nesne.

#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 AdventureWorks2008R2 database.
$db = $srv.Databases["AdventureWorks2008R2"]

#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()