优先约束在对象模型中由 PrecedenceConstraint 类表示,它确立 Executable 对象在包中的运行顺序。 优先约束允许包中容器和任务的执行取决于前一任务或容器的执行结果。 优先约束是通过调用容器对象中的 Executable 集合的 Add 方法,在成对的 PrecedenceConstraints 对象之间建立的。 在两个可执行对象之间创建约束后,设置 Value 属性可建立约束中定义的第二个可执行对象的执行条件。
在一个优先约束中既可以使用约束,又可以使用表达式,具体取决于为 EvalOp 属性指定的值,如下表中所述:
| EvalOp 属性的值 | 说明 | 
|---|---|
| Constraint | 指定执行结果确定受约束的容器或任务是否运行。 请将 Value 的 PrecedenceConstraint 属性设置为 DTSExecResult 枚举中的所需值。 | 
| Expression | 指定表达式的值确定受约束的容器或任务是否运行。 请设置 Expression 的 PrecedenceConstraint 属性。 | 
| ExpressionAndConstraint | 指定约束结果必须发生且表达式必须计算,受约束的容器或任务才能运行。 设置 Value 的 Expression 和 PrecedenceConstraint 属性,并将其 LogicalAnd 属性设置为 true。 | 
| ExpressionOrConstraint | 指定约束结果必须发生,或者表达式必须计算,受约束的容器或任务才能运行。 设置 Value 的 Expression 和 PrecedenceConstraint 属性,并将其 LogicalAnd 属性设置为 false。 | 
下面的代码示例演示将两个任务添加到包中。 在它们之间创建 PrecedenceConstraint,以防止第二个任务在第一个任务完成之前运行。
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
              
              随时了解 Integration Services
 有关来自Microsoft的最新下载、文章、示例和视频,以及来自社区的所选解决方案,请访问 MSDN 上的 Integration Services 页面:
 
              访问 MSDN 上的 Integration Services 页
 若要获得有关这些更新的自动通知,请订阅该页上提供的 RSS 源。