如何在 C# 中使用 TaskHost 对象(我在 SSIS 脚本任务中使用它,但这通常也适用于 C#)并处理不在“dbo”模式中的表?
我已经编写了基本上连接到源数据库和目标数据库的代码,然后应该一个一个地跨越特定的表(在循环内),包括表模式和数据。
在我的本地主机上,这很有效,但在我的客户中,供应商正在使用模式;不是我们最初考虑的东西,这给我带来了一些麻烦。
我曾尝试使用 TaskHost 对象的“SchemasList”属性,但一直遇到同样的错误:
? 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}
这是代码:
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();
如您所见,有一个注释位试图查看我是否可以以这种方式添加架构。我尝试的另一个选项是使用 CopyAllSchemas 属性,但这也无济于事。
我还尝试在将表名添加到表 stringcollection 的代码行中添加架构,但这会产生相同的错误,无论是否使用方括号 ([])。默认情况下,TaskHost 似乎只接受“dbo”模式中的表,除非我遗漏了一些东西。
有谁知道我如何让 TaskHost 处理数据库模式?