Delen via


Connecting Tasks Programmatically

A precedence constraint, represented in the object model by the PrecedenceConstraint class, establishes the order in which Executable objects run in a package. The precedence constraint allows the execution of the containers and tasks in a package to be dependent on the result of the execution of a previous task or container. Precedence constraints are established between pairs of Executable objects by calling the Add method of the PrecedenceConstraints collection on the container object. After you create a constraint between two executable objects, you set the Value property to establish the criteria for executing the second executable defined in the constraint.

You can use both a constraint and an expression in a single precedence constraint, depending on the value that you specify for the EvalOp property, as described in the following table:

Value of the EvalOp property

Description

Constraint

Specifies that the execution outcome determines whether the constrained container or task runs. Set the Value property of the PrecedenceConstraint to the desired value from the DTSExecResult enumeration.

Expression

Specifies that the value of an expression determines whether the constrained container or task runs. Set the Expression property of the PrecedenceConstraint.

ExpressionAndConstraint

Specifies that the constraint outcome must occur and the expression must evaluate for the constrained container or task to run. Set both the Value and the Expression properties of the PrecedenceConstraint, and set its LogicalAnd property to true.

ExpressionOrConstraint

Specifies that either the constraint outcome must occur, or the expression must evaluate, for the constrained container or task to run. Set both the Value and the Expression properties of the PrecedenceConstraint, and set its LogicalAnd property to false.

The following code sample demonstrates adding two tasks to a package. A PrecedenceConstraint is created between them that prevents the second task from running until the first task finishes.

using System;
using Microsoft.SqlServer.Dts.Runtime;

namespace Microsoft.SqlServer.Dts.Samples
{
  class Program
  {
    static void Main(string[] args)
    {
      Package p = new Package();

      // Add a File System task.
      Executable eFileTask1 = p.Executables.Add("STOCK:FileSystemTask");
      TaskHost thFileHost1 = eFileTask1 as TaskHost;

      // Add a second File System task.
      Executable eFileTask2 = p.Executables.Add("STOCK:FileSystemTask");
      TaskHost thFileHost2 = eFileTask2 as TaskHost;

      // Put a precedence constraint between the tasks.
      // Set the constraint to specify that the second File System task cannot run
      // until the first File System task finishes.
      PrecedenceConstraint pcFileTasks = 
        p.PrecedenceConstraints.Add((Executable)thFileHost1, (Executable)thFileHost2);
      pcFileTasks.Value = DTSExecResult.Completion;
    }
  }
}
Imports Microsoft.SqlServer.Dts.Runtime

Module Module1

  Sub Main()

    Dim p As Package = New Package()
    ' Add a File System task.
    Dim eFileTask1 As Executable = p.Executables.Add("STOCK:FileSystemTask")
    Dim thFileHost1 As TaskHost = CType(eFileTask1, TaskHost)

    ' Add a second File System task.
    Dim eFileTask2 As Executable = p.Executables.Add("STOCK:FileSystemTask")
    Dim thFileHost2 As TaskHost = CType(eFileTask2, TaskHost)

    ' Put a precedence constraint between the tasks.
    ' Set the constraint to specify that the second File System task cannot run
    ' until the first File System task finishes.
    Dim pcFileTasks As PrecedenceConstraint = _
      p.PrecedenceConstraints.Add(CType(thFileHost1, Executable), CType(thFileHost2, Executable))
    pcFileTasks.Value = DTSExecResult.Completion

  End Sub

End Module
Integration Services icon (small) Stay Up to Date with Integration Services

For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN or TechNet:

For automatic notification of these updates, subscribe to the RSS feeds available on the page.