How would one be able to use a TaskHost object within C# (I use it inside an SSIS scripting task, but this should apply to C# in general as well) and deal with tables that are not in the “dbo” schema?
I have written code that basically connects to a source and a destination database, and then is supposed to bring across specific tables (inside a loop) one by one, both the table-schemas and the data.
On my localhost, this works just peachy, but at my client, the supplier is using schemas; not something we took into account originally, and this is giving me some trouble.
I have tried using the “SchemasList” property of the TaskHost object, but keep running into the same error:
? child.Errors[0]
{Microsoft.SqlServer.Dts.Runtime.DtsError}
base {Microsoft.SqlServer.Dts.Runtime.DtsObject}: {Microsoft.SqlServer.Dts.Runtime.DtsError}
Description: "Table \"[theSchema].[theTable]\" does not exist at the source.\r\n"
ErrorCode: -1073548445
HelpContext: 0
HelpFile: ""
IDOfInterfaceWithError: "{B6F6D221-FC27-4F71-B5A0-597583986C28}"
Source: "{D6DF0C1F-0AC8-4748-836C-BEF052454AEE}"
SubComponent: "Transfer SQL Server Objects Task"
TimeStamp: {16-07-2012 13:41:43}
Here’s the code:
if (Dts.Variables["tableName"].Value.ToString() != "0") {
string tableName;
tableName = Dts.Variables["tableName"].Value.ToString();
// Add a child package
Package child = new Package();
child.Name = tableName;
Executable moveTable = child.Executables.Add("STOCK:TransferSQLServerObjectsTask");
TaskHost moveTableTask = (TaskHost)moveTable;
// Set properties for the task
moveTableTask.Properties["CopyAllObjects"].SetValue(moveTableTask, false);
moveTableTask.Properties["CopyAllTables"].SetValue(moveTableTask, false);
moveTableTask.Properties["CopySchema"].SetValue(moveTableTask, true);
moveTableTask.Properties["DropObjectsFirst"].SetValue(moveTableTask, true);
moveTableTask.Properties["CopyData"].SetValue(moveTableTask, true);
// Set schemas
//StringCollection schemas = new StringCollection();
//String[] schemaList = new String[] {"[theSchema].[" + tableName + "]"};
//schemas.AddRange(schemaList);
//moveTableTask.Properties["SchemasList"].SetValue(moveTableTask, schemas);
// Set tablenames
StringCollection tables = new StringCollection();
tables.Add(tableName);
moveTableTask.Properties["TablesList"].SetValue(moveTableTask, tables);
// Set up connections
ConnectionManager source;
ConnectionManager destination;
source = child.Connections.Add("SMOServer");
source.ConnectionString = "SqlServerName=*****;UseWindowsAuthentication=False;UserName=*****;Password=*****;";
source.Name = "Source";
destination = child.Connections.Add("SMOServer");
destination.ConnectionString = "SqlServerName=*****;Persist Security Info=True;Password=*****;USER ID=*****;Initial Catalog=*****";
destination.Name = "Destination";
moveTableTask.Properties["SourceConnection"].SetValue(moveTableTask, "Source");
moveTableTask.Properties["SourceDatabase"].SetValue(moveTableTask, "*****");
moveTableTask.Properties["DestinationConnection"].SetValue(moveTableTask, "Destination");
moveTableTask.Properties["DestinationDatabase"].SetValue(moveTableTask, "*****");
child.Execute();
child.Dispose();
As you can see, there is a commented bit which was an attempt to see if I could add the schema that way. Another option I tried was using the CopyAllSchemas property, but that also did nothing.
I also tried adding the schema in the line of code where the tablename is added to the tables stringcollection, but that yields the same error, regardless of using square brackets ([]) or not. It seems the TaskHost by default only accepts tables in the “dbo” schema, unless I am missing something.
Does anyone have any idea on how I could get the TaskHost to handle the database schema?
As it turns out, the tables were not tables, but views.Using a combination of a Views collection, and some additional tweakings with the schemas I managed to get things to work 🙂