问题
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