2012-07-16 57 views
1

一个如何将能够使用TaskHost对象C#中(我使用它的SSIS脚本任务里面,但是这应该适用于C#一般为好),并表处理不属于在“dbo”架构中?SSIS - TaskHost对象 - 数据库架构

我已经写,基本上连接到源和目的地数据库代码,然后假定通过一个带跨越特定的表(在循环中)的一个,两个表图式和数据。

在我的本地,这个工程只是桃色,但在我的客户,供应商正在使用的模式;不是我们原先考虑到的,这给我带来一些麻烦。

我已经使用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来处理数据库架构?

+0

在你的注释代码,看起来你正在构建'schemaList'的值,使得它是schema.table如果你刚刚在那里指定了模式,该怎么办?这将与您在下面列出的表名的分配保持一致。 – billinkc 2012-07-16 15:44:56

+0

感谢您的评论。我之前也尝试过使用它,只是在没有得到任何正确的结果后才提到上面的内容。这真的让我失望,因为这是项目中最后一个缺失的环节。 – SchmitzIT 2012-07-17 11:35:03

+0

我只是双重检查,并在我的本地机器上做了更多的测试。运行探查器捕捉到了什么东西被发送到SQL Server,我看到以下内容: EXEC sp_executesql的N'SELECT SCHEMA_NAME(tbl.schema_id)AS [架构], tbl.name [名称] FROM SYS.TABLES AS tbl WHERE ([email protected]_msparam_0 and SCHEMA_NAME(tbl.schema_id)= @ _ msparam_1)',N'@_ msparam_0 nvarchar(4000),@ _ msparam_1 nvarchar(4000)',@ _ msparam_0 = N'tableName', @ _msparam_1 = N'dbo' 这甚至在使用SchemasList和正确的模式时,并确保CopyAllSchemas设置为false。 – SchmitzIT 2012-07-17 14:21:06

回答

0

事实证明,表不表,但views.Using一个意见收集的组合,并与我设法让架构一些额外的东西tweakings工作:)