Transferring Data by SMO failing when using ObjectList

时光怂恿深爱的人放手 提交于 2021-02-07 19:53:15

问题


I can transfer all data from a source DB to a target DB by using the option CopyAllTables = true and not providing a list in ObjectList.

            ServerConnection conn = new ServerConnection(sourceServer);
            conn.LoginSecure = true;
            Server srvSource = new Server(conn);
            Database dbSource = srvSource.Databases[sourceDB];

            Transfer xfr = new Transfer(dbSource);

            xfr.CopyAllTables = true;
            xfr.Options.WithDependencies = false;
            xfr.Options.ContinueScriptingOnError = false;
            xfr.DestinationDatabase = destDB;
            xfr.DestinationServer = destServer;
            xfr.Options.DriAllKeys = true;
            xfr.Options.DriForeignKeys = true;
            xfr.DestinationLoginSecure = true;

            xfr.CopySchema = false;
            xfr.CopyData = true;
            xfr.TransferData();

This works and all data get copied to the target DB. I need exclude one table from the copy process. I tried

            ServerConnection conn = new ServerConnection(sourceServer);
            conn.LoginSecure = true;
            Server srvSource = new Server(conn);
            Database dbSource = srvSource.Databases[sourceDB];

            Transfer xfr = new Transfer(dbSource);

            xfr.CopyAllTables = false;
            xfr.Options.WithDependencies = false;
            xfr.Options.ContinueScriptingOnError = false;
            xfr.DestinationDatabase = destDB;
            xfr.DestinationServer = destServer;
            xfr.Options.DriAllKeys = true;
            xfr.Options.DriForeignKeys = true;
            xfr.DestinationLoginSecure = true;

             foreach (Table tb in dbSource.Tables)
            {
                if (tb.IsSystemObject == false && tb.Name != "ExcludedTable" )
                {
                    xfr.ObjectList.Add(tb);
                }
            }

            xfr.CopySchema = false;
            xfr.CopyData = true;
            xfr.TransferData();

When TransferData is called I get an Exception "System.NullReferenceException" and no data is transfered

{"Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."}
    [System.NullReferenceException]: {"Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    HResult: -2147467261
    InnerException: null
    Message: "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."
    Source: "Microsoft.SqlServer.SmoExtended"
    StackTrace: "   bei Microsoft.SqlServer.Management.Smo.Transfer.GetObjectList()\r\n   
    bei Microsoft.SqlServer.Management.Smo.Transfer.Microsoft.SqlServer.Management.Common.ITransferMetadataProvider.SaveMetadata()\r\n 
    bei Microsoft.SqlServer.Management.Dts.DtsTransferProvider.Configure(ITransferMetadataProvider metadataProvider)\r\n  
    bei Microsoft.SqlServer.Management.Smo.Transfer.GetTransferProvider()\r\n   
    bei Microsoft.SqlServer.Management.Smo.Transfer.TransferData()\r\n  

    TargetSite: {Microsoft.SqlServer.Management.Smo.DependencyCollection GetObjectList()}

How can I exclude "ExcludedTable" from the export?


回答1:


Except xfr.CopyAllTables = false; you must also set:

xfr.CopyAllObjects = false;

It is set by default to true and that is the reason for the error you get. If you omit this line you get exactly the null reference exception that you mention.

(Verified your code and working with 110\SDK\Assemblies)



来源:https://stackoverflow.com/questions/26259512/transferring-data-by-smo-failing-when-using-objectlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!