共用方式為


傳送資料

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體Azure Synapse Analytics

類別 Transfer 是公用程式類別,可提供傳送物件和資料的工具。

資料庫架構中的物件會藉由在目標伺服器上執行產生的腳本來傳輸。 Table 資料會隨著動態建立的 DTS 套件傳輸。

物件 Transfer 會使用 SQLBulkCopy API 來傳輸資料。 此外,用來執行資料傳輸的方法和屬性位於 物件上 Transfer ,而不是 Database 物件。 將功能從實例類別移至公用程式類別與較輕的物件模型一致,因為只有在需要時才會載入特定工作的程式碼。

Transfer物件不支援將資料傳輸到小於 SQL Server 實例版本的目標資料庫 CompatibilityLevel

範例

若要使用提供的任何程式碼範例,您必須選擇程式設計環境、程式設計範本,以及用來建立應用程式的程式設計語言。 如需詳細資訊,請參閱 在 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 AdventureWorks2022 database
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'Create a new database that is to be destination database.
Dim dbCopy As Database
dbCopy = New Database(srv, "AdventureWorks2022Copy")
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 = "AdventureWorks2022Copy"
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 AdventureWorks2022 database   
            Database db;  
            db = srv.Databases["AdventureWorks2022"];  
            //Create a new database that is to be destination database.   
            Database dbCopy;  
            dbCopy = new Database(srv, "AdventureWorks2022Copy");  
            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 = "AdventureWorks2022Copy";  
            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 AdventureWorks2022 database.  
$db = $srv.Databases["AdventureWorks2022"]  
  
#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()