在sql server之间复制数据库

本文关键字:复制 数据库 之间 server sql | 更新日期: 2023-09-27 18:15:03

我正在开发一个用于在SQL服务器之间复制数据的Web应用程序。该工具将允许您指定从哪个服务器复制/复制到哪个服务器,然后将特定的数据库(始终具有相同的名称)从源服务器复制到目标服务器。

最好的方法是什么?数据可能非常大,所以速度也需要考虑在内。


我的尝试是尝试运行我使用SQL Server管理工作室创建的SSIS包。包存储在本地。

计划是修改Source和Destination连接字符串并启动包。

这是我的代码:

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

我真的不知道从这里去哪里,有什么主意吗?

在sql server之间复制数据库

我将编写一个SSIS包来进行数据复制/转换,网站将简单地配置连接字符串并启动包。