2011-11-17 109 views
2

我正在開發一個用於在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 

我真的不知道從哪裏何去何從,任何想法?

+3

到目前爲止您嘗試了什麼?你遇到了什麼問題?你有沒有考慮過像SSIS這樣的預先存在的選項? –

+0

看看http://www.sqlservercentral.com/articles/Database+Mirroring/66357/ –

+0

我想盡可能使用SSIS。我猜服務器需要安裝Integration Services?我已經看過SSIS和bcp實用程序到目前爲止... –

回答

1

我會編寫一個SSIS包來完成數據複製/轉換,網站只需配置連接字符串並啓動包。

+0

這可以使用一個SSIS包嗎?我只需要更改連接字符串,或者包本身需要修改? –

+0

是的,你可以爲此寫一個包。您的網絡應用程序可以簡單地在外部配置連接字符串。因此,使您的軟件包從數據庫或文件讀取其連接,並且Web應用程序只需寫入配置源 – Sheff

+0

使用SQL Management Studio創建的SSIS軟件包可以實現同樣的功能嗎?我上次保存了一次導出,如果將它包含在我的解決方案中,我可以修改連接字符串並運行它嗎? –

相關問題