0

I am creating an SSIS package programatically in vb.net version 4.0 for SQL server 2008 R2 and I am having difficulty creating an Execute SQL Task. The code for the task is as follows:

  Public Sub CreateExecuteSQLTask()
    Dim TaskHost =  DirectCast(Package.Executables.Add("Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask, Microsoft.SqlServer.SQLTask, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"), Microsoft.SqlServer.Dts.Runtime.TaskHost) 
    TaskHost.Name = "Task"
    TaskHost.Description = "Task Description"
    Dim ExecuteSQLTask As Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask
    ExecuteSQLTask = DirectCast(TaskHost.InnerObject, ExecuteSQLTask)
    ExecuteSQLTask.Connection = DestinationDBConnectionManager.ID
    ExecuteSQLTask.SqlStatementSource = "Select * from Employees"
  End Sub

When the above code runs I get the following exception when casting the TaskHost to an Execute SQL command.

Unable to cast COM object of type 'System.__ComObject' to class type 'Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

I have used the following article as a reference:

Creating Packages in code

Please advise how I can get past this issue.

1
  • Which version of Visual Studio are you using? Commented Jan 21, 2013 at 17:58

2 Answers 2

1

I managed to work out the problem.

Dim ExecuteSQLTask As Dts.Tasks.ExecuteSQLTask.ExecuteSQLTask

needs to be changed to

Dim ExecuteSQLTask As Dts.Tasks.ExecuteSQLTask.IExecuteSQLTask

As the exception suggested, the COM component can be cast to an interfaces. In the above case, i was casting the COM component to the ExecuteSQLTask class rather than the interface.

I found that casting the COM component is a lot easier than setting the SQL task's properties through XML.

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is .Net 4.0. Or, more specifically, the ExecuteSQLTask assembly code has something in it that makes it incompatible with .Net 4.0. So just add the following lines to your app.config file:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<runtime>
  <NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>

And it'll work. No, I have no idea why.

1 Comment

Thank you for this suggestion. I had read an article that suggested this would resolve my problem, unfortunately it did not. I managed to solve this problem and have answered the question in this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.