Sunday, March 11, 2012

Add scripttask script programmatically

I'm building packages programmatically. I need to add a ScriptTask to my package and include the script code. The script task itself is easy to add. But I can't figure out how to add the script code to the task.

I found one post in this forum saying the trick is to use the PutSourceCode method of the StriptTaskCodeProvider class, but I can't figure out how to do that.

Can anyone provide a code sample of how this is done?

Thanks.

After looking into this more deeply I found that adding a Script Task is a large job and I opted for a simpler approach to my particular issue. A lot of code needs to be added into the package. You have to create a VSA project, then add the script code it contains. To see an example of what's required, open a .dtsx file containing a Script Task and look for the tag "ScriptProject Name", and examine the ProjectItems it contains.

In case it is of use to anyone in the future, here's what I came up with. This doesn't create both the project and the script code because I didn't go that far, but it shows you the references and objects you'll need to do that.

Add references to Microsoft.SqlServer.ScriptTask and Microsoft.SqlServer.VSAHosting.

Dim exe As Executable = _Package.Executables.Add("STOCK:ScriptTask")

Dim thTask As TaskHost = CType(exe, TaskHost)

thTask.Name = "MyScriptTask"

Dim st As ScriptTask = TryCast(thTask.InnerObject, ScriptTask)

dim Moniker as String = "dts://Scripts/" & st.VsaProjectName & "/" & st.VsaProjectName & ".vsaproj"

st.ReadWriteVariables = "Var1, Var2"

Dim sb As New StringBuilder

sb.AppendLine("'Microsoft SQL Server Integration Services Script Task")

'build your code here

'this inserts the code into the script task

st.CodeProvider.PutSourceCode(Moniker, sb.ToString)

No comments:

Post a Comment