2012-07-16 58 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工作:)