我正在開發一個用於在SQL Server之間複製數據的Web應用程序。該工具將允許您指定要從哪個服務器進行復制,然後將特定數據庫(始終具有相同名稱)從源服務器複製到目標服務器。在SQL服務器之間複製數據庫
這樣做的最佳方法是什麼?數據可能相當大,因此速度也需要考慮。
我的嘗試是嘗試運行使用SQL Server Management Studio創建的SSIS包。該軟件包存儲在本地。
該計劃是修改源和目標連接字符串並啓動包。
這是我這樣做代碼:
public void DataTransfer(String sourceConnection, String destConnection, String pkgLocation)
{
Package pkg;
Application app;
DTSExecResult pkgResults;
try
{
app = new Application();
pkg = app.LoadPackage(pkgLocation, null);
foreach (ConnectionManager connectionManager in pkg.Connections)
{
SqlConnectionStringBuilder builder;
switch (connectionManager.Name)
{
case "SourceConnection":
builder = new SqlConnectionStringBuilder(sourceConnection);
builder.Remove("Initial Catalog");
builder.Add("Initial Catalog", "StagingArea");
var sourceCon = builder.ConnectionString + ";Provider=SQLNCLI;Auto Translate=false;";
//Added spaces to retain password!!!
sourceCon = sourceCon.Replace(";", "; ");
connectionManager.ConnectionString = sourceCon;
Debug.WriteLine(connectionManager.ConnectionString.ToString());
break;
case "DestinationConnection":
builder = new SqlConnectionStringBuilder(destConnection);
builder.Remove("Initial Catalog");
builder.Add("Initial Catalog", "StagingArea");
var destCon = builder.ConnectionString + ";Provider=SQLNCLI;Auto Translate=false;";
//Added spaces to retain password!!!
destCon = destCon.Replace(";", "; ");
connectionManager.ConnectionString = destCon;
Debug.WriteLine(connectionManager.ConnectionString.ToString());
break;
}
}
pkgResults = pkg.Execute();
}
catch (Exception e)
{
throw;
}
Debug.WriteLine(pkgResults.ToString());
}
當pkg
執行我得到以下異常:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.SqlServer.ManagedDTS.dll
A first chance exception of type 'Microsoft.SqlServer.Dts.Runtime.DtsComponentException' occurred in Microsoft.SqlServer.ManagedDTS.dll
我真的不知道從哪裏何去何從,任何想法?
到目前爲止您嘗試了什麼?你遇到了什麼問題?你有沒有考慮過像SSIS這樣的預先存在的選項? –
看看http://www.sqlservercentral.com/articles/Database+Mirroring/66357/ –
我想盡可能使用SSIS。我猜服務器需要安裝Integration Services?我已經看過SSIS和bcp實用程序到目前爲止... –