2016-12-24 26 views
0

正在努力研究如何將參數映射添加到SSIS SQL任務。使用C#添加綁定到SSIS包的參數

so far I am able to create my SQL Task, and set a few properties. 
TaskHost taskhost = sequence1.Executables.Add("STOCK:SQLTask") as TaskHost; 
        taskhost.Name = myTable.ToString(); 
        taskhost.Properties["Connection"].SetValue(taskhost, connMgrOleDb.Name); 
        taskhost.Properties["SqlStatementSource"].SetValue(taskhost, "EXEC sp_GET_S_" + myTable.ToString() +"?, ?"); 


// need to add input parameter binding 

//任何想法??

回答

0

我看到您正在使用TaskHost對象來設置屬性。如果您想動態設置屬性,最好使用TaskHost。也可以直接使用底層控制流任務來設置屬性,並且在某些情況下更可取。

所以在這裏,您可以直接使用ExecuteSQLTask對象添加參數綁定。只需在ExecuteSqlTask​​的ParameterBindings屬性上調用Add方法即可。

要使用ExecuteSqlTask​​對象,您需要將程序集引用添加到Microsoft.SqlServer.SQLTask.dll。

因此,將TaskHost的InnerObject轉換爲ExecuteSqlTask​​,然後調用ExecuteSqlTask​​的ParameterBindings屬性的Add方法。同樣的模式也適用於添加結果綁定。

 // cast the TaskHost's InnerObject as ExecuteSqlTask 
     // add a reference to the Microsoft.SqlServer.SQLTask.dll to use ExecuteSqlTask 
     ExecuteSQLTask sqlTask = TaskHost.InnerObject as ExecuteSQLTask; 

     // call the Add method on ParameterBindings property of sqlTask. 
     //This will return a IDTSParameterBinding object 
     IDTSParameterBinding parameterBinding = sqlTask.ParameterBindings.Add(); 

     // using the IDTSParameterBinding object that is returned from the Add call, set the various properties 
     parameterBinding.ParameterName = "ParamName"; 
     parameterBinding.ParameterDirection = ParameterDirections.Input; 
     // set more properties on parameterBinding as needed ...... 
+0

謝謝你的作品 – Tempest